4. Jenkins: Declarative Pipelines & Shared Libraries
Jenkins is the most widely used open-source automation server. It's been the industry standard for CI/CD for over a decade. Understanding Jenkins is essential for working with enterprise CI/CD systems. This module covers Jenkins architecture, declarative pipelines, shared libraries, and best practices.
The key insight is that Jenkins pipelines are code. Pipelines are defined in a Jenkinsfile, which can be stored in source control. Declarative pipelines provide a structured, opinionated way to define pipelines. Scripted pipelines offer more flexibility. Shared libraries allow you to reuse pipeline code across multiple projects. Understanding these concepts is essential for building maintainable Jenkins pipelines.
This module covers Jenkins architecture, pipeline syntax, shared libraries, and production best practices.
Jenkins Architecture
Jenkins uses a master-agent architecture. The Jenkins master manages jobs, schedules builds, and hosts the web UI. Agents (also called slaves or nodes) execute jobs. This architecture enables distributed builds and scaling. The master can be a single server or a highly available cluster. Agents can be ephemeral (created for each job) or persistent. Understanding this architecture is essential for scaling Jenkins.
Jenkins supports cloud integration, allowing agents to be spun up on-demand in AWS, Azure, or GCP. This enables cost-effective CI/CD at scale. Jenkins also supports Kubernetes integration, allowing agents to run as pods in a Kubernetes cluster.
Declarative Pipeline Syntax
Declarative pipelines are the recommended way to define Jenkins pipelines. They use a structured Groovy DSL with predefined blocks. The basic structure includes: pipeline (top-level block), agent (where to run), stages (ordered list of stages), stages contain steps (commands to execute), and post (actions after pipeline completion).
Declarative pipelines are more readable and easier to maintain than scripted pipelines. They also provide built-in validation and error handling. Understanding declarative syntax is essential for writing Jenkins pipelines.
pipeline {
agent any
environment {
NODE_VERSION = '18'
}
stages {
stage('Build') {
steps {
echo 'Building the application...'
sh 'npm install'
sh 'npm run build'
stash includes: 'dist/**', name: 'build'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh 'npm test'
}
post {
always {
echo 'Tests completed'
}
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
echo 'Deploying to production...'
sh 'deploy.sh'
}
}
}
post {
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}
Shared Libraries
Shared libraries allow you to reuse pipeline code across multiple Jenkinsfiles. They're defined in a separate repository with a specific structure. Shared libraries can contain global variables, custom steps, and whole pipelines. They're essential for maintaining consistency across hundreds of pipelines.
Shared libraries are loaded using the @Library annotation. They can be versioned, allowing different projects to use different versions. They can also be loaded from different sources (Git, local, etc.). Understanding shared libraries is essential for enterprise Jenkins implementations.
// vars/buildAndDeploy.groovy
def call(String environment) {
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Deploy') {
when {
environment name: 'environment', value: environment
}
steps {
sh "deploy.sh $environment"
}
}
}
}
}
// Jenkinsfile using shared library
@Library('my-shared-library') _
buildAndDeploy('production')
Step-by-Step: Jenkins Pipeline Setup
Follow these steps to set up a Jenkins pipeline.
Step 1: Install Jenkins
Download Jenkins from jenkins.io and install it. You can run it locally using Docker:docker run -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
Step 2: Create a Jenkinsfile
Create a file called Jenkinsfile at the root of your repository.pipeline {<br> agent any<br> stages {<br> stage(<strong><code>Build) {
steps {
echo Building...
}
}
stage(Test) {
steps {
echo Testing...
}
}
}
}
Step 3: Create a Pipeline Job
In Jenkins, create a new pipeline job. Select Pipeline script from SCM and point to your repository.
Step 4: Add build steps
Update the Jenkinsfile with real build and test steps:stage(<strong><code>Build) {
steps {
sh npm install
sh npm run build
}
}
stage(Test) {
steps {
sh npm test
}
}
Step 5: Add notificationspost {<br> success {<br> echo <strong><code>Build succeeded! Sending notification...
}
failure {
echo Build failed! Alerting team...
}
}
Try It Yourself: Create a Jenkinsfile for a project with multiple stages. Add a deployment stage that runs only on the main branch. Configure email notifications for build failures.
Knowledge Check
Ready to test your understanding of 4. Jenkins: Declarative Pipelines & Shared Libraries?