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..."
  }
}
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'
    }
}
然后,在我的Jenkinsfile中:
pipeline {
  stages {
    stage('prepare') { steps { ciSkip action: 'check' } }
    // other stages here ...
  }
  post { always { ciSkip action: 'postProcess' } }
}
如您所见,构建标记为NOT_BUILT.ABORTED如果您愿意,可以将其更改为,但无法设置,SUCCESS因为构建结果只会变得更糟
到今天为止,这很容易实现。有趣的一行是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'
  ]]
])
小智 5
对于声明性管道,可以在“ when”指令中使用“ changelog”来跳过阶段:
when {
    not {
    changelog '.*^\\[ci skip\\] .+$'
    }
}
参见:https : //jenkins.io/doc/book/pipeline/syntax/#when
| 归档时间: | 
 | 
| 查看次数: | 26767 次 | 
| 最近记录: |