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
- You need to enable the KubeSphere DevOps system.
- You need to create a workspace, a DevOps project and a user (
project-regular
). This user must be invited to the DevOps project with theoperator
role. For more information, refer to Create Workspaces, Projects, Users and Roles. - You need to have a Jenkins shared library available. This tutorial uses the Jenkins shared library in a GitHub repository as an example.
Configure a Shared Library on the Jenkins Dashboard
Log in to the Jenkins dashboard and click Manage Jenkins in the left navigation pane.
Scroll down and click Configure System.
Scroll down to Global Pipeline Libraries and click Add.
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, select Modern SCM.
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.
When you finish editing, click Apply.
Note
You can also configure Folder-level Shared Libraries.
Use the Shared Library in a Pipeline
Step 1: Create a pipeline
Log in to the KubeSphere web console as
project-regular
. Go to your DevOps project and click Create on the Pipelines page.Set a name (for example,
demo-shared-library
) in the displayed dialog box and click Next.On the Advanced Settings tab, click Create to create a pipeline with the default settings.
Step 2: Edit the pipeline
In the pipeline list, click the pipeline to go to its details page and click Edit Jenkinsfile.
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() } } } } }
Note
You can specify alabel
foragent
based on your needs.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() } } } } }
Note
You can use@Library('demo-shared-library@<branch name>') _
to specify a specific branch.
Step 3: Run the pipeline
You can view the stage under the Task Status tab. Click Run to run it.
After a while, the pipeline ran successfully.
You can click the Successful record under Run Records, and then click View Logs to view the log details.
Feedback
Was this page Helpful?
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.