Use Jenkins Shared Libraries in a Pipeline

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

    • You need to enable the KubeSphere DevOps system.
    • You need to create a workspace, a DevOps project and a user (). This user must be invited to the DevOps project with the operator role. For more information, refer to .
    • You need to have a Jenkins shared library available. This tutorial uses the Jenkins shared library in a GitHub repository as an example.
    1. 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 below.

      • 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 Source Code Management, select 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.

    5. When you finish editing, click Apply.

      Note

      You can also configure Folder-level Shared Libraries.

    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 displayed dialog box and click Next.

    3. On the Advanced Settings tab, click Create to create a pipeline with the default settings.

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

      Note

      You can specify a label for agent based on your needs.

    2. Alternatively, you can use a Jenkinsfile starting with . 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.

      1. @Library('demo-shared-library') _
      2. pipeline {
      3. stages {
      4. stage('Demo') {
      5. script {
      6. mvn.fake()
      7. }
      8. }
      9. }
      10. }
      11. }

      Note

      You can use to specify a specific branch.

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