CodeMed
CodeMed

Reputation: 9191

Isolate the name of a repository branch in Jenkinsfile

What specific changes need to be made to the Jenkinsfile below in order to isolate the name of the branch of the commit into a variable that can be printed out in the logs? In the case below, the name of the branch is GWS-43-getissueforcommit.

Here are the specifics:


Jenkinsfile:

The following Jenkinsfile prints out the branch name in the midst of the output produced by running the checkout(scm).GIT_ASKPASS command, but this information is lost when the subsequent line of code ( sh "echo 'The repo Ask Pass is: ${repoAskPass}'" ) tries to print out the encapsulated result of the command:

node {
    // Clean workspace before doing anything
    deleteDir()
    try {
        stage ('Clone') {
            def repoAskPass = checkout(scm).GIT_ASKPASS
            sh "echo 'The repo Ask Pass is: ${repoAskPass}'"
        }
    } catch (err) {
        currentBuild.result = 'FAILED'
        throw err
    }
}


Resulting Logs:

The resulting log output is:

General SCM<1s
    Cloning the remote Git repository
    Cloning with configured refspecs honoured and without tags
    Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git init /var/jenkins_home/workspace/ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA # timeout=10
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
     > git --version # timeout=10
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
     > git config --add remote.origin.fetch +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit # timeout=10
     > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    No valid HEAD. Skipping the resetting
     > git clean -fdx # timeout=10
    Fetching without tags
    Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
    using GIT_ASKPASS to set credentials Bitbucket server credentials
     > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
    Checking out Revision 375b17c4e7453d802d94659836db436553cc7f0c (GWS-43-getissueforcommit)
     > git config core.sparsecheckout # timeout=10
     > git checkout -f 375b17c4e7453d802d94659836db436553cc7f0c
     > git branch -a -v --no-abbrev # timeout=10
     > git checkout -b GWS-43-getissueforcommit 375b17c4e7453d802d94659836db436553cc7f0c
    Commit message: "isolate ASKPASS"
     > git rev-list --no-walk 268c468a96de0fb27b6f205658a169b38871b581 # timeout=10
    Cleaning workspace
     > git rev-parse --verify HEAD # timeout=10
    Resetting working tree
     > git reset --hard # timeout=10
     > git clean -fdx # timeout=10
    [Bitbucket] Notifying commit build result

echo 'The repo Ask Pass is: null'— Shell Script<1s
    [ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
    + echo The repo Ask Pass is: null
    The repo Ask Pass is: null


Restated Question:

How does the above Jenkinsfile need to be modified in order to output the following line in the logs:

The name of the branch containing the commit for this build is: 
GWS-43-getissueforcommit

Upvotes: 1

Views: 1422

Answers (2)

amuniz
amuniz

Reputation: 3342

Given that you mentioned Jenkinsfile I guess you are using a Multibranch project. If so, the branch name is always available as an environment variable: env.BRANCH

Upvotes: 0

Vasan
Vasan

Reputation: 4956

GIT_ASKPASS is used for getting user credentials - it is not relevant to the branch.

Here are the list of variables the plugin sets, which you can use. From here you can see that GIT_BRANCH gives you the remote branch and GIT_LOCAL_BRANCH gives you the local branch checked out.

def branch = checkout(scm).GIT_BRANCH
sh "echo 'The name of the branch containing the commit for this build is: ${branch}'"

This prints the remote branch name along with origin prefix (which is usually desirable). But if you want it without the prefix:

def branch = checkout(scm).GIT_BRANCH
branch = branch.substring(branch.indexOf('/') + 1)
sh "echo 'The name of the branch containing the commit for this build is: ${branch}'"

Upvotes: 4

Related Questions