Jenkins Workflow 插件中的 Git 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35554983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git Variables in Jenkins Workflow plugin
提问by Oldek
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. Currently I find no available variable to access these two parameters.
我想访问的git变量,如GIT_COMMIT
和GIT_BRANCH
我已经从构建流中的git进一步下跌签出库。目前我发现没有可用的变量来访问这两个参数。
node {
git git+ssh://git.com/myproject.git
echo "$GIT_COMMIT - $BRANCH_NAME"
}
Is such variables available and in case, where would I find them. I don't mind if they are available through some groovy variables or wherever, just that I can access them.
这些变量是否可用,以防万一,我在哪里可以找到它们。我不介意它们是否可以通过一些常规变量或任何地方获得,只要我可以访问它们。
Maybe I lack the debugging skills in Groovy and this is easy to find, but I just can't find it with my limited skills.
也许我缺乏 Groovy 中的调试技巧,这很容易找到,但我只是以我有限的技巧找不到它。
采纳答案by Oldek
I good way to fix this for now is to use the Multi-branch pipeline, and it might be good to know that Bitbucket and Github have plugins in Jenkins that sets up an organisation and autodiscovers new projects. This works natively then with env.GIT_BRANCH
我现在解决这个问题的好方法是使用多分支管道,知道 Bitbucket 和 Github 在 Jenkins 中有插件,可以建立一个组织并自动发现新项目可能会很好。这适用于 env.GIT_BRANCH
For commit ID I would suggest what @mkobit wrote above.
对于提交 ID,我会建议@mkobit 上面写的内容。
回答by mkobit
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.
根据您使用的 SCM 插件,该checkout
步骤可能会返回有关修订的其他信息。这已在JENKINS-26100 中解决。它是在插件的2.6 版本中workflow-scm-step
发布的。
For example, using the Git plugin, you can do something like:
例如,使用 Git 插件,您可以执行以下操作:
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:
随着 2.4 版本的Pipeline Nodes and Processes Plugin,您可以简单地执行以下操作:
def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
回答by Jonathan Vanderick
Depending on the information you need, there is a very straightforward solution: get the "checkout scm" operation return: it provides GIT_BRANCH, GIT_COMMIT, GIT_PREVIOUS_COMMIT, GIT_PREVIOUS_SUCCESSFUL_COMMIT and GIT_URL.
根据您需要的信息,有一个非常简单的解决方案:获取“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}"
}
}
This will output:
这将输出:
...
[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
...
More details here Jenkins Pipeline SCM Steps
此处有更多详细信息Jenkins 管道 SCM 步骤
回答by Kevin London
This is what I'm doing, based on the example provided in the Jenkins examples repo:
这就是我正在做的,基于Jenkins 示例存储库中提供的示例:
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
}
Edityou can do this shorter via
编辑你可以通过
git_commit = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
回答by daemonsl
You can define your jobs (extracting git info from last commit) inside node
for execution in a queue.
您可以在内部定义您的作业(从上次提交中提取 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}")
}
After your job is finished, you will see three additional file from above task in your workspace :
工作完成后,您将在工作区中看到上述任务中的三个附加文件:
.
|-- [email protected]
|-- GIT_COMMITTER_NAME-username
|-- GIT_SHORT_COMMIT_<commit-short-ID-ef9e91c>
.
|-- [email protected]
|-- GIT_COMMITTER_NAME-username
|-- GIT_SHORT_COMMIT_<commit-short-ID-ef9e91c>
回答by BritishKnight
This example might get you further: https://github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/gitcommit
这个例子可能会让你更进一步:https: //github.com/jenkinsci/pipeline-examples/tree/master/pipeline-examples/gitcommit
In this example they are piping the output of git commands to a file, and then reading the file.
在这个例子中,他们将 git 命令的输出通过管道传输到一个文件,然后读取该文件。
回答by pix data
The simplest way to fetch the Git variable in Jenkins through Jenkinsfile
通过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}'"
}
In console you will get the
在控制台中,您将获得
GIT_COMMIT:
GIT_BRANCH:
GIT_PREVIOUS_COMMIT:
commitEmail:
commitName: