Skip to main content

Command Palette

Search for a command to run...

Automating CI/CD with Jenkins: A Dockerized Approach

Published
2 min read
Automating CI/CD with Jenkins: A Dockerized Approach
M

Hey there! I'm currently working as an Associate DevOps Engineer, and I'm diving into popular DevOps tools like Azure Devops,Linux, Docker, Kubernetes,Terraform and Ansible. I'm also on the learning track with AWS certifications to amp up my cloud game. If you're into tech collaborations and exploring new horizons, let's connect!

In today's fast-paced world of software development, the need for automation in building, testing, and deploying applications is more critical than ever. Continuous Integration (CI) and Continuous Deployment (CD) pipelines play a key role in streamlining these processes. In this blog post, we'll explore a Jenkins pipeline written in Groovy that leverages Docker to automate these tasks seamlessly.

Jenkins Pipeline Overview:

Our Jenkins pipeline consists of several stages, each serving a specific purpose:

  1. Code Stage:

     groovyCopy codestage('code') {
         steps {
             git url: 'https://github.com/muhammadhassanb111/node-todo-cicd.git', branch: 'master'
         }
     }
    

    In this stage, we fetch the latest code from our GitHub repository, ensuring that we're always working with the most up-to-date version of our application.

  2. Build and Test Stage:

     groovyCopy codestage('build and test') {
         steps {
             script {
                 echo '=== Building Docker Image ==='
                 sh 'docker build . -t hassanb111/node-todo-app:latest'
                 echo '=== Docker Image Built ==='
    
                 echo '=== Docker Images List ==='
                 sh 'docker images'
                 echo '=== End of Docker Images List ==='
             }
         }
     }
    

    In this stage, we build a Docker image from our application's Dockerfile. The process is accompanied by informative messages, and we list the Docker images for visibility.

  3. Login and Push Images Stage:

     groovyCopy codestage('login and push images') {
         steps {
             echo 'Logging into Docker Hub'
             withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerhubpassword', usernameVariable: 'dockerhubuser')]) {
                 sh "docker login -u ${env.dockerhubuser} -p ${env.dockerhubpassword}"
                 sh "docker push hassanb111/node-todo-app:latest"
             }
         }
     }
    

    This stage handles the secure login to Docker Hub using Jenkins credentials and subsequently pushes the Docker image to the Docker Hub repository.

  4. Deploy Stage:

     groovyCopy codestage('deploy') {
         steps {
             script {
                 echo '=== Stopping and Removing Containers ==='
                 sh 'docker-compose down'
                 echo '=== Containers Stopped and Removed ==='
    
                 echo '=== Starting Containers ==='
                 sh 'docker-compose up -d'
                 echo '=== Containers Started ==='
             }
         }
     }
    

    The final stage involves stopping and removing existing Docker containers and then initiating fresh containers using Docker Compose.

By breaking down our CI/CD process into these stages, we ensure a systematic and automated approach to software development. This Jenkins pipeline, combined with Docker, provides a powerful foundation for building, testing, and deploying applications efficiently.

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.