jenkins pipeline library
A shared library is a collection of independent Groovy scripts which you pull into your Jenkinsfile at runtime.
The best part is, the Shared Library can be stored in a Git repository. It can be versioned, tagged, etc.
Collection of jenkins stages which can be glued together to setup complex pipelines.
Most of the groovy scripts are written for Kubernetes running on AWS. This Project offers sample pipelines to easily implement CI/CD processes. The corresponding “Shared Library” (
vars
) provides a set of “steps” to build your own scenarios beyond defaults.
Inside a Shared Library you’ll probably have two types of code:
Steps: These are called Global Variables in Jenkins terminology, but these are the custom steps that you want to be available to all your Jenkins pipelines.
For example, you can write a standard step to deploy an application, or perform a code review.
Other common code: This might include helper classes, or common code that you might want to include inside pipeline steps.
Pipeline Shared Libraries plugin
Other plugins may be required for specific library calls (i.e. AWS, Docker)
This library consists of groovy
and shell
scripts.
Build
-> artifact-repo (covering two use cases: gradle
and maven
projects)Deploy
-> config-repo (manage k8s, manage Terraform)List of sh
and groovy
scripts. Scripts are to build:
docker
imagesKubernetes
(Statefulset, Deployments, PVCs)Terraform
validationSonarqube
Git
(commits, approvals)OPA(Open Policy Agent)
, Spinnaker
NiFi
To use shared library in a pipeline, you add @Library('your-library-name')
to the top of your pipeline definition, or Jenkinsfile
@Library('pipeline-library-demo')_
stage('Demo') {
echo 'Hello world'
sayHello 'Dave'
}
NOTE: The underscore (_) is not a typo! You need this underscore if the line immediately after the @Library annotation is not an import statement.
stage('Start') {
steps {
sendStartBuildNotification(env.channel)
}
stage('Apply Kubernetes Definitions') {
when {
anyOf{
branch "master"
branch "production"
}
}
steps {
applyKubernetesChangesContext()
}
}
There is a plan to split
Build
and Deploy
and use Jenkins for building and Spinnaker for deploying.