Pipeline Perfection: A Step-by-Step Mastery of Jenkins Pipelines

Lakshmi Thungala
Towards AWS
Published in
6 min readJul 8, 2024

Jenkins is a popular open-source automation server that facilitates continuous integration and continuous delivery (CI/CD). One of Jenkins’ most powerful features is the Pipeline, which allows for the definition and automation of complex workflows. This blog delves into the basics of Jenkins pipelines, provides a step-by-step guide to creating your first Pipeline, and explores advanced features to optimize your CI/CD processes with interactive examples.

Introduction to Jenkins Pipelines

Jenkins pipelines are a suite of plugins that support the implementation and integration of continuous delivery pipelines into Jenkins. These pipelines define your build, test, and deployment stages in a simple, human-readable format, making it easier to maintain and share your CI/CD workflows.

Building Jenkins pipeline

Benefits of Jenkins Pipelines

➡️ Code as Configuration: Pipelines are defined in code, typically using Groovy, which allows for version control and easy collaboration.

➡️ Complex Workflows: Pipelines can model complex workflows, including parallel execution and conditional steps.

➡️ Robust Plugin Ecosystem: Leverage Jenkins’ vast plugin ecosystem to extend pipeline functionality.

➡️ Scalability: Pipelines can be scaled across multiple nodes, supporting large and distributed builds.

Setting Up Jenkins

Before creating a Jenkins pipeline, ensure you have Jenkins installed and running. Follow these steps to set up Jenkins:

➡️ Install Jenkins:

Download the Jenkins installer from the official website https://www.jenkins.io/doc/book/installing/

Follow the installation instructions for your operating system.

➡️ Start Jenkins:

Run Jenkins and access it through your web browser at http://localhost:8080.

➡️ Install Required Plugins:

  • Navigate to Manage Jenkins > Manage Plugins and install the following plugins:
  • Pipeline
  • Git
  • GitHub Integration (optional, if using GitHub)

Creating Your First Jenkins Pipeline

Let’s create a simple Jenkins pipeline to demonstrate the basics.

Step 1: Create a New Pipeline Job

  1. Open Jenkins Dashboard: Access your Jenkins instance and log in.
  2. Create a New Item: Click New Item, select Pipeline, name your job, and click OK.

Step 2: Define the Pipeline Script

  1. Open the Pipeline Configuration: Navigate to the Pipeline section in the newly created pipeline job.
  2. Add Pipeline Script: Choose Pipeline script and enter the following script:
pipeline {
// Define the agent where the pipeline will run. 'any' means it can run on any available agent.
agent any

// stages of the pipeline
stages {
// First stage: Build
stage('Build') {
steps {
// message to confirm build process is starting.
echo 'Building...'
// Add build steps here, such as compiling code, running build scripts, etc.
}
}

// Second stage: Test
stage('Test') {
steps {
// message to confirm test process is starting.
echo 'Testing...'
// Add testing steps here, such as running unit tests, integration tests, etc.
}
}

// Third stage: Deploy
stage('Deploy') {
steps {
// message to confirm the deployment process is starting.
echo 'Deploying...'
// Add deployment steps here, such as deploying to a server, updating a database, etc.
}
}
}
}
  1. Save and Run: Save the Pipeline and click Build Now to run it. You should see the stages executed in sequence, with logs displaying the messages defined in the echo steps.

Step 3: Adding Source Control Integration

To make the Pipeline more useful, integrate it with a source control system like GitHub.

  1. Set Up a GitHub Repository:
  • Create a new repository on GitHub.
  • Add a Jenkinsfile to the root of the repository with the following content:
pipeline {
// Define the agent where the pipeline will run. 'any' means it can run on any available agent.
agent any

// stages of the pipeline
stages {
// First stage: Build
stage('Build') {
steps {
echo 'Building...'
}
}

// Second stage: Test
stage('Test') {
steps {

echo 'Testing...'
}
}

// Third stage: Deploy
stage('Deploy') {
steps {
echo 'Deploying...'

}
}
}
}
  1. Configure Jenkins to Use GitHub:
  • In your Jenkins job configuration, scroll to the Pipeline section.
  • Select Pipeline script from SCM.
  • Choose Git as the SCM and enter the repository URL.
  • Specify Jenkinsfile in the Script Path field.

