For Jenkins pipelines containing the same stages or steps, you can use Jenkins shared libraries in the Jenkinsfile to avoid code duplication.

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

Prerequisites

  • The DevOps extension must be installed and enabled on the KubeSphere platform.

  • A workspace, a DevOps project, and a user (for example, project-regular) have been created. The user has been invited to the DevOps project and granted the operator role. Please refer to Invite a User to a DevOps Project.

  • An available Jenkins shared library is ready. This tutorial uses the Jenkins shared library from the GitHub repository as an example.

Step 1: Configure the Shared Library in the Jenkins Dashboard

  1. Log in to the Jenkins Dashboard and click Manage Jenkins in the left navigation pane.

  2. Scroll down and click Configure System.

  3. Scroll down to Global Pipeline Libraries, then click Add.

  4. Configure the fields as shown below.

    • Name: Set a name for the shared library (for example, demo-shared-library). This name will be referenced in the Jenkinsfile to import the shared library.

    • Default version: Set a branch name of the repository where the shared library is located, which will be used as the default branch for importing the shared library. This tutorial uses master.

    • Under Retrieval method, select Modern SCM.

    • Under Source Code Management, select Git, and enter the URL of the example repository for Project Repository. If you are using your own repository and credentials are required to access it, you also need to configure Credentials.

  5. After editing, click Apply.

    Note

    You can also configure Folder-level Shared Libraries.

Step 2: Use the Shared Library in a Pipeline

Create a Pipeline

  1. Log in to the KubeSphere console as the project-regular user and enter your workspace.

  2. Click  DevOps > Pipelines in the left navigation pane.

  3. Select a DevOps project from the drop-down list in the upper left corner of the page.

  4. Click Create on the page.

  5. In the dialog box that appears, name it demo-shared-library and click Next.

  6. In Advanced Settings, directly click Create to create the pipeline using the default settings.

Edit the Pipeline

  1. On the pipeline list page, click the pipeline name to enter its details page, then click Edit Jenkinsfile.

  2. In the dialog box that appears, add the following example Jenkinsfile. After 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()
            }
          }
        }
      }
    }
    Note

    Specify a label for agent as needed.

    Alternatively, use a Jenkinsfile starting with @Library('<configured shared library name>') _ . If using this type of Jenkinsfile, you need to configure the shared library in 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()
            }
          }
        }
      }
    }
    Note

    Use @Library('demo-shared-library@<branch name>') _ to specify a particular branch.

Step 3: Run the Pipeline

  1. On the pipeline details page, click Run to execute the pipeline.

  2. Click the record under the Run Records tab to view the pipeline run details. Click Run Logs to view the detailed logs.