How do I use Jenkins Pipeline properties step?
Using properties
with explicit method syntax will work, i.e.:properties( [ ... ] )
rather than properties [ ... ]
Alternatively, it will work without if you specify the parameter name, e.g.:
properties properties: [ ... ]
For example defining three properties is as easy as :
properties([
parameters([
string(name: 'submodule', defaultValue: ''),
string(name: 'submodule_branch', defaultValue: ''),
string(name: 'commit_sha', defaultValue: ''),
])
])
/* Accessible then with : params.submodule, params.submodule_branch... */
There is an excellent example of parameters usage in the official Jenkins doc. Check the pipeline below:
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
Thanks to e.g. boolean parameter which generates checkbox in Jenkins GUI you can conditionally run / skip tests:
pipeline {
agent any
parameters {
booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Should we run tests before deployment?')
}
stages {
stage('Test') {
when {
expression {
return params.RUN_TESTS
}
}
steps {
sh '${FABRIC} test'
}
}
stage('Deploy') {
steps {
sh '${FABRIC} deploy'
}
}
}
}
Multiple choice in Jenkins scripted pipeline
properties([
parameters([
choice(choices: 'sprint_6\nsprint_7\nsprint_8\nSprint_9', description: 'Select branch to Build', name: 'Branch'),
choice(choices: 'No\nYes', , name: 'choice2'),
choice(choices: 'No\nYes', name: 'choice3')
])
])