简体   繁体   中英

Jenkins MultiBranch Pipeline: Select to build only 2 specific branches

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)'
                   }
                }
        }
}

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 eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM