Some projects require dynamically passing in parameters, such as requiring user input, or uploading a file, or checking configurations, as different parameters that are used as environment variables to influence the specific build process when built.
For example, we know that sh “printenv” will print all environment variables for debugging purposes, but if written in pipeline, it will output a lot of content every time the console output is built. Such as now
stage('debug') {
steps {
sh "printenv"}}Copy the code
I want to be able to manually control whether debugging information is output at build time. The default value is off. That is, no output is displayed. Information is displayed only after a check.
The following example shows how to do this
When the newly created project is freestyle or Pipeline type, we will find an option “This project is parameterized” in the General TAB of the configuration page, which means that the project type is parameterized. After selecting This option, You can add many types of parameters, as shown in the figure below
For example, if I add a Boolean Parameter here, the name of the Parameter is is_print_env, and by default the environment variable information is not displayed, so I don’t want to execute sh “printenv”.
Modify the previous pipeline, according to the is_print_env value of different logic.
stage('debug') {
steps {
// echo env.is_print_env
script {
if (env.is_print_env) {
sh "printenv"
} else {
echo "no execute 'sh printenv'"}}}}Copy the code
After saving, go to the home page of the project. In the left function list, you will find that “Build Now “has changed to “Build with Parameters”. The Boolean Parameter configuration is visualized by clicking on it.
If checked, all environment variables will be printed
Pipeline Parameter
Pipeline syntax supports passing parameters directives. Parameter includes string, string, string, string, string, string, string, string, string Text (multiple lines of text), Boolean, choice(dropdown), file (rarely used), password(password type), etc.
pipeline {
agent any
parameters {
booleanParam(defaultValue: true, description: ' ', name: 'p_userFlag')
choice(
choices: 'dev\nprod',
description: 'choose deploy environment',
name: 'p_deploy_env'
)
string (name: 'p_version', defaultValue: '1.0.0', description: 'build version')
text (name: 'p_deploy_text', defaultValue: 'One\nTwo\nThree', description: ' ')
password (name: 'p_password', defaultValue: ' ', description: ' ')}}Copy the code
After saving the Settings, you need to manually execute the Settings to view the effects on the page
The passed parameters are placed in an object named params, which can be used directly in the Pipeline. For example, params.userFlag refers to the userFlag parameter defined in the Parameters directive
Make logical judgments based on parameters
stage('debug') {
steps {
script {
if (params.p_deploy_env == 'dev') {
echo "deploy to dev"}}}}Copy the code
Conditional BuildStep can be installed to determine conditions just like the when directive. Here is how to write after installing the plug-in
pipeline {
agent any
parameters {
choice(name: 'CHOICES', choices: 'dev\nstaging', description: 'Please select a deployment environment')
}
stages {
stage('deploy test') {
when {
expression( return params.CHOICES == 'test')
}
scripts {
echo 'deploy to test'
}
}
stage('deploy staging') {
when {
expression( return params.CHOICES == 'staging')
}
scripts {
echo 'deploy to staging'}}}}Copy the code
Expression is essentially a block of Groovy code that can write more complex logical judgments
when {
expression { return A || B || C && D }
}
Copy the code
Extract from a file
when {
expression { return readFile('pom.xml'.contains('foo'))}}Copy the code
regular
when {
expression { return returntoken ==~ /(? i)(Y|YES|TRUE)/) } }Copy the code
The input step
Executing the input step suspends the Pipeline until the user enters parameters. Scenario 1: Approval process. Pipeline is suspended at the stage before deployment. The person in charge clicks “OK” before deployment. 2. For manual tests, add a manual test stage, which has only one input step. You can pass this input step only after passing the manual test.
Add input step to pipeline
pipeline {
agent any
stages {
stage('deploy') {
steps {
input message: 'Publish or stop'// If there is only one messge parameter, it can be abbreviated as input'Publish or stop'}}}Copy the code
Whether aborted or passed, the job log records who did it, which is very audit friendly