如何让 Jenkins 从 Jenkinsfile 中的命令生成的控制台输出中提取值?
具体来说,如何让 Jenkins 从
checkout scm
以下非常简单的 Jenkinsfile中的命令结果中提取提交哈希?
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
checkout scm
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
checkout scm
命令的输出:
作为checkout scm
在上述简化的 Jenkinsfile中运行命令的结果,Jenkins 日志打印以下内容:
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
> git init /var/jenkins_home/workspace/le-repo_sample-issue-branch-2WOFDGRDQWR367VAM7O26H2DKPTRVPDKRTGNRIS4AQNNFP7QIX2Q # timeout=10
Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch
> git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch # timeout=10
> git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
Fetching without tags
Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
using GIT_ASKPASS to set credentials
> git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch
Checking out Revision 77cf12f42136efc77fecbcd1f761a54254278cb3 (sample-issue-branch)
> git config core.sparsecheckout # timeout=10
> git checkout -f 77cf12f42136efc77fecbcd1f761a54254278cb3
Commit message: "add whitespace"
> git rev-list --no-walk e975fb4391677bc09f2056b3e8a6be62eda0b222 # timeout=10
[Bitbucket] Notifying commit build result
具体来说,我们在 Jenkinsfile 中添加了什么,以便日志在最后额外打印出以下内容:
Commit hash is: 77cf12f42136efc77fecbcd1f761a54254278cb3
这显然过于简化了。现实生活中的打印输出将进入一个变量并作为参数传递到脚本中。但是对于这个 OP,什么特定的语法可以让 Jenkins 提取提交哈希?
如果您特别需要提交哈希,结帐步骤的文档提到了一种简单的方法来获取它(而不是解析控制台输出等):
此步骤返回 SCM 插件将在 Freestyle 作业中设置的任何变量的 Map,因此如果您的 SCM 是 git,您可以执行以下操作:
def scmVars = checkout scm def commitHash = scmVars.GIT_COMMIT // or def commitHash = checkout(scm).GIT_COMMIT
一旦你把它放在一个变量中,它只是回显它以在日志中看到它的问题。
echo "Commit hash is: ${commitHash}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句