Skip to content

Instantly share code, notes, and snippets.

@razbomi
Last active June 19, 2023 11:00
Show Gist options
  • Save razbomi/90bfa275bebc01a4356ab8241b305af3 to your computer and use it in GitHub Desktop.
Save razbomi/90bfa275bebc01a4356ab8241b305af3 to your computer and use it in GitHub Desktop.
Create Jenkins pipeline job parameters from yaml

Jenkins Pipeline Parameters Party

Not that you would, but if you DID add a whole bunch of parameters for your pipeline job... why not have it in version control?

To avoid the obligatory 500 million unsafe script approvals readMyProps may need to got to workflow-libs folder

def myText = '''
properties:
- param: booleanParam
args:
name: ENABLE_AAT
defaultValue: true
description: "Should we run AATs"
- param: stringParam
args:
name: NEXT_BUILD
defaultValue: '${env.BUILD_NUMBER.toInteger() + 1}'
description: 'Choose the next build number'
'''
node('master') {
stage('yaml') {
def myYaml = readYaml text: myText
def myProps = readMyProps myYaml.properties
properties([
parameters(myProps)
])
}
}
@NonCPS
def readMyProps(properties) {
properties.collect { parameter ->
this.invokeMethod parameter.param, parameter.args.collectEntries { name, value ->
[
name,
value instanceof String ? interp(value) : value
]
}
}
}
@NonCPS
def interp(value) {
new groovy.text.GStringTemplateEngine()
.createTemplate(value)
.make([env:env])
.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment