如果提交消息包含[ci skip],如何获取git最新提交消息并阻止jenkins构建?

时间:2016-12-14 10:28:16

标签: jenkins jenkins-pipeline jenkins-workflow multibranch-pipeline

我试图在jenkinsfile中获取git commit消息,并根据提交消息阻止构建。

env.GIT_COMMIT 不会在jenkinsfile中返回提交详细信息。

如果提交消息中包含[ci skip],如何获取git最新提交消息并阻止jenkins构建?

5 个答案:

答案 0 :(得分:15)

当在最后一个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..."
  }
}

答案 1 :(得分:10)

我有同样的问题。我正在使用管道。我通过实施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'
    }
}

然后,在我的Jenkins文件中:

pipeline {
  stages {
    stage('prepare') { steps { ciSkip action: 'check' } }
    // other stages here ...
  }
  post { always { ciSkip action: 'postProcess' } }
}

如您所见,构建标记为NOT_BUILT。如果您愿意,可以将其更改为ABORTED,但不能将其设置为SUCCESS,因为a build result can only get worse

答案 2 :(得分:5)

我认为您可以轻松地在多分支管道作业配置中执行此操作 分支来源>附加行为>轮询忽略对某些消息的提交 multi branch pipeline job configuration

答案 3 :(得分:2)

截至今天,它很容易实现。有趣的一行是名为extension的{​​{1}},其中MessageExclusion接受正则表达式。

excludedMessage

答案 4 :(得分:2)

对于声明式管道,可以在'when'指令中使用'changelog'来跳过阶段:

when {
    not {
    changelog '.*^\\[ci skip\\] .+$'
    }
}

请参阅:https://jenkins.io/doc/book/pipeline/syntax/#when