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

          Ronald Pomeroy created issue -

          Jesse Glick added a comment -

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

          Workaround: env.JOB_NAME

          Jesse Glick added a comment - Depends on RFE to let SCM pass environment variables to a Run . Workaround: env.JOB_NAME
          Ronald Pomeroy made changes -
          Comment [ I tried to log out the env.JOB_NAME

          with echo " *** ${env.JOB_NAME} *** "

          and got
          ...
          Running: Print Message
          *** ${env.JOB_NAME} ***
          ... ]

          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

          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

          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).

          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).
          Jesse Glick made changes -
          Labels New: multibranch
          Jesse Glick made changes -
          Remote Link New: This issue links to "Stack Overflow (Web Link)" [ 13165 ]
          Jesse Glick made changes -
          Link New: This issue is blocking JENKINS-30744 [ JENKINS-30744 ]

          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.
          Jesse Glick made changes -
          Link New: This issue is related to JENKINS-24141 [ JENKINS-24141 ]

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

              Created:
              Updated:
              Resolved: