How to get the latest git error message and prevent jenkins from collecting if commit message contains [ci skip]?

I tried to get the git commit message in the jenkinsfile and prevent the build based on the commit message.

env.GIT_COMMIT does not return command details in the jenkins file.

How to get the latest git commit message and prevent jenkins from collecting if the commit message contains [ci skip] in it?

+15
source share
5 answers

Build will take place when [ci skip] is provided in the last git log, but will not run the actual build code (replacement with the first echo statement)

node {
  checkout scm
  result = sh (script: "git log -1 | grep '\\[ci skip\\]'", returnStatus: true) 
  if (result != 0) {
    echo "performing build..."
  } else {
    echo "not running..."
  }
}
+15

. . , .

:

// 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,

+10

, . > > multi branch pipeline job configuration

+5

. extension MessageExclusion 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'
  ]]
])
+3

, changelog when :

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

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

0

Source: https://habr.com/ru/post/1016038/


All Articles