4

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?

Andrew Gray
  • 3,593
  • 3
  • 35
  • 62
Peter
  • 302
  • 3
  • 16

3 Answers3

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.

Andrew Gray
  • 3,593
  • 3
  • 35
  • 62
  • 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? – Peter Oct 16 '18 at 14:39
  • 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. – Andrew Gray Oct 16 '18 at 22:29
  • 2
    BTW, there also built-in conditions as well https://jenkins.io/doc/book/pipeline/syntax/#when `when { branch 'master' }` – Sasha Miroshnychenko Apr 05 '19 at 10:13
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.

ben5556
  • 2,915
  • 2
  • 11
  • 16
  • 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? – Oli Feb 21 '19 at 16:00
  • Oh, I did it! I just struggled to interpret the documentation. Thanks for your answer. It helped me a lot. – Oli Feb 21 '19 at 16:12
  • Glad it helped :) – ben5556 Feb 21 '19 at 18:40
  • 4
    You really need to provide an example. Otherwise your answer is useless. – stricq May 13 '19 at 01:11
1

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

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

JRichardsz
  • 14,356
  • 6
  • 59
  • 94