44

我想访问 git 变量,例如GIT_COMMITGIT_BRANCH我在构建流中进一步从 git 签出存储库时。目前我发现没有可用的变量来访问这两个参数。

node {
    git git+ssh://git.com/myproject.git
    echo "$GIT_COMMIT - $BRANCH_NAME"
}

这些变量是否可用,以防万一,我在哪里可以找到它们。我不介意它们是否可以通过一些 groovy 变量或任何地方获得,只是我可以访问它们。

也许我缺乏 Groovy 中的调试技能,这很容易找到,但我只是用我有限的技能找不到它。

4

8 回答 8

44

根据您使用的 SCM 插件,该checkout步骤可能会返回有关修订的附加信息。这已在JENKINS-26100中解决。它是在2.6 版本的workflow-scm-step插件中发布的。

例如,使用 Git 插件,您可以执行以下操作:

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

这将根据您使用的插件而有所不同,因此原始答案可能对您更有效。


原始答案

使用 2.4 版本的Pipeline Nodes and Processes Plugin,您可以简单地执行以下操作:

def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
于 2016-08-26T20:10:04.347 回答
12

根据您需要的信息,有一个非常简单的解决方案:获取“checkout scm”操作返回:它提供了 GIT_BRANCH、GIT_COMMIT、GIT_PREVIOUS_COMMIT、GIT_PREVIOUS_SUCCESSFUL_COMMIT 和 GIT_URL。

node { 
    stage ("Checkout") {

        scmInfo = checkout scm

        /*...*/
        echo "scm : ${scmInfo}"
        echo "${scmInfo.GIT_COMMIT}"


    }
}

这将输出:

...
[Pipeline] echo
    scm : [GIT_BRANCH:my-branch, GIT_COMMIT:0123456789abcdefabcdef0123456789abcdef01, GIT_PREVIOUS_COMMIT:aaaabbbcccdddeeeefffe0123456789012345678, GIT_PREVIOUS_SUCCESSFUL_COMMIT:aaaabbbcccdddeeeefffe0123456789012345678, GIT_URL:http://my.si.te/my-repository.git]
[Pipeline] echo
    0123456789abcdefabcdef0123456789abcdef01
...

此处的更多详细信息Jenkins Pipeline SCM 步骤

于 2017-10-11T08:55:26.010 回答
10

这就是我正在做的事情,基于Jenkins 示例 repo 中提供的示例

node {
    git url: 'https://git.com/myproject.git'

    sh 'git rev-parse --abbrev-ref HEAD > GIT_BRANCH'
    git_branch = readFile('GIT_BRANCH').trim()
    echo git_branch

    sh 'git rev-parse HEAD > GIT_COMMIT'
    git_commit = readFile('GIT_COMMIT').trim()
    echo git_commit
}

编辑你可以通过

git_commit = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
于 2016-04-12T21:10:00.403 回答
1

这个例子可能会让你更进一步: https ://github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/gitcommit

在此示例中,他们将 git 命令的输出通过管道传输到文件,然后读取该文件。

于 2016-02-29T22:04:21.977 回答
1

您可以在内部定义您的作业(从上次提交中提取 git 信息)node以在队列中执行。

node {

  //Code checkout from SCM (here - `git`)
  checkout scm

  stage("GIT INFO"){
    echo ":::::::::::GIT_SHORT_COMMIT::::::::::::::::::::::::"

    GIT_SHORT_COMMIT = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
    //echo in jenkins console
    echo GIT_SHORT_COMMIT
    //wanted to send these info to build artifacts, append to any file
    sh("echo ${GIT_SHORT_COMMIT} > GIT_SHORT_COMMIT")

    //Similar proceed for other git info's 
    echo ":::::::::::GIT_COMMITTER_EMAIL::::::::::::::::::::::::"

    GIT_COMMITTER_EMAIL = sh(returnStdout: true, script: "git show -s --pretty=%ae").trim()
    sh("echo ${GIT_COMMITTER_EMAIL} > GIT_COMMITTER_EMAIL-${GIT_COMMITTER_EMAIL}")



    echo ":::::::::::GIT_COMMITTER_NAME::::::::::::::::::::::::"

    GIT_COMMITTER_NAME = sh(returnStdout: true, script: "git show -s --pretty=%an").trim()
    sh("echo ${GIT_COMMITTER_NAME} > GIT_COMMITTER_NAME-${GIT_COMMITTER_NAME}")
  }

工作完成后,您将在工作区中看到上述任务中的三个附加文件:

. |-- GIT_COMMITTER_EMAIL_email@gmail.com |-- GIT_COMMITTER_NAME-username |-- GIT_SHORT_COMMIT_<commit-short-ID-ef9e91c>

于 2017-10-03T13:17:48.230 回答
0

通过Jenkinsfile在Jenkins中获取Git变量的最简单方法

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}'"
}

在控制台中,您将获得

GIT_COMMIT:
GIT_BRANCH:
GIT_PREVIOUS_COMMIT:
commitEmail:
commitName:
于 2018-09-06T04:17:57.827 回答
0

附加信息:如果您使用代理,例如管道中的 docker,代理应该在容器内安装 git。如果没有 git,它不会添加像“GIT_COMMIT、GIT_BRANCH、GIT_COMMITTER_EMAIL ... 等”这样的变量

如果容器内没有安装 git,您可以使用 Jenkins git 插件进行检查并与 git 变量交互,如下所示:

stage ('Clone') {
  steps {
    script {
      def scmVars = checkout BbS(branches: [[name: '*/develop']], credentialsId: 'bitbucket-jenkins', extensions: [], gitTool: 'default-git', id: '436fd534-c616-4a81-94d9-f67f32c03c1c', projectName: 'intuence-discovery-analysis-configuration', repositoryName: 'api-gateway-elasticsearch-proxy', serverId: 'b4943a46-b885-4aa3-90be-033e40460794')
      echo "scmVars.GIT_COMMIT"
      echo "${scmVars.GIT_COMMIT}"
      env.GIT_COMMIT = scmVars.GIT_COMMIT
      sh "printenv"
    }
  }
}
于 2021-03-16T12:00:37.417 回答
0

我现在解决这个问题的好方法是使用多分支管道,知道 Bitbucket 和 Github 在 Jenkins 中有可以建立组织并自动发现新项目的插件可能会很好。这可以在原生环境下使用 env.GIT_BRANCH

对于提交 ID,我会建议 @mkobit 上面写的内容。

于 2016-11-17T08:47:19.507 回答