Skip to content

Instantly share code, notes, and snippets.

@smalltown
Last active October 10, 2019 13:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smalltown/58246107e176461eb88df644e90d91c4 to your computer and use it in GitHub Desktop.
Save smalltown/58246107e176461eb88df644e90d91c4 to your computer and use it in GitHub Desktop.
//
// This Groovy snippet is used for Jenkins Job DSL groovy script,
// when ...
// 1. enable Jenkins Global Security
// 2. use pipelineJob
// 3. want to approve pipeline cps automatically
//
import jenkins.model.Jenkins
//
// Here is the Job DSL
//
pipelineJob("${The Pipeline Job Name}") {
definition {
cps {
script(readFileFromWorkspace("${Jenkins Pipeline Script Path}"))
}
}
}
//
// Add below Groovy snippet into Job DSL groovy script,
// then the Jenkins pipeline script can be approved automaitcally
// P.S. don't forgot to customize ${Jenkins Pipeline Script Path}
//
def groovyscript = readFileFromWorkspace("${Jenkins Pipeline Script Path}")
def scriptApproval = Jenkins.instance.getExtensionList('org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval')[0]
scriptApproval.approveScript(scriptApproval.hash(groovyscript, 'groovy'))
//
// If your pipeline script include method which not allow by Jenkins,
// Add below Groovy snippet into Job DSL groovy script,
// the method can be approved automatically.
// P.S. don't forgot to customize ${The Method Need To Be Approve}
//
// repeat this line if want to approve more method automatically
scriptApproval.approveSignature("${The Method Need To Be Approve}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment