Building a CI/CD Pipeline with Jenkins, Docker, and GitHub on a Linux Server: A Hands-On Guide
Shubham Niranjan
NOC Engineer | ?? AWS | ?? Linux | ?? Docker | ?? Kubernetes | ?? Jenkins | ?? Git & GitHub | ?? Python | ??? CI/CD | ?? Cloud Infrastructure | ?? Automation | ?? Monitoring & Logging | ?? DevOps Enthusiast
In today's fast-paced development environment, Continuous Integration and Continuous Deployment (CI/CD) have become essential for delivering high-quality software efficiently. If you're a DevOps enthusiast like me, you’ll appreciate the beauty of automating the software release process. Today, I’ll walk you through building a CI/CD pipeline using Jenkins, Docker, and GitHub on a Linux server. Let’s dive right in!
Step 1: Setting Up Your Environment
Creating an Ubuntu VM on Azure
First things first, we need a Linux environment to host our Jenkins server. Azure makes it easy to spin up a virtual machine:
Log into the Azure Portal: Head to Azure Portal and log in with your Microsoft account.
Create a Virtual Machine
Networking:Ensure SSH (port 22) is open for remote access.Enable HTTP (port 80) and HTTPS (port 443) if you plan to host a web application.
Review and Create: Double-check your settings and hit Create.
Step 2: Connecting to Your VM
Once your VM is up, connect to it using PuTTY (if you’re on Windows):
Step 3: Preparing Your Server
Installing Docker
Docker is key to our pipeline for containerizing applications:
Reboot your VM to apply the changes: sudo reboot.
Installing Jenkins
Now, let’s set up Jenkins:
Add Jenkins' official repository and its key:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
Restart the VM: sudo reboot.
Step 4: Setting Up Your Pipeline
Accessing Jenkins
Creating a Jenkins Pipeline
领英推荐
Step 5: Building and Deploying with Docker
Configuring the Jenkinsfile
In your GitHub repository, create a Jenkinsfile:
pipeline {
agent any
stages {
stage('Clone') {
steps {
git url: 'https://github.com/username/repository.git', branch: 'main'
}
}
stage('Build') {
steps {
sh 'docker build -t my-note-app .'
}
}
stage('Push') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', passwordVariable: 'dockerHubPass', usernameVariable: 'dockerHubUser')]) {
sh 'docker login -u $dockerHubUser -p $dockerHubPass'
sh 'docker tag my-note-app $dockerHubUser/my-note-app:latest'
sh 'docker push $dockerHubUser/my-note-app:latest'
}
}
}
stage('Deploy') {
steps {
sh 'docker-compose down && docker-compose up -d'
}
}
}
}
Step 6: Automating with GitHub Webhooks
Set up a webhook in GitHub to trigger your pipeline on code changes:
Step 7: Monitoring and Running Your Application
After setting everything up, every code push to GitHub will trigger Jenkins, which will build, push, and deploy your Dockerized application. Access your app at https://<YOUR_VM_IP>:8000.
Conclusion
By following these steps, you’ve built a robust CI/CD pipeline that automates your development workflow, from code integration to deployment. This hands-on approach with Jenkins, Docker, and GitHub not only streamlines the process but also ensures consistent and reliable software delivery. Happy DevOps-ing!