Jenkins pipeline parameters parametric build usage example

Jenkins Pipeline Parameters allows users to provide some parameters when running Jenkins Pipeline, so that the Pipeline can be dynamically configured at runtime based on the values ​​of these parameters. Here is an example of Jenkins Pipeline Parameters:

pipeline {
    agent any
    parameters {
        string(name: 'BUILD_VERSION', defaultValue: '1.0', description: 'Version number to build')
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, description: 'Deploy to production server?')
        choice(name: 'ENVIRONMENT', choices: ['dev', 'qa', 'prod'], description: 'Select environment for deployment')
    }
    stages {
        stage('Build') {
            steps {
                echo "Building version ${params.BUILD_VERSION}"
            }
        }
        stage('Test') {
            steps {
                echo "Running tests on version ${params.BUILD_VERSION}"
            }
        }
        stage('Deploy') {
            when {
                expression { params.DEPLOY_TO_PROD == true }
            }
            steps {
                echo "Deploying to ${params.ENVIRONMENT}"
            }
        }
    }
}

In this example, we define three parameters:

  • BUILD_VERSION: A parameter of type string, used to specify the version number to build. If the user does not provide this parameter, it defaults to 1.0.
  • DEPLOY_TO_PROD: A Boolean parameter indicating whether to deploy the build result to production. If the user does not provide this parameter, it defaults to false.
  • ENVIRONMENT: A select-type parameter that specifies the environment to deploy to. The options are dev, qaand prod. If the user does not provide this parameter, there will be no default value.

We use these parameters in different stages of the Pipeline. For example, in Buildthe stage we used $params.BUILD_VERSIONto print the version number to build. In Deploythe stage, we use $params.DEPLOY_TO_PRODand $params.ENVIRONMENTto decide whether to deploy the build result to the production environment and to which environment.

When users run the Pipeline, they will be asked to provide values ​​for these parameters. Users can enter these values ​​in the Jenkins interface, or provide these values ​​dynamically through the Jenkins API.

Guess you like

Origin blog.csdn.net/a772304419/article/details/131998978