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

Building and Executing a Pipeline on Jenkins

Building a Pipeline

Build a pipeline in Jenkins to pull code from the code repository, pack the code into an image, and push the image to the SWR image repository.

  1. Click Dashboard in the upper left corner of the page to switch to the Jenkins Dashboard page. In the navigation pane on the right, click New Item.
  2. Enter an item name (for example, test-pipe) and select Pipeline.

    Figure 1 Pipeline

  3. Configure only the pipeline script and retain the default values for other parameters.

    The following pipeline script is for reference only. You can modify the script content based on your service requirements. For details about the syntax, see Pipeline.

    def swr_region = "cn-east-3"
    def organization = "container"
    def git_repo = "http://github.com/xxx.git"
    def app_git_branch = "master"
    
    
    podTemplate(
    inheritFrom: 'jenkins-agent',    // Replace the value with the name of the pod template created in 5.
    cloud: 'ap-test'    // Replace the value with the name of the cloud created in 4.
    ) {
       // Pull the code from the code repository.
       node(POD_LABEL) {
          stage('Pull the code.') {
                echo "pull clone"
                git branch: "${app_git_branch}",  url: "${git_repo}"
           }
    
       // Use the maven container to pack the code pulled from the code repository. (This packing method applies only to Java. Use another packing method for other languages.)
       container('maven'){
          stage ('Pack the code.') {
                echo "build package"
                sh "mvn clean package  -DskipTests"
           }
        }
    
       // Use the kaniko container to push the packed code to SWR and name the image tomcat.
       container('kaniko'){
          stage('Push the image.') {
                echo "build images and push  images"
                sh "/kaniko/executor -f Dockerfile -c . -d swr.${swr_region}.myhuaweicloud.com/${organization}/tomcat:${BUILD_ID} --force"
           }
        }      
    }
    }
    Table 1 Pipeline script parameters

    Parameter

    Example Value

    Description

    swr_region

    cn-east-3

    Region where the SWR image repository is located. For details, see Regions and Endpoints.

    NOTE:

    The SWR image repository is used to store images packaged by code. The region must be the same as that in 3.

    organization

    container

    Organization name in SWR. You can enter any organization name as needed.

    git_repo

    https://github.com/xxx.git

    Specific address where the code is stored, which is the address of the code library.

    app-git-branch

    master

    Branch of the code library.

  4. Click Save.

Executing the Pipeline and Viewing the Execution Result

After the pipeline is executed, a pod named pipe-xxx will be automatically created in the cluster, and three containers (named jnlp, kaniko, and maven) will be created in the pod based on the information in the pod template. The pod pulls code from the code repository, packs the code into an image, and pushes the image to the SWR image repository. After the operations are complete, the pod is automatically deleted.

  1. In the navigation pane, choose Build Now to execute the pipeline job.
  2. Return to the CCE console and click the cluster name. In the navigation pane, choose Workloads. On the Pods tab, view the pod created by the pipeline.

    Figure 2 Pod created by the pipeline

  3. Click More > View Container in the Operation column. You can see that three containers are created based on the pod template.

    Figure 3 Creating a container

  4. Verify that the pod has been deleted automatically. After the code is pulled from the code repository and packed into an image, and the image is pushed to the SWR image repository, the pod will be automatically deleted.

    Figure 4 Automatic pod deletion

  5. Log in to the SWR console and verify that the Tomcat image is available in the SWR image repository.

    Figure 5 Pushed image

Follow-up Operations: Releasing Resources

To avoid additional expenditures, release resources promptly if you no longer need them.

  1. Log in to the CCE console. In the navigation pane, choose Clusters.
  2. Locate the cluster, click in the upper right corner of the cluster card, and click Delete Cluster.
  3. In the displayed Delete Cluster dialog box, delete related resources as prompted.

    Enter DELETE and click Yes to start deleting the cluster.

    It takes 1 to 3 minutes to delete a cluster. If the cluster name disappears from the cluster list, the cluster has been deleted.

  4. Log in to the ECS console. In the navigation pane, choose Elastic Cloud Server. Locate the target ECS and click More > Delete.

    In the displayed dialog, select Delete the EIPs bound to the ECSs and Delete all data disks attached to the ECSs, and click Next.

    Figure 6 Deleting ECSs

    Enter DELETE and click OK to start deleting the ECS.

    It takes 0.5 minutes to 1 minute to delete an ECS. If the ECS name disappears from the ECS list, the ECS has been deleted.

    Figure 7 Confirming the deletion