Hello! In this post I want to show a simple Jenkins feature that proves to be very useful, especially in pipelines that are triggered manually. The execution parameters.
There are 5 basic types of parameters:
For more complex parameter types, there are many types of plugins that can be used.
In the example that I will leave below, I create an example with each of the items above, I show how to use the parameter values to decide Stages and I also make a println (Groovy), so you can use them as parameters, for example, for any call of libs.
An important point, which is another Jenkins behavior, and can be confusing. The parameters shown when we click on “Build with Parameters” are always related to the previous execution, so if you make a change now, you will first have to run the pipeline, even if it gives an error, so that the next time you come with the correct parameter changes . This behavior does not apply to the code itself, which is updated normally.
For example, to create the pipeline with the parameters below:
I leave the code below, simple, just to illustrate how to use the parameters and values. https://github.com/faustobranco/devops-db/blob/master/knowledge-base/jenkins/Parameters/pipeline.groovy
pipeline {
parameters {
string (name: 'str_services', defaultValue:'infrastructure', description: 'Blueprint origin service.')
choice (name: 'opt_action', choices: ['Deploy', 'Redeploy', 'Delete'], description: 'Action on the resource.')
booleanParam (name: 'bol_verbose', defaultValue: false, description: 'Show logs with verbose.')
text (name: 'txt_commitmessage', defaultValue: '''this is a multi-line\nstring parameter example\n''', description: 'Commit Message.')
password (name: 'pwd_deploy', defaultValue: 'SECRET', description: 'Password for deployment.')
}
agent any
stages {
stage('Build Deploy') {
when {
expression { return params.opt_action == 'Deploy' }
}
steps {
script {
println 'Build Deploy'
println 'String Parameter: ' + params.str_services
println 'Multi Line Parameter: ' + params.txt_commitmessage
println 'Option Parameter: ' + params.opt_action
println 'Boolean Parameter: ' + params.bol_verbose
println 'Password Parameter: ' + params.pwd_deploy
}
}
}
stage('Build Redeploy') {
when {
expression { return params.opt_action == 'Redeploy' }
}
steps {
script {
println 'Build Redeploy'
println 'String Parameter: ' + params.str_services
println 'Multi Line Parameter: ' + params.txt_commitmessage
println 'Option Parameter: ' + params.opt_action
println 'Boolean Parameter: ' + params.bol_verbose
println 'Password Parameter: ' + params.pwd_deploy
}
}
}
stage('Delete') {
when {
expression { return params.opt_action == 'Delete' }
}
steps {
script {
println 'Delete'
println 'String Parameter: ' + params.str_services
println 'Multi Line Parameter: ' + params.txt_commitmessage
println 'Option Parameter: ' + params.opt_action
println 'Boolean Parameter: ' + params.bol_verbose
println 'Password Parameter: ' + params.pwd_deploy
}
}
}
}
}
The result looks like this: