Use Jenkins Shared Libraries in a Pipeline

For Jenkins pipelines that contain the same stages or steps, one way to avoid repetition in the pipeline codes is to use Jenkins shared libraries in the Jenkinsfiles.

This tutorial demonstrates how to use Jenkins shared libraries in KubeSphere DevOps pipelines.

Prerequisites

Configure a Shared Library on the Jenkins Dashboard

  1. Log in to the Jenkins dashboard and click Manage Jenkins in the left navigation bar.

  2. Scroll down and click Configure System.

click_configure

  1. Scroll down to Global Pipeline Libraries and click Add.

    click-add

  2. Configure the fields as below.

    • Name. Set a name (for example, demo-shared-library) for the shared library so that you can import the shared library by referring to this name in a Jenkinsfile.

    • Default version. Set a branch name from the repository where you put your shared library as the default branch for importing your shared library. Enter master for this tutorial.

    • Under Retrieval method, choose Modern SCM.

    • Under Source Code Management, choose Git and enter the URL of the example repository for Project Repository. You have to configure Credentials if you use your own repository that requires the credentials for accessing it.

      configure-shared-library

  3. When you finish editing, click Apply.

    备注

    You can also configure Folder-level Shared Libraries.

Use the Shared Library in a Pipeline

Step 1: Create a pipeline

  1. Log in to the KubeSphere web console as project-regular. Go to your DevOps project and click Create on the Pipelines page.

  2. Set a name (for example, demo-shared-library) in the pop-up window and click Next.

    set-name

  3. In Advanced Settings, click Create directly to create a pipeline with the default settings.

    click-create

Step 2: Edit the pipeline

  1. In the pipeline list, click the pipeline to go to its detail page and click Edit Jenkinsfile.

    edit-jenkinsfile

  2. In the displayed dialog box, enter the following example Jenkinsfile. When you finish editing, click OK.

    library identifier: 'devops-ws-demo@master', retriever: modernSCM([
        $class: 'GitSCMSource',
        remote: 'https://github.com/devops-ws/jenkins-shared-library',
        traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]
    ])
       
    pipeline {
        agent any
       
        stages {
            stage('Demo') {
                steps {
                    script {
                        mvn.fake()
                    }
                }
            }
        }
    }
    

    备注

    You can specify a label for agent based on your needs.
  3. Alternatively, you can use a Jenkinsfile starting with @Library('<the configured name of shared library>') _. If you use this type of Jenkinsfile, you need to configure the shared library on the Jenkins dashboard in advance. In this tutorial, you can use the following example Jenkinsfile.

    @Library('demo-shared-library') _
       
    pipeline {
        agent any
       
        stages {
            stage('Demo') {
                steps {
                    script {
                        mvn.fake()
                    }
                }
            }
        }
    }
    

    备注

    You can use @Library('demo-shared-library@<branch name>') _ to specify a specific branch.

Step 3: Run the pipeline

  1. You can view the stage under the Pipeline tab. Click Run to run it.

    click-run

  2. After a while, the pipeline will run successfully.

    run-successfully

  3. You can click the Success record under Status, and then click Show Logs to view the log details.

    log-details


感谢您的反馈。如果您有关于如何使用 KubeSphere 的具体问题,请在 Slack 上提问。如果您想报告问题或提出改进建议,请在 GitHub 存储库中打开问题。