Skip to content

Instantly share code, notes, and snippets.

@JD10NN3
Created November 11, 2019 13:54
Show Gist options
  • Save JD10NN3/5de45a5c9b75c25c81da107acdda34c8 to your computer and use it in GitHub Desktop.
Save JD10NN3/5de45a5c9b75c25c81da107acdda34c8 to your computer and use it in GitHub Desktop.
Example of a External Jenkins Pipeline triggers configuration
/*
This is used as part of a dynamic Jenkins pipeline configuration.
You can reference to this Gist for more details:
https://gist.github.com/JD10NN3/7796639e4d427d474c172d5e6b0fa49a
*/
// If you need some libraries, feel free to import them here.
// import groovy.json.JsonOutput
this.triggersMapping = [
// Contains all the triggers for the Master branch
"master" : [
// This is executed when a new commit is done
// Example of usage:
// - Send an email to notify the Team about the new content added to the master branch.
// - Automatic rebase of the integration branch on master.
"on_commit" : {
// Note: You have access to global variable defined in the jenkinsfile
// and other groovy script evaluated previously.
// eg.: GITHUB_ACCESS_TOKEN, currentBuild, env.BRANCH_NAME, etc...
print "Entered trigger -> on_commit (on master)"
}
],
"integration" : [
// Example of usage:
// - Notify the committer about the integration of his changes.
// - Update the GitHub Pull Request description used to merge from integration
// to master with a checklist for every commits to test.
"on_commit" : {
print "Entered trigger -> on_commit (on integration)"
}
],
]
/**
* The default/fallback configuration
* Those triggers are called only if there is not existing triggers
* mapping for a specific branch.
*/
this.defaultTriggersMapping = [
"on_commit" : {
print "Entered default trigger -> on_commit"
},
"on_approval" : { target ->
// Example of usage:
// - Send a MS Teams notification on the CICD Channel to notify about a waiting deployment approval
// and use the target argument to provide a better context within the notification content.
// to master with a checklist for every commits to test.
print "Entered default trigger -> on_approval"
print "With argument -> ${target}"
}
]
/**
* Call the specified trigger for a specific branch
* @param trigger name of the targeted trigger
* @param branch name of the targeted branch
* @return default or specific mapping based on branch name provided
*/
def callTriggerOnBranch(trigger, branch) {
return this.callTriggerOnBranchWithArgs(trigger, branch, null)
}
/**
* Call the specified trigger for a specific branch with arguments
* @param trigger name of the targeted trigger
* @param branch name of the targeted branch
* @param args arguments to pass to a trigger
* @return default or specific mapping based on branch name provided
*/
def callTriggerOnBranchWithArgs(trigger, branch, args) {
triggers = this.triggersMapping.getOrDefault(branch, defaultTriggersMapping)
return triggers.getOrDefault(trigger, this.defaultTriggersMapping[trigger]).call(args)
}
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment