How to access git branch name from pipeline job?

后端 未结 3 1495
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-20 18:32

I have a Jenkins Pipeline job which is configured to checkout a git repo and a specific local branch.

How can i get the name of the local branch in my Jenkinsfile?<

3条回答
  •  春和景丽
    2021-02-20 18:58

    I found that I can capture the return value from checkout scm and use that to get the branch name (and other values)

      def scmVars
    
      node('api-sample-build') {
        stage('Clone source code') {
            scmVars = checkout scm
            // scmVars contains the following values
            // GIT_BRANCH=origin/mybranch
            // GIT_COMMIT=fc8279a107ebaf806f2e310fce15a7a54238eb71
            // GIT_PREVIOUS_COMMIT=6f2e319a1fc82707ebaf800fce15a7a54238eb71
            // GIT_PREVIOUS_SUCCESSFUL_COMMIT=310fce159a1fc82707ebaf806f2ea7a54238eb71
            // GIT_URL=https://stash.someworkplace.com/scm/poc/api-sample.git
        }
        stage('test scope') {
          echo scmVars.GIT_BRANCH
        }
      }
    

    By defining the variable outside the node it is available in stages after the checkout.

提交回复
热议问题