Build Your First CI/CD Pipeline with Jenkins: Complete Guide
Set up a complete CI/CD pipeline using Jenkins, Docker, and GitHub. Learn automated testing, building, and deployment in this practical tutorial.
Prerequisites
- โข Basic understanding of Git and GitHub
- โข Familiarity with Linux command line
- โข Basic knowledge of Docker concepts
- โข A computer with at least 4GB RAM
Understanding CI/CD and Why It Matters
CI/CD stands for Continuous Integration / Continuous Delivery (or Deployment). It automates the process of building, testing, and deploying code changes โ reducing human error and speeding up releases.
Continuous Integration: Every code commit triggers automated builds and tests. Developers merge code to a shared branch frequently (multiple times a day), catching bugs early.
Continuous Delivery: After CI, code is automatically deployed to a staging environment. Continuous Deployment goes further โ every passing build is automatically deployed to production.
Benefits: 10x faster release cycles, 90% fewer production bugs, instant rollback capability, and consistent deployments. Companies using CI/CD deploy hundreds of times per day (Netflix, Amazon).
Installing Jenkins on Linux
Jenkins is the most popular open-source CI/CD tool with 1,800+ plugins. Install on Ubuntu: sudo apt update && sudo apt install openjdk-17-jre -y (Jenkins needs Java).
Add Jenkins repository: curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc, then add the repo and install with apt.
Start Jenkins: sudo systemctl start jenkins && sudo systemctl enable jenkins. Access at http://your-server-ip:8080. Get the initial admin password: sudo cat /var/lib/jenkins/secrets/initialAdminPassword.
Complete setup wizard: Install suggested plugins, create your admin user, and configure the Jenkins URL. Install additional plugins: Docker Pipeline, GitHub Integration, Blue Ocean (modern UI).
Creating Your First Jenkins Pipeline
Jenkins uses 'Jenkinsfile' โ a text file defining your pipeline as code. This lives in your project's root directory alongside your source code, versioned with Git.
Create a Declarative Pipeline: pipeline { agent any; stages { stage('Build') { steps { sh 'echo Building...' } }; stage('Test') { steps { sh 'echo Testing...' } }; stage('Deploy') { steps { sh 'echo Deploying...' } } } }
In Jenkins UI: New Item โ Pipeline โ Name it โ Under Pipeline section, choose 'Pipeline script from SCM' โ Select Git โ Enter your repository URL โ Set branch to 'main'.
Trigger builds automatically: Configure webhooks in GitHub (Settings โ Webhooks โ Add โ Payload URL: http://jenkins-url/github-webhook/). Now every push triggers a build.
Adding Docker and Automated Testing
Install Docker on your Jenkins server: sudo apt install docker.io -y && sudo usermod -aG docker jenkins && sudo systemctl restart jenkins. This lets Jenkins build Docker images.
Add a Dockerfile to your project: FROM node:18-alpine; WORKDIR /app; COPY package*.json ./; RUN npm install; COPY . .; EXPOSE 3000; CMD ['npm', 'start'].
Update Jenkinsfile to build Docker image: stage('Build Docker Image') { steps { sh 'docker build -t myapp:${BUILD_NUMBER} .' } }. The BUILD_NUMBER ensures unique image tags.
Add testing stage: stage('Test') { steps { sh 'npm test' } post { always { junit 'test-results/*.xml' } } }. Jenkins displays test results with pass/fail trends over time.
Deploying and Monitoring Your Pipeline
Add deployment stage: Deploy to a server via SSH, push Docker image to a registry (Docker Hub, AWS ECR), or deploy to Kubernetes. Start simple with: sh 'docker run -d -p 80:3000 myapp:${BUILD_NUMBER}'.
Add post-build actions: post { success { echo 'Build succeeded!' } failure { mail to: 'team@company.com', subject: 'Build Failed' } }. Integrate with Slack for instant notifications.
Set up Jenkins agents/nodes for parallel builds. Use Docker agents for clean, reproducible build environments: agent { docker { image 'node:18' } } โ each build gets a fresh container.
Monitor your pipeline: Blue Ocean plugin provides beautiful visualization. Track build times, test coverage trends, and deployment frequency. Set up health checks for deployed applications.
Ready to Go Deeper?
This tutorial covers the basics. Join our instructor-led program for hands-on projects, certification prep, and placement assistance.