Jenkins. How to configure tasks for specific git branch? - jenkins

I have multi-configuration project in Jenkins. My git repository have different branches. For example:
dev
stage
bug/code1
feature/code2
etc...
I want to create different Post build tasks, Publish HTML reports etc for each branch.
What is the problem? I changed configuration for stage branch. All works fine only before Branch Indexing. After this process, custom configuration for each branch replaced by multi-configuration project. It means if I added specific task only for stage branch, after Branch Indexing task will be removed.
Multi-Branch Project Plugin says this:
Sub-projects appear to be configurable, but they will be overwritten
by branch indexing if you manually modify them. There is no clear way
to remove or hide the configuration option on sub-projects (except
maybe with project-based matrix authorization??), though version 0.1.x
of this plugin accomplished that via some trickery that is not
possible in newer versions.
So my question is: How I can create custom configuration for each branch? Or what is the best solution for this? Maybe I should create different projects for stage, dev branches?
Thank you.

We have the similar situation like you, more than 10 branches need to be maintained. Instead of using multi branch plugin, we use job dsl to create the jobs for each branch.
For example, hello_branch1, hello_branch2....
Inside our job dsl project, we save the different json config for the different branches. In your case, you can think we save the post build script, or report to be published....
This will make sure the generated jobs are standalone and will not affect each other.
Br,
Tim

Related

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.

Is there a way to automatically build tags using the Multibranch Pipeline Jenkins plugin?

After creating a Multibranch Pipeline in Jenkins I can easily tell it to poll for changes or additions of any branches and it will automatically create jobs for and build those branches. I told the Multibranch job to also discover tags, so it automatically creates jobs for each tag, which is great.
Is there a clean way for Jenkins to automatically build those tags as well, instead of me having to trigger them manually? As you can see below, the job for the tag is there, but I have to manually build it.
I would have commented with this, but I don't have enough reputation. I believe this is a duplicate of Jenkins Multi-branch pipeline doesn't schedule tag jobs. See my answer there, copied below.
In short, if you build and install the Jenkins plugin available at https://github.com/AngryBytes/jenkins-build-everything-strategy-plugin then you can add a Build Everything strategy which will automatically build tags.
Not automatically triggering a build for discovered tags seems to be by design according to JENKINS-47496. Stephen Connolly offers an explanation and suggestion for what you might do:
Stephen Connolly added a comment - 6 days ago
Tags are not built by default (because otherwise you could have a
build storm when checking out a repository) and worse, the order tags
will be built in is unpredictable... and you might have a Jenkinsfile
that deploys to production when a tag is built.
There is an extension point in branch-api called BranchBuildStrategy
which - if implemented - will allow deciding whether to build tags.
See
https://github.com/jenkinsci/github-branch-source-plugin/pull/158#issuecomment-332773194
for starting point on how to create such an extension plugin... I
believe there is some work on one at
https://github.com/AngryBytes/jenkins-build-everything-strategy-plugin
As #tommy-ludwig says, you need an additional plugin providing a Build Strategy ; since his original post, Stephen Connolly has published the Basic Branch Build Strategies Plugin which among others provides a strategy to build tags.
If, like me, you don't care if the tags show up on a separate tab, you just want the tags to be visible when your various branches are being built (which implies that your tags will be built), then do this:
In your multibranch pipeline's configuration, go to "Branch Sources", "Git", "Behaviors", and add "Advanced clone behaviors". By default, when you add this you will see that "Fetch tags" is checked. Voila!

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).

TFS build, one build for many branches

We use TFS source control and have two build controllers (one VS2010 the other VS2012). We use the default build workflow template bar a few custom changes.
As we have many branches, how would we go about changing the workspace based on the desired branch?
I was thinking of adding a parameter for the branch ($\oursourcecontrol\branches\main) and then just passing it as a variable in to the get workspace part of the workflow.
Or is there a better way of doing it?
You currently can't and it is really a shame. It would completely break the Trigger support for CI and Gates checkins. You can use the TFS Community Build Manager to quickly clone and adjust build definitions to support multiple branches though.
What you could do, is to fetch more than you need (say $/Sourcecontrol/Branches/*) and then use a string-replace on any path parameter (like solutions to build, test settings file etc) to point them all to the correct solution. You'll probably need to do some templating like {BRANCH} so that you can easily replace these tokens.
The Git build template does support this on a Git repository, but you'd need TFS 2013 to make use of server side Git support.
I mapped the server path in the Repository tab at higher level that TFS branches are contained. Cloak rest of the branches not required for Get sources step except the branch to be built. And create variable to pass the branch name, check allow at queue time. Use this branch variable in build steps to make the build definition work for all the branches. You may need more than one variable based on your branches structure in TFS.

Resources