Step 4: Running the Pipeline with Source Control

  1. Trigger a Build: After saving the configuration, click Build Now to trigger a build.
  2. Review the Build Logs: Jenkins will clone the repository and execute the Pipeline defined in the Jenkinsfile.

Advanced Jenkins Pipeline Features

Once you’re comfortable with the basics, you can explore advanced features to enhance your pipelines.

Parallel Execution

Execute multiple stages in parallel to speed up your build process.

pipeline {
// Define the agent where the pipeline will run. 'any' means it can run on any available agent.
agent any

// stages of the pipeline
stages {
// First stage: Build
stage('Build') {
steps {

echo 'Building...'
}
}

// Second stage: Test
stage('Test') {
// Run multiple stages in parallel to save time.
parallel {
// Sub-stage for unit tests
stage('Unit Tests') {
steps {
// a message indicate that unit tests are running.
echo 'Running unit tests...'
// Add steps to run unit tests here.
}
}
// Sub-stage for integration tests
stage('Integration Tests') {
steps {
// message to indicate that integration tests are running.
echo 'Running integration tests...'
// Add steps to run integration tests here.
}
}
}
}

// Third stage: Deploy
stage('Deploy') {
steps {
// message to indicate that the deployment process is starting.
echo 'Deploying...'
// Add deployment steps here, such as deploying to a server, updating a database, etc.
}
}
}
}

Conditional Steps

Execute steps based on certain conditions.

pipeline {
// Define the agent where the pipeline will run. 'any' means it can run on any available agent.
agent any

stages {
// First stage: Build
stage('Build') {
steps {
echo 'Building...'
}
}

// Second stage: Test
stage('Test') {
steps {
echo 'Testing...'
}
}

// Third stage: Deploy
stage('Deploy') {
steps {
script {
// Conditional deployment based on the branch name.
// If the current branch is 'main', deploy to production.
if (env.BRANCH_NAME == 'main') {
echo 'Deploying to production...'
// Add production deployment steps here.
} else {
// If the branch is not 'main', deploy to staging.
echo 'Deploying to staging...'
// Add staging deployment steps here.
}
}
}
}
}
}

Using Credentials

Securely handle sensitive information like passwords and API keys.

pipeline {
// Define the agent where the pipeline will run. 'any' means it can run on any available agent.
agent any

// Define environment variables. Here, we're securely accessing a credential stored in Jenkins.
environment {
SECRET = credentials('my-secret-id') // 'my-secret-id' is the ID of the credential stored in Jenkins.
}

stages {
// First stage: Build
stage('Build') {
steps {
echo 'Building...'
}
}

// Second stage: Test
stage('Test') {
steps {
echo 'Testing...'
}
}

// Third stage: Deploy
stage('Deploy') {
steps {
// Note: Be careful with echoing secrets in real-world pipelines to avoid exposing sensitive information.
echo "Deploying with secret: ${env.SECRET}"
}
}
}
}
  • Agent Declaration: Clearly explained that ‘any’ means the pipeline can run on any available agent.
  • Environment Variables: comments to describe how the credentials function securely retrieves the credential.
  • Stages: describe what should be done in each phase (Build, Test, Deploy).
  • Steps: suggest where additional steps related to build, test, and deployment should be added.
  • Security Note: be cautious when echoing sensitive information.

Conclusion

Jenkins pipelines provide a robust framework for defining and automating your CI/CD workflows. By mastering the basics and exploring advanced features, you can optimize your build, test, and deployment processes, ensuring faster and more reliable software delivery.

Start experimenting with Jenkins pipelines today and unlock the full potential of your CI/CD practices. Whether you’re a beginner or an experienced DevOps engineer, there’s always more to learn and improve.

Happy Pipelining! 🚀

--

--

DevOps/Cloud Engineer/1xAWS Certified/Azure/Scrum Certified/Terraform/Docker/Kubernetes/Jenkins/SVM/CI-CD/Python/Shell