Updated on 2023-10-30 GMT+08:00

Configuring a Pipeline Build Task

  • Scenario 1: If a software package is generated using Jenkins, for example, a JAR package, use the software package deployment scenario in the script. During deployment, the built software package is uploaded to the OBS bucket and the ServiceStage component is upgraded.
  • Scenario 2: If an image package is generated using Jenkins, use the image deployment scenario in the script. During deployment, the built image package is uploaded to the SWR image repository and the ServiceStage component is upgraded.

This section uses the scenario where the instance in Configuring a Pipeline Script is a JAR package as an example.

Creating a GitLab Credential

Use the account and password with the GitLab code repository permission to create a credential in Jenkins for pulling GitLab code.

  1. Enter http://{IP address of the Linux VM where Jenkins is installed}:8080 in the address box of the browser to log in to Jenkins.
  2. Choose Manage Jenkins > Jenkins Configuration. In Configuration, select Gitlab.
  3. Click Add under Credentials and select Jenkins.
  4. Configure the GitLab account password and click Add to save the configuration.
  5. Choose Manage Jenkins > Manage Credentials to view the configured credentials.

    The unique ID is used in Configuring a Pipeline Script.

Creating a Pipeline Task

  1. Enter http://{IP address of the Linux VM where Jenkins is installed}:8080 in the address box of the browser to log in to Jenkins.
  2. Click New Item.
  3. Enter the task name, for example, test-upgrade, select Pipeline, and click OK.

Configuring a Build Trigger

  1. Configure the Jenkins build trigger.

    1. Select Build when a change is pushed to GitLab, save the GitLab webhook URL (required when configuring GitLab webhook), and click Advanced in the lower right corner.
    2. Select Filter branches by regex and configure the build task to be triggered after the specified branch is changed. In the example, the branch name is main. Click Generate to generate a secret token and save it. The token will be used for configuring GitLab webhook.

  2. Configure GitLab webhook.

    1. Log in to GitLab and go to the code repository. In the example, the repository name is test. Select Webhooks in settings, and set URL and Secret token to the GitLab webhook URL and secret token obtained in 1.
    2. Deselect Enable SSL verification for SSL verification and click Add webhook.

Configuring a Pipeline Script

A pipeline script is a build command that runs during build. For details about script parameters, see Table 1.

Table 1 Table 1 Pipeline script parameters

Parameter

Mandatory

Type

Description

git_url

Yes

String

Address of the GitLab code repository.

credentials_id

Yes

String

GitLab credential ID configured using the account password. For details, see Creating a GitLab Credential.

branch_name

Yes

String

Name of the GitLab code repository branch .

maven

Yes

String

Path of the executable file for Maven installation, for example, /root/app/maven/apache-maven-3.8.6/bin/mvn.

upgrade_shell

Yes

String

Path for storing the upgrade.sh script on the VM where Jenkins is deployed, for example, /root/jar/upgrade.sh. For details, see upgrade.sh Description.

  1. After the build trigger is configured, select Pipeline script from the drop-down list on the Pipeline tab.
  2. Configure the pipeline script. In the example, the JAR package build scenario is used. The script is as follows:

    Replace the parameters in the script with the actual parameters in your environment.
    node {
        //Code repository address, for example, http://10.95.156.58:8090/zmg/test.git.
        def git_url = '{Code repository address}'
        //GitLab credential ID.
        def credentials_id = '{GitLab credential ID}'
        //Name of the Git code repository branch, for example, main.
        def branch_name = '{Git code repository branch name}'
        //Path of the executable file for Maven installation, for example, /root/app/maven/apache-maven-3.8.6/bin/mvn.
        def maven = '{Path of the executable file for Maven installation}'
        //Path for storing the upgrade.sh script, for example, /root/jar/upgrade.sh.
        def upgrade_shell = '{Path for storing the upgrade.sh script}'
    
        stage('Clone sources') {
            git branch: branch_name, credentialsId: credentials_id, url: git_url
        }
        stage('Build') {
            //Build a JAR package.
            sh "'$maven' clean package -Dmaven.test.failure.ignore=true -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true"
        }
        stage('upgrade') {
            //Execute the script and use the JAR package uploaded to OBS to upgrade the ServiceStage component. The timeout period is 5 minutes.
            sh "timeout 300s '$upgrade_shell'"
        }
    }
    • During pipeline script running, upgrade.sh is invoked. For details about the script, see upgrade.sh Description.
    • Set upgrade.sh as an executable file.