Skip to content

Instantly share code, notes, and snippets.

@rufoa
Created May 13, 2019 02:29
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rufoa/2807ad19328f70dc81fec25c317661b8 to your computer and use it in GitHub Desktop.
Save rufoa/2807ad19328f70dc81fec25c317661b8 to your computer and use it in GitHub Desktop.
Jenkins [skip ci] implementation for multi-branch declarative pipeline
// change 'agent' lines as appropriate
pipeline {
agent none
stages {
stage('Run CI?') {
agent any
steps {
script {
if (sh(script: "git log -1 --pretty=%B | fgrep -ie '[skip ci]' -e '[ci skip]'", returnStatus: true) == 0) {
currentBuild.result = 'NOT_BUILT'
error 'Aborting because commit message contains [skip ci]'
}
}
}
}
stage('next stage') {
agent { dockerfile true }
steps {
echo 'next stage here...'
}
}
}
}
@tylerlwsmith
Copy link

You're a hero 🦸

@rufoa
Copy link
Author

rufoa commented May 28, 2023

@tylerlwsmith 😄 IIRC this has difficulty with squashed commits and/or pushing several commits at once, some of which have [skip ci]. So watch out for that!

@tylerlwsmith
Copy link

Good to know, thank you 😁

@moertel
Copy link

moertel commented Jul 17, 2023

I just stumbled across this, and if anyone's wondering about addressing multiple pushed commits, this SO answer is a fantastic resource for traversing all commit messages instead of just the first:
https://stackoverflow.com/a/60390768/3405140

def getChangesSinceLastSuccessfulBuild() {
    def changes = []
    def build = currentBuild

    while (build != null && build.result != 'SUCCESS') {
        changes += (build.changeSets.collect { changeSet ->
            (changeSet.items.collect { item ->
                (item.affectedFiles.collect { affectedFile ->
                    affectedFile.path
                }).flatten()
            }).flatten()
        }).flatten()

        build = build.previousBuild
    }

    return changes.unique()
}

@rufoa
Copy link
Author

rufoa commented Jul 17, 2023

Thank you for that @moertel!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment