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

Declarative pipeline branch condition not working

      I am working with a simple pipeline, just to get familiar with the system.  My pipeline is defined in the attached Jenkinsfile.  I can't seem to get the "Deploy" stage to work.  I am using the simple "when { branch 'master' }".  However I keep getting: Stage 'Deploy' skipped due to when conditional

      NOTE: This is a single branch repo with only a master branch

      Jenkins Version: 2.51

      Pipeline Plugin Version: 2.5

       

      Running on AWS Linux image. I followed this setup guide: https://aws.amazon.com/getting-started/projects/setup-jenkins-build-server/

       

          [JENKINS-43104] Declarative pipeline branch condition not working

          Andrew Bayer added a comment -

          So...yeah. There's no way to know with consistency what branch we're on without branch-api providing BRANCH_NAME. You can do something custom yourself to check for the branch name in a non-multibranch Jenkinsfile, though, like

          when {
            expression {
              return sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true) == "master"
            }
          }
          

          Andrew Bayer added a comment - So...yeah. There's no way to know with consistency what branch we're on without branch-api providing BRANCH_NAME . You can do something custom yourself to check for the branch name in a non-multibranch Jenkinsfile, though, like when { expression { return sh(script: "git rev-parse --abbrev-ref HEAD" , returnStdout: true ) == "master" } }

          Nicolas Picon added a comment -

          This should be made clearer in the documentation. There is no mention of the when-branch directive only working for multibranch pipeline

          https://jenkins.io/doc/book/pipeline/syntax/#when

           

          Nicolas Picon added a comment - This should be made clearer in the documentation. There is no mention of the when-branch directive only working for multibranch pipeline https://jenkins.io/doc/book/pipeline/syntax/#when  

          Andrew Bayer added a comment -

          Andrew Bayer added a comment - npicon Good catch - opened https://github.com/jenkins-infra/jenkins.io/pull/982 to update that.

          Liam Newman added a comment -

          abayer
          I'd suggest that this is still a defect. The defect is in an underlying plugin, but it is still a defect and should be fixed.
          Aren't there related bugs filed, that this could be marked as dependent on instead of resolving it?

          Liam Newman added a comment - abayer I'd suggest that this is still a defect. The defect is in an underlying plugin, but it is still a defect and should be fixed. Aren't there related bugs filed, that this could be marked as dependent on instead of resolving it?

          Andrew Bayer added a comment -

          bitwiseman I disagree - this isn't a bug anywhere, it's just that when branch doesn't work outside of multibranch. That's as designed, not a bug.

          Andrew Bayer added a comment - bitwiseman I disagree - this isn't a bug anywhere, it's just that when branch doesn't work outside of multibranch. That's as designed, not a bug.

          Code changed in jenkins
          User: Andrew Bayer
          Path:
          content/doc/book/pipeline/syntax.adoc
          http://jenkins-ci.org/commit/jenkins.io/37472aedc9de44ca1f3cbe98c48becfefe8f706f
          Log:
          JENKINS-43104 Clarify that when->branch only works on multibranch

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: content/doc/book/pipeline/syntax.adoc http://jenkins-ci.org/commit/jenkins.io/37472aedc9de44ca1f3cbe98c48becfefe8f706f Log: JENKINS-43104 Clarify that when->branch only works on multibranch

          Code changed in jenkins
          User: R. Tyler Croy
          Path:
          content/doc/book/pipeline/syntax.adoc
          http://jenkins-ci.org/commit/jenkins.io/69263ee53b6f54e1d31485149be76fe08388a212
          Log:
          Merge pull request #982 from abayer/when-branch-only-for-multibranch

          JENKINS-43104 Clarify that when->branch only works on multibranch

          Compare: https://github.com/jenkins-infra/jenkins.io/compare/2822a194249e...69263ee53b6f

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: R. Tyler Croy Path: content/doc/book/pipeline/syntax.adoc http://jenkins-ci.org/commit/jenkins.io/69263ee53b6f54e1d31485149be76fe08388a212 Log: Merge pull request #982 from abayer/when-branch-only-for-multibranch JENKINS-43104 Clarify that when->branch only works on multibranch Compare: https://github.com/jenkins-infra/jenkins.io/compare/2822a194249e...69263ee53b6f

          Miyata Jumpei added a comment -

          FYI, trim() is required for the workaround in my case.

          when {
            expression {
              return sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim() == "master"
            }
          }
          

          Miyata Jumpei added a comment - FYI, trim() is required for the workaround in my case. when {   expression {     return sh(script: "git rev-parse --abbrev-ref HEAD" , returnStdout: true ).trim() == "master"   } }

          Raymond Chiu added a comment -

          Another workaround is to use:

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

           

          Raymond Chiu added a comment - Another workaround is to use: when { expression { return env.GIT_BRANCH == "origin/master" } }  

          Liam Newman added a comment -

          Bulk closing resolved issues.

          Liam Newman added a comment - Bulk closing resolved issues.

            abayer Andrew Bayer
            d_smith Derek Smith
            Votes:
            3 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: