Jenkins pipeline part 6 – parameters
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss parameters in a Jenkins pipeline and how to pass arguments in Jenkins pipeline using parameter directive.
What is parameters is Jenkins pipeline?
The parameters directive provides a list of parameters that a user can provide when triggering the Pipeline. The values for these user-specified parameters are made available to Pipeline steps via the params object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## ---------- ## parameters ## ---------- pipeline { agent { ... } parameters { } stages { stage(' steps { ${params. ... } ... } ... } } |
Let’s dig down a bit with some examples.
Jenkins pipeline part 6 – parameters
Prerequisites
One system with Jenkins installed.
string:
Pass a string value to your Jenkins pipeline using string parameters.
Create a new pipeline in your Jenkins controller server using below Jenkinsfile definition. Replace the label as per your Jenkins configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ------------------- ## parameters | string ## ------------------- pipeline { agent { label 'test' } parameters { string(name: 'ENVIRONMENT', defaultValue: 'development', description: 'set env to dev') } stages { stage('parameter_string') { steps { echo "${params.ENVIRONMENT}" } } } } |
Trigger the pipeline depending on you pipeline setup.
text:
Pass a multiline string value to your Jenkins pipeline using text parameters.
Update the pipeline definition with below and rerun the pipeline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ----------------- ## parameters | text ## ----------------- pipeline { agent { label 'test' } parameters { text(name: 'ENVIRONMENT', defaultValue: '', description: 'pass env') } stages { stage('parameter_text') { steps { echo "${params.ENVIRONMENT}" } } } } |
Jenkins will ask you to supply parameters values. Provide a multiline parameter and execute.
booleanParam
Pass a Boolean value (true or false) to your Jenkins pipeline using booleanParam parameters.
Update the pipeline definition with below and rerun the pipeline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ------------------------- ## parameters | booleanParam ## ------------------------- pipeline { agent { label 'test' } parameters { booleanParam(name: 'BUILD', defaultValue: true, description: 'do you want to build the app?') } stages { stage('parameter_booleanParam') { steps { echo "${params.BUILD}" } } } } |
Jenkins will ask to confirm parameters values. Check or uncheck the parameter and click “Build”.
choice:
Pass a list of values to your Jenkins pipeline using choice parameters.
Update the pipeline definition with below and rerun the pipeline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ------------------- ## parameters | choice ## ------------------- pipeline { agent { label 'test' } parameters { choice(name: 'ENV', choices: ['prod', 'dev', 'test'], description: 'select your build env') } stages { stage('parameter_choice') { steps { echo "${params.ENV}" } } } } |
Jenkins will ask to confirm parameters values. Select the parameter value from dropdown and click “Build”.
password:
Pass a secret value (password) to your Jenkins pipeline using choice password.
Update the pipeline definition with below and rerun the pipeline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## --------------------- ## parameters | password ## --------------------- pipeline { agent { label 'test' } parameters { password(name: 'PASSWORD', defaultValue: 'MYSECRET', description: 'Enter a password') } stages { stage('parameter_password') { steps { echo "${params.PASSWORD}" } } } } |
Jenkins will ask to confirm parameters values. Click on “Build” to use the default password or “Change Password” to update the password and then click “Build”.
Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.