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

Provide easy access to git branch name in multibranch workflow scripts

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • branch-api-plugin
    • None

      Popular maven plugins designed to give you access to the git branch name are ineffective when using the workflow Multibranch Plugin.

      For multbranch builds it's reasonable to expect use cases where the branch info is use to augment artifact names and manifest entries so people can easily distinguish branch built artifacts from one.

      Would be nice if the DSL just made this state available - but populating the env would be a nice start.

          [JENKINS-30252] Provide easy access to git branch name in multibranch workflow scripts

          magnayn added a comment - - edited

          Ah. That explains why my workflow produces nulls in

          node('docker') {
            
            checkout scm;
          
            echo "Build of ${env.GIT_COMMIT} on ${env.GIT_BRANCH}";
          
           // etc etc
          

          Seems like SCM passing environment vars is going to be pretty important?

          magnayn added a comment - - edited Ah. That explains why my workflow produces nulls in node( 'docker' ) { checkout scm; echo "Build of ${env.GIT_COMMIT} on ${env.GIT_BRANCH}" ; // etc etc Seems like SCM passing environment vars is going to be pretty important?

          Jesse Glick added a comment -

          JENKINS-24141 tracks core changes needed to allow the Git plugin to define $GIT_COMMIT and the like. You can work around that particular variable using git rev-parse HEAD of course.

          Jesse Glick added a comment - JENKINS-24141 tracks core changes needed to allow the Git plugin to define $GIT_COMMIT and the like. You can work around that particular variable using git rev-parse HEAD of course.

          Liel Ran added a comment -

          jglick - this workaround is now working for me (even inside SCM block). any other idea ?

          Liel Ran added a comment - jglick - this workaround is now working for me (even inside SCM block). any other idea ?

          Jesse Glick added a comment -

          lielran not working for you, you mean? And what specifically is not working, in what way?

          Jesse Glick added a comment - lielran not working for you, you mean? And what specifically is not working, in what way?

          Code changed in jenkins
          User: Jesse Glick
          Path:
          src/main/java/jenkins/branch/BranchNameContributor.java
          src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly
          src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties
          http://jenkins-ci.org/commit/branch-api-plugin/93e542c28b00cdb17a6251a43e8a61c3a6a8f710
          Log:
          [FIXED JENKINS-30252] Defining environment variable BRANCH_NAME.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: src/main/java/jenkins/branch/BranchNameContributor.java src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties http://jenkins-ci.org/commit/branch-api-plugin/93e542c28b00cdb17a6251a43e8a61c3a6a8f710 Log: [FIXED JENKINS-30252] Defining environment variable BRANCH_NAME.

          Code changed in jenkins
          User: Jesse Glick
          Path:
          src/main/java/jenkins/branch/BranchNameContributor.java
          src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly
          src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties
          http://jenkins-ci.org/commit/branch-api-plugin/4f02e07e44726dc4277e29907c1f8e10ee06da22
          Log:
          Merge pull request #12 from jglick/branch-name-JENKINS-30252

          JENKINS-30252 Defining environment variable BRANCH_NAME

          Compare: https://github.com/jenkinsci/branch-api-plugin/compare/b8f6a22a92fa...4f02e07e4472

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: src/main/java/jenkins/branch/BranchNameContributor.java src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties http://jenkins-ci.org/commit/branch-api-plugin/4f02e07e44726dc4277e29907c1f8e10ee06da22 Log: Merge pull request #12 from jglick/branch-name- JENKINS-30252 JENKINS-30252 Defining environment variable BRANCH_NAME Compare: https://github.com/jenkinsci/branch-api-plugin/compare/b8f6a22a92fa...4f02e07e4472

          Code changed in jenkins
          User: Jesse Glick
          Path:
          multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/BranchJobProperty.java
          multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactory.java
          multibranch/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java
          http://jenkins-ci.org/commit/workflow-plugin/7e91ca4f506e596f2abab4c1d45ca076fd30e2c4
          Log:
          JENKINS-30252 Verifying that ${env.BRANCH_NAME} works.
          Also clarifying that BranchJobProperty.branch is nonnull, always.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/BranchJobProperty.java multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactory.java multibranch/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java http://jenkins-ci.org/commit/workflow-plugin/7e91ca4f506e596f2abab4c1d45ca076fd30e2c4 Log: JENKINS-30252 Verifying that ${env.BRANCH_NAME} works. Also clarifying that BranchJobProperty.branch is nonnull, always.

          Code changed in jenkins
          User: Jesse Glick
          Path:
          multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/BranchJobProperty.java
          multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactory.java
          multibranch/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java
          http://jenkins-ci.org/commit/workflow-plugin/8803a3e713d041cda74031829fa8b13a496642ae
          Log:
          Merge pull request #236 from jglick/branch-name-JENKINS-30252

          JENKINS-30252 Verifying that ${env.BRANCH_NAME} works

          Compare: https://github.com/jenkinsci/workflow-plugin/compare/85553288ab80...8803a3e713d0

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/BranchJobProperty.java multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactory.java multibranch/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java http://jenkins-ci.org/commit/workflow-plugin/8803a3e713d041cda74031829fa8b13a496642ae Log: Merge pull request #236 from jglick/branch-name- JENKINS-30252 JENKINS-30252 Verifying that ${env.BRANCH_NAME} works Compare: https://github.com/jenkinsci/workflow-plugin/compare/85553288ab80...8803a3e713d0

          Marcus Sjölin added a comment - - edited

          This feature seem to have gone into 1.11, however I cannot get this to work in 1.13

          node {
                  // Mark the code checkout 'stage'....
                  stage 'Checkout'
                  // Get some code from a GitHub repository
                  checkout([$class: 'GitSCM', branches: [[name: 'develop'], [name: 'feature/*']], doGenerateSubmoduleConfigurations: false, submoduleCfg: [], userRemoteConfigs: [[credentialsId: '...', url: 'ssh://git@giturl']]])
                  echo "${BRANCH_NAME} ${env.BRANCH_NAME}"
          }
          

          Marcus Sjölin added a comment - - edited This feature seem to have gone into 1.11, however I cannot get this to work in 1.13 node { // Mark the code checkout 'stage' .... stage 'Checkout' // Get some code from a GitHub repository checkout([$class: 'GitSCM' , branches: [[name: 'develop' ], [name: 'feature/*' ]], doGenerateSubmoduleConfigurations: false , submoduleCfg: [], userRemoteConfigs: [[credentialsId: '...' , url: 'ssh: //git@giturl' ]]]) echo "${BRANCH_NAME} ${env.BRANCH_NAME}" }

          Code changed in jenkins
          User: Jesse Glick
          Path:
          multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/BranchJobProperty.java
          multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactory.java
          multibranch/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java
          http://jenkins-ci.org/commit/workflow-multibranch-plugin/05869c106e5dc5aadfab507673b7b888d7ae5584
          Log:
          JENKINS-30252 Verifying that ${env.BRANCH_NAME} works.
          Also clarifying that BranchJobProperty.branch is nonnull, always.

          Originally-Committed-As: 7e91ca4f506e596f2abab4c1d45ca076fd30e2c4

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Jesse Glick Path: multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/BranchJobProperty.java multibranch/src/main/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowBranchProjectFactory.java multibranch/src/test/java/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProjectTest.java http://jenkins-ci.org/commit/workflow-multibranch-plugin/05869c106e5dc5aadfab507673b7b888d7ae5584 Log: JENKINS-30252 Verifying that ${env.BRANCH_NAME} works. Also clarifying that BranchJobProperty.branch is nonnull, always. Originally-Committed-As: 7e91ca4f506e596f2abab4c1d45ca076fd30e2c4

            jglick Jesse Glick
            rpomeroy Ronald Pomeroy
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: