Git Variables in Jenkins Workflow plugin

前端 未结 7 1526
故里飘歌
故里飘歌 2020-12-08 04:17

I\'d like to access git variables such as GIT_COMMIT and GIT_BRANCH when I have checked out a repository from git further down in the build stream.

7条回答
  •  情歌与酒
    2020-12-08 05:01

    Depending on the SCM plugin you are using, the checkout step may return additional information about the revision. This was resolved in JENKINS-26100. It was released in the 2.6 version of the workflow-scm-step plugin.

    For example, using the Git plugin, you can do something like:

    final scmVars = checkout(scm)
    echo "scmVars: ${scmVars}"
    echo "scmVars.GIT_COMMIT: ${scmVars.GIT_COMMIT}"
    echo "scmVars.GIT_BRANCH: ${scmVars.GIT_BRANCH}"
    

    This will vary depending on the plugin you use, so the original answer may work better for you.


    Original Answer

    With the 2.4 release of the Pipeline Nodes and Processes Plugin, you can simply do:

    def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
    

提交回复
热议问题