3

I have a Jenkins MultiBranch project and I want the test circle to run only on two specific branches on master and on dev. I tried to add on all stages the following

when { anyOf { branch 'master'; branch 'dev' } } 

but the only thing I managed to achieve was to deactivate all branch runs

Here is my full pipeline Jenkinsfile

 pipeline { agent any triggers { cron('H 0 * * *') } options { disableConcurrentBuilds() } stages { stage('Prepare env') { when { anyOf { branch 'master'; branch 'dev' } } steps { sh 'rm -rf venv' sh 'rm -rf "${WORKSPACE}/uploads"' sh 'rm -rf "${WORKSPACE}/downloads"' sh 'mkdir "${WORKSPACE}/uploads"' sh 'mkdir "${WORKSPACE}/downloads"' catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { sh 'docker kill $(docker ps -q)' sh 'docker rm $(docker ps -a -q)' sh 'docker volume rm $(docker volume ls -q)' } } } stage('Start Services') { when { anyOf { branch 'master'; branch 'dev' } } steps { } } stage('Test Common') { when { anyOf { branch 'master'; branch 'dev' } } steps { } } stage('Test Validations') { when { anyOf { branch 'master'; branch 'dev' } } steps { } } stage('Test CSV Issuance') { when { anyOf { branch 'master'; branch 'dev' } } steps { } } stage('Test XLS Issuance') { when { anyOf { branch 'master'; branch 'dev' } } steps { } } stage('Clean env') { when { anyOf { branch 'master'; branch 'dev' } } steps { sh 'rm -rf venv' sh 'rm -rf "${WORKSPACE}/uploads"' sh 'rm -rf "${WORKSPACE}/downloads"' catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { sh 'docker kill $(docker ps -q)' sh 'docker rm $(docker ps -a -q)' sh 'docker volume rm $(docker volume ls -q)' } } } } 

1 Answer 1

4

Can you post the full pipeline you have?

You would use the when block on the stage you want run only on the two branches e.g.

pipeline { agent any stages { stage ("Testing") { when { anyOf { branch 'master' branch 'dev' } } steps { echo "run testing" } } stage ("everything") { steps{ echo "run on all branches" } } } } 

tested pipeline

pipeline { agent any stages { stage("stage") { when { anyOf { branch 'master'; branch 'dev' } } steps { echo "Hello" } } } } 

on master

[Pipeline] stage [Pipeline] { (stage) [Pipeline] echo Hello [Pipeline] } [Pipeline] // stage 

On Fish

[Pipeline] stage [Pipeline] { (stage) Stage "stage" skipped due to when conditional [Pipeline] } [Pipeline] // stage 
Sign up to request clarification or add additional context in comments.

6 Comments

I added almost full of the file!
OK so you have the when clause on every stage therefore there are no stages to run on branches that are not master or dev. What are you expecting to happen?
the problem is that master or dev branche are not running either!
Thats strange, can you post some of you console log. I have just tested with a simple pipeline (see above) and it is working. So be interesting to see why it is skipping in your case
Is there any other way through multibranch project configuration to set these two branches as the only to activate a run?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.