Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-40370

Run stage when branch name matches

XMLWordPrintable

      Motivation

      Commonly people want to run particular stages when they match a specific branch name(s). We would like to make this pattern accessible in the editor.

      Declarative allows the use of when to condition the stage execution like:

      stage('second') {
          agent label:'some-node'
          branch "master" 
          when {
              env.BRANCH == 'master'
          }
      }
      

      However, this isn't exactly what we would consider "friendly" for an editor accessible feature (though when will be supported via a text area) and the developer would have to learn the Script syntax to use it correctly.

      Solution

      We would like to formalise the pattern in a way that is more Editor and user friendly with the introduction of branch. Ideally we could tell from BO if the user has skipped via when or via branch.

      Example - match single branch
      This stage would only be executed if master was the name of the current branch.

      stage('deploy to staging') {
          agent label:'some-node'
          when { branch "master" }
          steps {
              sh './deploy_pr.sh'
          }
      }
      

      Example - match branch name pattern
      This stage would only be executed if the branch name started with feature/.

      stage('deploy to staging') {
          agent label:'some-node'
          when { branch "feature/*" }
          steps {
              sh './deploy_pr.sh'
          }
      }
      

      Example - expression
      You can use an expression to achieve the same thing by:

      stage('deploy to staging') {
          agent label:'some-node'
          when {
      	expression {
      		return BRANCH == 'master';
              }
          }
          steps {
              sh './deploy_pr.sh'
          }
      }
      

            rsandell rsandell
            jamesdumay James Dumay
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: