Multibranch pipeline per-branch parameters - jenkins

I have a build job on Bamboo that has parameterized branches. All branches (for e.g. feature branches created by developers at will) have a default set of parameters, and specific branches override some of them. Note that once I set a variable for a given branch to a certain value, every build of that branch takes that value. I'm trying to recreate this in Jenkins, but there doesn't seem to be a way to do so.
I created a multibranch pipeline and defined the parameters in my Jenkinsfile with suitable defaults. However, the only way I can override the parameters for a branch is to click "Build with parameters" and update them. That works for that build, but next build that gets triggered reverts back to using the default values. This seems like such a basic use case that I feel like I must be missing something or misconfigured the project somehow, considering I don't have any experience using Jenkins before trying this. Thanks in advance for your help!

Related

How to take branch specifier as user input along with other hard coded branch specifiers

I have provided branch specifiers as shown in below image. If I remove the branch specifier with $BRANCH in it, job works perfectly(gets triggered) as anyone commits to develop or feature branches.
Now I also want that same job should also be able to build the project on any specific branch of choice. So I added a branch specifier and put $BRANCH in it. I have defined BRANCH as a string parameter and the user can give branch value as input when triggering the job manually.
But in this scenario even if the user gives a "bugfix112" job is still building develop branch. Or if earlier poll action had built the job on feature branch than even after providing user-input, the job will still build feature branch.
It seems like manual input has no effect. How can I configure a Jenkins job that can be triggered on poll SCM and also with user input for the branch parameter?
i also tried giving */$BRANCH,*/${BRANCH} but no success.
So I had raised above issue with Jenkins git plugin author and it looks that for the above scenario we have to use the multi-branch pipeline. In fact, I agree with the statement.
https://issues.jenkins-ci.org/browse/JENKINS-61074

Jenkins Multibranch Config: How to Filter branches based on variable string?

We have Jenkins set up with 7 multibranch pipeline projects, each building off the same git repo, but for different target platforms. Each of these multibranch pipelines builds a number of branches. We currently set which branches each multibranch pipeline builds by using the following property in the multibranch project configuration:
Branch Sources -> Git -> Behaviors -> Filter by name (with wildcards)
Currently, each multibranch pipeline has the same string of branches in this Filter by name (with wildcards) field. Each time we want Jenkins to start building a new branch, we go through all 7 multibranch project configurations and update this field to include the new branch.
It's a bit of a pain to go through each configuration and change this field every time, since we always want each configuration to have the same list of branches. Is it possible to simply use some type of a variable in this field? This way we only would need to change one location instead of trying to keep 7 different configurations in sync with each other, which is prone to error and also a bit of a pain.
Thanks for your help!
Allen
Rather than filtering with wildcards, you could try filtering branches with regular expression. In our case, pattern like:
(master|develop|release.*|feature.*|bugfix.*)
has been working well to cover the repository. That is, assuming that you follow Git Flow or similar methodology. Unfortunately, there is no simple way to sync the configuration between MultiBranch Pipelines build from one repository. Neither Multibranch Pipeline, nor Organization plugins are designed to work with Multiple Jenkinsfiles.
Also, you can try to sync only the branch configuration between Projects using Jenkins script console. Most of the Job configuration does not have to be set on Project level. For instance, you can create shared script (or shared library) to would be sourced by other jobs, to set the same job properties on each of them. See How do you load a groovy file and execute it for details.
if you want to use the wildcard you can provide like below:
In this example it will discover only qa and dev branch.
NOTE: You have to use "Discover branches" also with "filter by name (with wildcards)" behaviour.

Multibranch Pipeline - configure branch projects

