Git Variables in Jenkins Workflow plugin

前端 未结 7 1525
故里飘歌
故里飘歌 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 04:52

    The simplest way to fetch the Git variable in Jenkins through Jenkinsfile

    node {
      def scmVars = checkout scm
      echo 'scm : the commit id is ' +scmVars.GIT_COMMIT
      echo 'scm : the commit branch  is ' +scmVars.GIT_BRANCH
      echo 'scm : the previous commit id is ' +scmVars.GIT_PREVIOUS_COMMIT
      def commitEmail = sh(returnStdout: true, script: "git --no-pager show -sformat=\'%ae\'")
      echo " the commiter email is'${commitEmail}'"
      def commitName = sh(returnStdout: true, script: "git --no-pager show -s format=\'%an\'")
      echo " the commiter name is'${commitName}'"
    }
    

    In console you will get the

    GIT_COMMIT:
    GIT_BRANCH:
    GIT_PREVIOUS_COMMIT:
    commitEmail:
    commitName:
    

提交回复
热议问题