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

Yah*_*Raj 19 jenkins jenkins-workflow jenkins-pipeline multibranch-pipeline

我试图在jenkinsfile中获取git commit消息并阻止基于提交消息的构建.

env.GIT_COMMIT不返回jenkinsfile中的提交详细信息.

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

Ami*_*tyo 16

当在最后一个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..."
  }
}
Run Code Online (Sandbox Code Playgroud)


csa*_*zar 13

我遇到过同样的问题.我正在使用管道.我通过实现共享库解决了这个问题.

该库的代码是这样的:

// 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'
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在我的Jenkinsfile中:

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

如您所见,构建标记为NOT_BUILT.ABORTED如果您愿意,可以将其更改为,但无法设置,SUCCESS因为构建结果只会变得更糟


hak*_*iri 5

我认为您可以轻松地在多分支管道作业配置中执行此操作分支源>其他行为>轮询忽略对某些消息的提交 多分支管道作业配置

  • @hakamairi 这个选项是否可用于 multibranch 因为我找不到它? (2认同)

man*_*ana 5

到今天为止,这很容易实现。有趣的一行是extension命名MessageExclusionwhereexcludedMessage接受正则表达式。

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'
  ]]
])
Run Code Online (Sandbox Code Playgroud)

  • 那些是内置类吗? (2认同)

小智 5

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

when {
    not {
    changelog '.*^\\[ci skip\\] .+$'
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 有什么方法可以将其限制为最新的提交消息吗?对此进行了测试,如果最近的提交消息不匹配,但之前的提交仍然会跳过。 (2认同)