9

Using a multibranch pipeline I'd like trigger a slightly different build and deploy procedure depending on which git branch has triggered the build.

The two approaches I can think of are : 1) Use a different jenkins file in each branch 2) Use a series of when {branch 'X'} blocks in the jenkins file

The first approach mean I'll need to be careful when merging branches which I know I'll forget to be at some point.

The second approach is pretty messy but does mean I can use just one Jenkins file.

I can't believe there isn't a better way than these two approaches.

2 Answers 2

29

Even though I am not a DevOps expert, I can suggest using when conditions in your pipeline so that some stages are only triggered if when conditions are satisfied.

pipeline { triggers { # triggered by changes in every branch } stages { stage('first-stage'){ when { anyOf { branch 'feature-branch/*'; branch 'master' } } steps{ .... } } stage('second-stage'){ when { not { branch 'release/*' } not { branch 'staging' } } steps{ .... } } } } 

Or you can have multiple job templates using job-builder plugin. Then you can create different projects with different branch parameters materializing job templates. This is a bit complex and I would go with the "pipeline with when conditions" above.

This link may also help: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/

Sign up to request clarification or add additional context in comments.

1 Comment

The documentation says branch condition only works with multi-branch pipelines.
0

This worked for me

when { expression { return env.GIT_BRANCH == "origin/master" } } 

Make sure the branch selected in Jenkins configuration Git SCM ismaster

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.