Back to Blog

Jenkins: Automation and CI/CD

May 10, 2025

Jenkins is a powerful open-source automation server widely used for continuous integration (CI) and continuous delivery (CD). It allows development teams to automate repetitive tasks in the software delivery lifecycle, including builds, tests, and deployments.


📑 Table of Contents

  1. What is Jenkins?
  2. Installing Jenkins
  3. Jenkins Pipeline
  4. Jenkinsfile
  5. Plugins and Extensibility
  6. Agents and Executors
  7. Triggers and Automation
  8. Build Stages and Steps
  9. Integration with Git
  10. Running Jenkins with Docker Agents
  11. Security and Credentials
  12. Using Environment Variables
  13. Plugin Ecosystem
  14. Parallel Stages
  15. Reusability and Shared Libraries
  16. Notifications
  17. Monitoring and Observability
  18. CI/CD Best Practices with Jenkins
    Conclusion
    Further Reading & Official Resources


1. What is Jenkins?

Jenkins is an automation server that facilitates the continuous integration and continuous delivery (CI/CD) process. It helps automate the non-human part of software development:

Key Goals:

  • Continuous Integration: Detect integration issues early by frequently merging code into a shared repository and running automated tests.
  • Continuous Delivery: Automate deployment steps to deliver software more reliably and quickly.
  • Monitoring Pipelines: Keep track of code quality, test results, and build health with visual dashboards.

It supports a wide range of tools and plugins for integration with virtually every component in your development lifecycle.

Main benefits:

  • Pipeline-as-code
  • Extensible via plugins
  • Open-source and community-driven
  • Supports distributed builds


2. Installing Jenkins

Jenkins can be installed on:

  • Linux (Debian, RedHat, etc.)
  • macOS and Windows
  • Docker containers
  • Kubernetes clusters
  • Cloud platforms (via Helm charts or AMIs)

Example: Ubuntu Installation

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo apt-add-repository https://pkg.jenkins.io/debian-stable
sudo apt update
sudo apt install jenkins

Start and enable the service:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Alternatively, run with Docker:

docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

➡️ Jenkins UI will be available at http://localhost:8080


3. Jenkins Pipeline

A Pipeline is the set of steps that Jenkins uses to build and deliver software.

Two types:

  • Declarative: YAML-like syntax, more readable and standardized.
  • Scripted: Groovy-based, more powerful but verbose.

Example declarative pipeline:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying...'
      }
    }
  }
}


4. Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins pipeline.

✅ Benefits:

  • Version-controlled CI logic
  • Reusability
  • Consistency across branches

Place this file at the root of your project. Jenkins will detect and use it automatically.

Use multibranch pipelines to detect Jenkinsfiles automatically in feature branches.


5. Plugins and Extensibility

Jenkins features a vast ecosystem of plugins, enabling:

  • SCM integration (GitHub, Bitbucket, GitLab)

  • Notification systems (Slack, Email)

  • QA tools (SonarQube, Selenium)

  • Deployment tools (Docker, Kubernetes, Ansible)

💡 Use the Plugin Manager to install/update plugins from the UI.


6. Agents and Executors

Jenkins follows a master-agent architecture:

  • Master (or controller): Schedules jobs, dispatches builds.

  • Agent (or worker): Executes tasks.

  • Executors: Threads on agents used to run builds concurrently.

  • Agents can be configured as static (dedicated machines) or dynamic (on-demand via Docker, Kubernetes).


7. Triggers and Automation

Jenkins can automatically trigger jobs via:

  • Webhooks from Git providers (e.g., GitHub push)

  • Scheduled jobs using cron syntax

  • Manual triggers (via UI or REST API)

  • Upstream/downstream job completion

Example GitHub Push trigger:

triggers {
  githubPush()
}

Or on a schedule:

triggers {
  cron('H 4 * * 1-5') // Every weekday at 4 AM
}


8. Build Stages and Steps

Use logical stages to separate your pipeline into functional steps.

Each stage contains one or more steps:

stage('Install dependencies') {
  steps {
    sh 'npm ci'
  }
}

Stages help visualize your build process in the Jenkins UI and isolate failures.


9. Integration with Git

Jenkins supports all major Git platforms:

  • GitHub

  • GitLab

  • Bitbucket

  • Azure Repos

To clone a Git repo:

pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git url: 'https://github.com/your-repo/project.git', branch: 'main'
      }
    }
  }
}

Use credentials securely via the credentials plugin.


10. Running Jenkins with Docker Agents

Use Docker to isolate builds:

agent {
  docker {
    image 'node:20'
  }
}

Combine with Kubernetes plugin to dynamically spawn containers on demand.


11. Security and Credentials

Jenkins supports:

  • Role-based access control (RBAC)

  • Integration with LDAP/SSO providers

  • API tokens for automation

🔐 Store secrets in Credentials Manager:

Never hardcode secrets. Jenkins allows:

  • Secure storage of secrets via Credentials Manager

  • Access secrets via bindings in pipeline code

withCredentials([usernamePassword(credentialsId: 'git-creds', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
  sh 'echo $GIT_USER && echo $GIT_PASS'
}

Other types: API tokens, SSH keys, secret files, certificates.


12. Using Environment Variables

Set environment variables globally or per stage:

environment {
  NODE_ENV = 'production'
  DEPLOY_PATH = '/var/www'
}

They simplify configuration and reduce duplication.


13. Plugin Ecosystem

The Plugin Manager gives access to 1800+ plugins:

Popular plugins:

  • GitHub — SCM support

  • Docker Pipeline — Build & run containers

  • Pipeline: Multibranch — Detect Jenkinsfiles per branch

  • Blue Ocean — Modern UI for pipelines

  • Slack Notification — Notify teams

  • JUnit — Test reporting

Plugins make Jenkins adaptable to virtually any workflow.


14. Parallel Stages

Improve efficiency by running stages in parallel:

stage('Tests') {
  parallel {
    stage('Unit') {
      steps { sh 'npm test' }
    }
    stage('Integration') {
      steps { sh 'npm run test:int' }
    }
  }
}

Useful for large test suites or builds targeting multiple environments.


15. Reusability and Shared Libraries

Use Shared Libraries to avoid duplicating logic across pipelines (reuse steps across multiple projects).

@Library('my-shared-lib') _
myCustomStep()

Project structure:

/vars/myCustomStep.groovy
/src/org/mycompany/MyClass.groovy

Great for large organizations or monorepos.


16. Notifications

Notify teams of build results:

  • Slack

  • Email

  • Microsoft Teams

  • Discord

slackSend channel: '#ci', message: 'Build complete!'


17. Monitoring and Observability

Track job metrics with plugins:

  • Build Monitor View

  • Monitoring Plugin

  • Prometheus Plugin — Expose metrics

  • Email Extension Plugin — Custom alerts

Integrate with Grafana or Datadog for visibility.


18. CI/CD Best Practices with Jenkins

✅ Use declarative pipelines

✅ Keep stages focused and atomic

✅ Run tests early and in isolation

✅ Use Docker for reproducibility

✅ Monitor builds with alerts

✅ Review Jenkinsfiles like code

✅ Clean up stale jobs and workspaces

✅ Backup Jenkins home regularly


Conclusion

Jenkins empowers teams to automate every step of software delivery. With pipeline-as-code, extensible plugins, and integration with Git, Docker, and cloud tools, Jenkins remains one of the most trusted CI/CD platforms.


Further Reading & Official Resources

Here are some reliable sources to learn more about Jenkins and the concepts covered: