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

Provide easy access to git branch name in multibranch workflow scripts

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      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.

        Attachments

          Issue Links

            Activity

            Hide
            jglick Jesse Glick added a comment -

            Depends on RFE to let SCM pass environment variables to a Run.

            Workaround: env.JOB_NAME

            Show
            jglick Jesse Glick added a comment - Depends on RFE to let SCM pass environment variables to a Run . Workaround: env.JOB_NAME
            Hide
            rpomeroy Ronald Pomeroy added a comment - - edited

            env.JOB_NAME (I assume) is static. If new branches are indexed (in the multibranch plugin case) how can the JOB_NAME contain the git branch name?

            Am I missing something?

            Regards

            Ron

            Show
            rpomeroy Ronald Pomeroy added a comment - - edited env.JOB_NAME (I assume) is static. If new branches are indexed (in the multibranch plugin case) how can the JOB_NAME contain the git branch name? Am I missing something? Regards Ron
            Hide
            jglick Jesse Glick added a comment -

            Yes you are missing something. For example, in the stock CD demo (1.10), adding

            echo "running from ${env.JOB_NAME.replaceFirst('.+/', '')}"
            

            works (after approving method java.lang.String replaceFirst java.lang.String java.lang.String for the sandbox).

            Show
            jglick Jesse Glick added a comment - Yes you are missing something. For example, in the stock CD demo (1.10), adding echo "running from ${env.JOB_NAME.replaceFirst( '.+/' , '')}" works (after approving method java.lang.String replaceFirst java.lang.String java.lang.String for the sandbox).
            Hide
            magnayn 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?

            Show
            magnayn 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?
            Hide
            jglick 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.

            Show
            jglick 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.
            Hide
            lielran Liel Ran added a comment -

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

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

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

            Show
            jglick Jesse Glick added a comment - Liel Ran not working for you, you mean? And what specifically is not working, in what way?
            Hide
            scm_issue_link 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.

            Show
            scm_issue_link 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.
            Hide
            scm_issue_link 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

            Show
            scm_issue_link 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
            Hide
            scm_issue_link 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.

            Show
            scm_issue_link 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.
            Hide
            scm_issue_link 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

            Show
            scm_issue_link 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
            Hide
            marcuss 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}"
            }
            
            Show
            marcuss 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}" }
            Hide
            scm_issue_link 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

            Show
            scm_issue_link 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

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: