Updated on 2025-01-17 GMT+08:00

Procedure

Scenario

There are components A, B, C, and D in this example.

Component D depends on components B and C, and components B and C depend on component A.

The Jenkins pipeline is used to orchestrate and deploy the four components. You can upgrade one or more components without considering the dependency.

Configuring a Jenkins Pipeline

  1. Go to the Jenkins system management page and click New Item.
  2. Enter a task name and select Pipeline.
  3. On the General page, add Extended Choice Parameter.
  4. Configure the components to be orchestrated.

    Table 1 Configuring build parameters

    Parameter

    Description

    Example

    Name

    Parameter name. The component selected during build is stored in the variable name.

    Model_Name

    Description

    Description of the build parameter.

    Select the components to be upgraded.

    Parameter Type

    Basic parameter type.

    Check Boxes

    Number of Visible Items

    Number of components orchestrated by the pipeline.

    4

    Delimiter

    Separator between components. Set it to a comma (,).

    ,

    Value

    Name of the components orchestrated by the pipeline. Use commas (,) to separate multiple component names.

    componentA, componentB, componentC, componentD

  5. Select Pipeline. On the Pipeline page, configure the script. For details, see Pipeline Script.
  6. Click Save.

Pipeline Script

The pipeline script can upgrade one or more of components A, B, C, and D at the same time. Component A is upgraded first, then components B and C are upgraded concurrently, and finally component D is upgraded. Components that are not selected during the build will be skipped and not upgraded.

pipeline {
    agent any
    environment{
        // The components selected during build are separated by commas (,) and stored in the variable Model_Name.
        // Check whether the optional components in the variable Model_Name exist. If yes, assign the component name to the corresponding variable. If no, assign the string "error" to the corresponding variable.
        // Used to determine whether subsequent components need to be built.
        _componentA="${sh(script:' echo $Model_Name| grep -w -o "componentA" || echo "error" ', returnStdout: true).trim()}"
        _componentB="${sh(script:' echo $Model_Name| grep -w -o "componentB" || echo "error" ', returnStdout: true).trim()}"
        _componentC="${sh(script:' echo $Model_Name| grep -w -o "componentC" || echo "error" ', returnStdout: true).trim()}"
        _componentD="${sh(script:' echo $Model_Name| grep -w -o "componentD" || echo "error" ', returnStdout: true).trim()}"
    }

    stages {
        stage('Build componentA') {
           // When the environment variable _componentA equals to componentA, the component is selected during build. In this case, execute this stage. Otherwise, skip this stage.
            when { environment name: '_componentA', value: 'componentA' }
            steps {
               sh ''' echo "start to build componentA" '''
               script{
                  //build (job: 'componentA') indicates that the component A task is executed. This task is created in Jenkins and is used to upgrade component A.
                  def componentBuild=build(job: 'componentA')
                  // Print the task execution result.
                  println componentBuild.getResult()
               }
            }
        }
        stage('Build parallel jobs') {
            // failFast true indicates that other parallel stages will be stopped as long as one stage fails.
            // If failFast true is not added, the failed stage does not affect the execution of other parallel stages until all parallel stages are complete.
            //failFast true // Add as required.

            // parallel indicates that the stages are executed in parallel. If one stage fails to be executed, the stage in "Build parallel jobs" fails and the subsequent stages will not be executed.
            parallel {
                stage('Build componentB') {
                    when { environment name: '_componentB', value: 'componentB' }
                    steps {
                       sh ''' echo "start to build componentB" '''
                       script{
                          def componentBuild=build(job: 'componentB')
                          println componentBuild.getResult()
                        }
                    }
                }
                stage('Build componentC') {
                    when { environment name: '_componentC', value: 'componentC' }
                    steps {
                       sh ''' echo "start to build componentC" '''
                       script{
                          def componentBuild=build(job: 'componentC')
                          println componentBuild.getResult()
                       }
                    }
                }
            }
        }
        stage('Build componentD') {
            when { environment name: '_componentD', value: 'componentD' }
            steps {
               sh ''' echo "start to build componentD" '''
               script{
                  def componentBuild=build(job: 'componentD')
                  println componentBuild.getResult()
               }
            }
        }
    }
}

