Skip to main content
  1. Posts/

How to implement [skip ci] for Jenkins multi-branch pipeline

·298 words·1 min· ·
Xianpeng Shen
Author
Xianpeng Shen
DevOps & Build Engineer | Python Enthusiast | Open Source Maintainer

When I want to implement [skip ci] or [ci skip] for Jenkins multi-branch pipeline, the existing plugin seems broken.

My advice: try not to use the Jenkins plugin if possible.

Good, it’s time to implement [skip ci] myself.

If you like me used Jenkins shared library, you can create a function like SkipCI from src/org/cicd/utils.groovy, then other jobs can reused this function.

// src/org/cicd/utils.groovy
def SkipCI(number = "all"){
  def statusCodeList = []

  String[] keyWords = ['ci skip', 'skip ci'] // add more keywords if need.
  keyWords.each { keyWord ->
    def statusCode = null
    if (number == "all") {
      statusCode = sh script: "git log --oneline --all | grep \'${keyWord}\'", returnStatus: true
    } else {
      statusCode = sh script: "git log --oneline -n ${number} | grep \'${keyWord}\'", returnStatus: true
    }
    statusCodeList.add(statusCode)
  }

  if (statusCodeList.contains(0)) {
    return true
  } else {
    return false
  }
}

Then I can call this function from other jobs.

// The following is not the complete code, it is just sample code and may not be run successfully.

import org.cicd.utils

def call(){

  pipeline {
    agent {
      node {
        label 'linux'
      }
    }

    parameters {
      booleanParam defaultValue: true, name: 'Build', summary: 'Uncheck to skip build.'
    }

    def utils = new org.cicd.utils()

    stage("Checkout") {
      checkout scm

      // just check the latest commit message.
      SkipCI = utils.SkipCI('1')
    }

    stage("Build"){
      when {
        beforeAgent true
        expression { return params.Build && !SkipCI }
      }

      steps {
        script {
          sh "make build"
        }
      }
    }
  }
}

Please let me know if any questions or suggestions.


转载本站文章请注明作者和出处,请勿用于任何商业用途。欢迎关注公众号「DevOps攻城狮」

Related

Jenkins Multibranch Pipeline

·405 words·2 mins
Discusses the use of Jenkins Multibranch Pipeline to manage multiple branches in a project, enabling parallel builds for pull requests and efficient code review processes.