Building a CI/CD Pipeline with Jenkins, Docker, and GitHub on a Linux Server: A Hands-On Guide

Building a CI/CD Pipeline with Jenkins, Docker, and GitHub on a Linux Server: A Hands-On Guide

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

  • Click on Create a resource.
  • Search for Virtual Machine and select it.
  • In the Basics tab, set your subscription, create a resource group, and give your VM a name like myUbuntuVM.
  • Choose Ubuntu 20.04 LTS as the image and select a suitable VM size (like B1s for small-scale testing).
  • For authentication, choose SSH public key, set a username (e.g., azureuser), and paste your SSH public key.

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):

  1. Open PuTTY and enter your VM’s public IP (found in the Azure portal) in the Host Name field.
  2. Load your SSH private key (.ppk file) under Connection > SSH > Auth.
  3. Save your session for future use and click Open.
  4. Log in with the username you created (azureuser), and you’re in!


Step 3: Preparing Your Server

Installing Docker

Docker is key to our pipeline for containerizing applications:

  • Update your package list: sudo apt-get update.
  • Install Docker: sudo apt install docker.io.
  • Add your user to the Docker group to avoid using sudo with every Docker command:

Reboot your VM to apply the changes: sudo reboot.

Installing Jenkins

Now, let’s set up Jenkins:

  1. Update your package list: sudo apt update.
  2. Install Java, which Jenkins requires:

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


  • Install Jenkins: sudo apt-get update && sudo apt-get install jenkins -y.
  • Ensure Jenkins can communicate with Docker by adding it to the Docker group:

Restart the VM: sudo reboot.


Step 4: Setting Up Your Pipeline

Accessing Jenkins

  • Open your browser and navigate to https://<YOUR_VM_IP>:8080.
  • Retrieve the initial admin password:

  • Complete the setup wizard, installing suggested plugins and creating an admin user.

Creating a Jenkins Pipeline

  1. Click on New Item in Jenkins, name your pipeline (e.g., notes-app-cicd), and select Pipeline.
  2. In the Pipeline section, choose Pipeline script from SCM and select Git.
  3. Enter your GitHub repository URL and branch name, and provide your GitHub credentials.


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:

  1. In your GitHub repository, go to Settings > Webhooks.
  2. Add a new webhook with the Jenkins webhook URL (https://<YOUR_VM_IP>:8080/github-webhook/).
  3. Set the content type to application/json and select Send me everything.


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!

要查看或添加评论,请登录

Shubham Niranjan的更多文章

社区洞察

其他会员也浏览了