我尝试在 Jenkinsfile 中获取 git 提交信息并根据提交信息阻止构建。
env.GIT_COMMIT 在 Jenkinsfile 中不会返回提交的详细信息。
如何获取 git 的最新提交信息,并在提交信息中包含 [ci skip] 时阻止 Jenkins 构建?
我尝试在 Jenkinsfile 中获取 git 提交信息并根据提交信息阻止构建。
env.GIT_COMMIT 在 Jenkinsfile 中不会返回提交的详细信息。
如何获取 git 的最新提交信息,并在提交信息中包含 [ci skip] 时阻止 Jenkins 构建?
我遇到了同样的问题。我正在使用管道。我通过实现共享库来解决这个问题。
该库的代码如下:
// 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中:
pipeline {
stages {
stage('prepare') { steps { ciSkip action: 'check' } }
// other stages here ...
}
post { always { ciSkip action: 'postProcess' } }
}
正如您所看到的,构建被标记为NOT_BUILT
。如果您愿意,可以将其更改为ABORTED
,但它不能设置为SUCCESS
,因为构建结果只能变得更糟
如果在最后一个git日志中提供[ci skip],则构建将通过,但不会运行实际的构建代码(即第一个echo语句的替代品)。
node {
checkout scm
result = sh (script: "git log -1 | grep '\\[ci skip\\]'", returnStatus: true)
if (result != 0) {
echo "performing build..."
} else {
echo "not running..."
}
}
sh
的更多信息,请参见:https://issues.jenkins-ci.org/browse/JENKINS-26133 - Mike Rylander对于声明式流水线,可以在“when”指令中使用“changelog”来跳过一个阶段:
when {
not {
changelog '.*^\\[ci skip\\] .+$'
}
}
MessageExclusion
的extension
,其中excludedMessage
接受正则表达式。checkout([ $class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[
$class: 'MessageExclusion', excludedMessage: '.*skip-?ci.*'
]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: 'xxx', url: 'git@github.com:$ORG/$REPO.git'
]]
])