< img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=3131724&fmt=gif" />

Use Jenkins Shared Libraries in a Pipeline

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

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

Prerequisites

  • DevOps must have been installed and enabled.

  • A workspace, a DevOps project, and a user (e.g. project-regular) have been created, and the user has been invited to the DevOps project with the operator role. See Role and Member Management.

  • You have a usable Jenkins shared library. This tutorial uses the Jenkins shared library in the GitHub repository as an example.

Step 1: Configure Shared Libraries 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 and click Add.

  4. Configure the fields as follows.

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

    • Default version: Set a branch name of the repository where the shared library is located as the default branch to import the shared library. Enter master for this tutorial.

    • 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 use your own repository and access to this repository requires credentials, you also need to configure Credentials.

  5. After editing, click Apply.

    Note

    You can also configure folder-level shared libraries.

Step 2: Use Shared Libraries in a Pipeline

Create a Pipeline

  1. Log in to the KubeSphere web console as the project-regular user.

  2. Click Workspace Management and enter your DevOps project, then click Create on the Pipelines page.

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

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

Edit the Pipeline

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

  2. In the dialog 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 that starts with @Library('<the configured shared library name>') _. 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()
            }
          }
        }
      }
    }
    Note

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

Step 3: Run the Pipeline

  1. On the pipeline detail page, click Run to run the pipeline.

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

Receive the latest news, articles and updates from KubeSphere


Thanks for the feedback. If you have a specific question about how to use KubeSphere, ask it on Slack. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.