Limit which branch is built by Jenkins pipeline?
Asked Answered
T

3

5

I am currently configuring a Jenkins server hosted on a Docker container in AWS.

I am using BlueOcean to configure a repository.

Right now, the pipeline scans all branches on a repository to detect Jenkinsfiles and then will automatically build on that branch if it detects changes. I scan the repo every 5 minutes to detect changes.

However, I do not want to be running builds and jobs automatically if it is some random feature branch. I am trying to limit the automatically triggered builds to only changes in staging and master branches.

So my question is, how/where do you configure Jenkins GitHub pipeline to only build on certain branches rather than scanning all branches?

Tenorrhaphy answered 15/10, 2018 at 17:3 Comment(0)
T
5

A Multibranch pipeline job is your friend.

Rather than trying to limit which branches Jenkins is polling firstly what I do in my Jenkinsfile is poll source control every minute:

triggers { pollSCM('* * * * *') }

This will poll every branch and create a job where it finds a Jenkinsfile in the location and name you specify in the Multibranch Pipeline job configuration.

Side Note

About the only configuration in a multibranch pipeline is:

  1. Where's the SCM repo?
  2. Workspace relative path and name of Jenkinsfile. (You can call it Bob if you want)

A multibranch pipeline job sets an additional environment variable: BRANCH_NAME which allows you to conditionally perform actions in pipeline like so:

script {
    if( "${env.BRANCH_NAME}" == "integration" ) {
        //Do something useful
    }
}

Using this method you can also decide to do nothing in response to a poll event.

Tribunate answered 16/10, 2018 at 4:45 Comment(3)
this nailed it. I had a follow up question if possible. I am running Jenkins master on a container, and I can spawn and run individual jobs on slave containers but when I try to run a pipeline using the same docker cloud I have made, the pipeline immediately crashes and states line 2: docker: command not found. Any idea why that happens in pipeline but not in individual jobs?Tenorrhaphy
Thanks Peter. I have not used docker yet. I would suggest reading up on Jenkins Pipeline on how to call docker. Remember pipeline is still not fully functional as compared to freestyle, although it is filling out fast.Tribunate
BTW, there also built-in conditions as well jenkins.io/doc/book/pipeline/syntax/#when when { branch 'master' }Fanniefannin
J
2

Pipeline accepts input parameters. So you can create a parameter called branch.

Inside your pipeline you could use regex to match only required branches.

Jehoash answered 16/10, 2018 at 1:22 Comment(0)
M
1

I assume you are using github plugin. I'd suggest configuring a webhook on your repository using Generic Webhook Trigger Plugin - https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin

This plugin is awesome and lets you easily extract the values in the incoming webhook and use those in your pipeline. For ex. you can extract the branch from where the webhook came from and only build if the branch is staging or master

In our setup we use a simple job 'webhook trigger processor' which reads the incoming webhook from all repositories and triggers downstream pipelines using values extracted from webhook.

Maroon answered 15/10, 2018 at 22:21 Comment(4)
I would love to know more about how you "extract the branch from where the webhook came from and only build if the branch is x"! Could you share this please?Verda
Oh, I did it! I just struggled to interpret the documentation. Thanks for your answer. It helped me a lot.Verda
Glad it helped :)Maroon
You really need to provide an example. Otherwise your answer is useless.Pipage

© 2022 - 2024 — McMap. All rights reserved.