When I create a plain pipeline project I have the option to periodically poll the scm and if changes are detected, the build is run. that worked well for me.
Now I created a multibranch pipeline and added 2 branches. However, in the configuration I can not set the same as in the normal pipeline project, because it tells me I can only view the configurations of the sub-branch-projects.
Maybe I'm also doing it wrong, so I try to tell you what I actually want to achieve.
I have a PHP project inside of a Git repository. There are two branches that I want to be built on new commits (when pushed to the main repository)
The main repository resides on a self hosted version of Bitbucket Server. If possible, I want to avoid hooks and let Jenkins poll for changes on the bitbucket server. So how can I achieve that?
You just need to check the option Periodically if not otherwise run trigger at the multibranch level. This replaces per-job polling, because it also detects new branches and the like.
If I got you right, all you want to do is to build ONLY these 2 branches?
If so, under "Branch Sources" just click the "Advanced" and fill in the textfield "Include branches" your branches e.g. test test2 (note the space between the branch names)
Actually you'll have configure SCM pollig (the way you expect it) in the Jenkinsfile itself. It's the properties DSL elelement that handles the configuration:
properties([
pipelineTriggers([pollSCM('H 20 * * 1-5')])
])
Anyways I highly suggest to have a closer look at the Pipeline Snippet Generator:
<your-jenkins-url>/pipeline-syntax/
it s easy to miss but extremely helpful and it s populated based on your currently installed plugins. There you will also find a comprehensible set of options available for properties

Jenkins schedule multiple versions of the same build

I have a project that has 3-5 different mercurial branches going at all times. I want to schedule a weekly Jenkins test to run our tests on all relevant branches.
What I want, I think, is a parameterized build, with the branch name as the parameter, and then to have a list of branches, and once a week, run the parameterized build with each of the parameters in the list.
However, I see that you can't send parameters into a triggered build. I assume that there is a plugin for this. Is job Generator the correct plugin? Is there something better?
I should mention that currently, we are doing this with multiple SCMs, and having the body of the build have a sh loop that runs through each directory and runs the tests. This is really inefficient, and a pain to maintain...
I can suggest one solution but it couldn't be called elegant.
Firstly, you need create multi-configuration project (aka Matrix project).
In this project you need declare one node (it can be already existed master node)
And one type of axis (for example BRANCH - be careful don't use Jenkins Set Environment Variables variables) with values corresponding for each branch (for example default, testing, devel, etc).
After you need add in your project build action in which you need check environment variable (previously declared $BRANCH) and discover for which branch this build was launched (the main idea is illustrated by example with using bash).
And finally you need manually get sources from corresponding branch.
Next build steps can be the same for all branches.
This approach have set of drawbacks:
1. You can not triggered this project by changes in repository (you can check using Mercurial plugin only one branch).
2. All subprojects will be rebuilt even if they have not changed.
3. Appropriate only for statically defined branches.
4. Not elegant.
But it has one advantage versus parameterized build:
1. All artifacts (and build logs) of branches is stored in separated directories (because they are separate subprojects).

Jenkins Perforce Label Sharing across jobs

Is there are a way one can share variables across jenkins jobs ?
Job1 collects the required source code and labels them using perl scripts.
Then a number of other jobs compiles the code since there are many versions. As of now i have made the other jobs depend on Job1 so that same code could be collected from head since it was labelled just before in Job1, but this was not the case during release since codes were going in the repository at odd hours so we had no control, so we thought it would be nice if we could find a way to sync the code using perforce label created in Job1. I did not find any way to sync to particular label that got created in a different job.
So i thought if we could set an environment variable and then use the same for the following jobs, then the codes can be in perfect sync. But seems like environment variables cannot be shared across jobs.
I would appreciate any ideas and help.
Can you use the "Use Upstream Project revision" option? It allows you to sync to the changeset of another project.
If you want to stick to the label idea, I think it's doable. I haven't tried this, but I think I would first have the first job create a new label based on the job name and the build number; both are available in the create label post-build action.
If you launch the downstream job using the paramaterized trigger plugin it will have access to the upstream job name and build number as environment variables. The 'P4 Label' field in the downstream job can then use parameter substitution to specify the correct label name to sync to.
Perforce plugin can help you.
Look at section "Sync multiple builds to the same changeset".

Resources