Script Description

Each component needs to obtain component parameters from the environment in the pipeline to determine whether the component is selected during parameterized build. Run the following script:

_{component_name}="${sh(script:' echo $Model_Name| grep -w -o "{component_name}" || echo "error" ', returnStdout: true).trim()}"
Table 2 Parameters

Parameter

Description

Model_Name

Name in Configuring build parameters.

{component_name}

One of the component name in Configuring build parameters.

A stage needs to be configured for each component for component upgrade tasks. If the tasks are executed in serial mode, the stages are placed in sequence. If the tasks are executed in parallel mode, the stages are placed in parallel in stage('Build parallel jobs').

Stage script:

stage('Build {component_name}') {
    
    when { environment name: '_{component_name}', value: '{component_name}' }
    steps {
       sh ''' echo "start to build {component_name}" '''
       script{
          def componentBuild=build(job: '{component_jenkins_task}')
          // Print the task execution result.
          println componentBuild.getResult()
       }
    }
}
Table 3 Parameters

Parameter

Description

{component_name}

One of the component name in Configuring build parameters.

{component_jenkins_task}

Name of the component upgrade task created in Jenkins.

Upgrading Multiple Components Using a Pipeline

  1. Select a pipeline and click Build with Parameters.
  2. Select the components to be upgraded. You can select one or more components at the same time. In this example, all components are upgraded at the same time. Click Start Build.
  3. You can view the pipeline task execution result in the build history.
  4. The pipeline log is as follows, indicating that component A is upgraded first, then component B and component C are upgraded concurrently, and finally component D is upgraded.

    Started by user admin
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in /root/.jenkins/workspace/pipeline-arrange
    [Pipeline] {
    [Pipeline] sh
    + echo componentA,componentB,componentC,componentD
    + grep -w -o componentA
    [Pipeline] sh
    + echo componentA,componentB,componentC,componentD
    + grep -w -o componentD
    [Pipeline] sh
    + echo componentA,componentB,componentC,componentD
    + grep -w -o componentB
    [Pipeline] sh
    + echo componentA,componentB,componentC,componentD
    + grep -w -o componentC
    [Pipeline] withEnv
    [Pipeline] {
    [Pipeline] stage
    [Pipeline] { (Build componentA)
    [Pipeline] sh
    + echo 'start to build componentA'
    start to build componentA
    [Pipeline] script
    [Pipeline] {
    [Pipeline] build
    Scheduling project: componentA
    Starting building: componentA #13
    [Pipeline] echo
    SUCCESS
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Build parallel jobs)
    [Pipeline] parallel
    [Pipeline] { (Branch: Build componentB)
    [Pipeline] { (Branch: Build componentC)
    [Pipeline] stage
    [Pipeline] { (Build componentB)
    [Pipeline] stage
    [Pipeline] { (Build componentC)
    [Pipeline] sh
    [Pipeline] sh
    + echo 'start to build componentB'
    start to build componentB
    [Pipeline] script
    + echo 'start to build componentC'
    start to build componentC
    [Pipeline] {
    [Pipeline] script
    [Pipeline] {
    [Pipeline] build
    Scheduling project: componentB
    [Pipeline] build
    Scheduling project: componentC
    Starting building: componentB #12
    Starting building: componentC #12
    [Pipeline] echo
    SUCCESS
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] echo
    SUCCESS
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // parallel
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Build componentD)
    [Pipeline] sh
    + echo 'start to build componentD'
    start to build componentD
    [Pipeline] script
    [Pipeline] {
    [Pipeline] build
    Scheduling project: componentD
    Starting building: componentD #10
    [Pipeline] echo
    SUCCESS
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

  5. Log in to CAE and check the component status. If Upgraded is displayed in the Last Change Status/Time column, the component has been upgraded.