How to receive local Git branch name with Jenkins Git plugin?

前端 未结 12 779
闹比i
闹比i 2020-12-12 17:52

I am using Branch Specifier option of Jenkins Git plugin (v2.0) to run build on specific branch, e.g. 1.4.

${GIT_BRANCH} in this case conta

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 18:32

    Based on this SO answer: How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?

    I used this, and finally was able to get it to work (the below example assumes a branch variable of $Branch):

    stage('Checkout') {
        GIT_BRANCH_LOCAL = sh (
            script: "echo $Branch | sed -e 's|origin/||g'",
            returnStdout: true
        ).trim()
        echo "Git branch: ${GIT_BRANCH_LOCAL}"
        git branch: "${GIT_BRANCH_LOCAL}", credentialsId: '...', url: 'git@github.com:......git'
    }
    

提交回复
热议问题