如果提交消息包含 [ci skip],如何获取 git 最新提交消息并阻止 jenkins 构建? [英] How to get the git latest commit message and prevent the jenkins build if the commit message contains [ci skip]?
问题描述
我尝试在 jenkinsfile 中获取 git commit 消息并阻止基于提交消息的构建.
I tried to get the git commit message in jenkinsfile and prevent the build based on commit message.
env.GIT_COMMIT 不会在 jenkinsfile 中返回提交详细信息.
env.GIT_COMMIT doesn't return the commit details in jenkinsfile.
如果提交消息中包含 [ci skip],如何获取 git 最新提交消息并阻止 jenkins 构建?
How to get the git latest commit message and prevent the jenkins build if the commit message contains [ci skip] in it?
推荐答案
我遇到了同样的问题.我正在使用管道.我通过实施一个共享库解决了这个问题.
I had the same issue. I'm using pipelines. I solved this issue by implementing a shared library.
库的代码是这样的:
// vars/ciSkip.groovy
def call(Map args) {
if (args.action == 'check') {
return check()
}
if (args.action == 'postProcess') {
return postProcess()
}
error 'ciSkip has been called without valid arguments'
}
def check() {
env.CI_SKIP = "false"
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true)
if (result == 0) {
env.CI_SKIP = "true"
error "'[ci skip]' found in git commit message. Aborting."
}
}
def postProcess() {
if (env.CI_SKIP == "true") {
currentBuild.result = 'NOT_BUILT'
}
}
然后,在我的 Jenkinsfile 中:
Then, in my Jenkinsfile:
pipeline {
stages {
stage('prepare') { steps { ciSkip action: 'check' } }
// other stages here ...
}
post { always { ciSkip action: 'postProcess' } }
}
如您所见,构建被标记为 NOT_BUILT
.如果您愿意,可以将其更改为 ABORTED
,但不能将其设置为 SUCCESS
,因为 构建结果只会变得更糟
As you can see, the build is marked as NOT_BUILT
. You can change it to ABORTED
if you prefer, but it cannot be set to SUCCESS
because a build result can only get worse
这篇关于如果提交消息包含 [ci skip],如何获取 git 最新提交消息并阻止 jenkins 构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!