Skip to main content
Background Image
  1. Posts/

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

·298 words·1 min· ·
Xianpeng Shen
Author
Xianpeng Shen

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.
Jenkins Linux agent configuration
·298 words·2 mins
Provides a step-by-step guide on how to configure a Jenkins Linux agent, including setting up the Java runtime, creating the node, and troubleshooting common issues.
Jenkins Windows agent configuration
·544 words·3 mins
Provides a step-by-step guide on how to configure a Jenkins Windows agent, including setting up the Java runtime, creating the node, and troubleshooting common issues.
How to enable, configure and disable Jenkins LDAP
·183 words·1 min
This article explains how to enable and configure LDAP authentication in Jenkins, including how to disable it temporarily if needed.
How to make Jenkins job fail after timeout? (Resolved)
·208 words·1 min
This article explains how to handle Jenkins job timeouts effectively by using try and catch blocks to ensure the job fails correctly when a timeout occurs.
Resolved problem that ESlint HTML report is not displayed correctly in Jenkins job
·159 words·1 min
This article explains how to resolve the issue of ESlint HTML report not displaying correctly in Jenkins jobs due to Content Security Policy restrictions, including the steps to configure Jenkins to allow the report to load properly.