Day 36: Jenkins end to end project - Part:2

Day 36: Jenkins end to end project - Part:2

To be Continued

Quick Recap:

In the last session, we saw the below topics:

  1. EC2 Setup: Configure Master and Agent EC2 instances.

  2. Secure Connection: Link instances using SSH keys.

  3. Install Java & Jenkins: Set up Java and Jenkins on Master.

  4. Master-Agent Link: Connect Jenkins Master with an Agent.

  5. Pipeline Creation: Design a CI/CD pipeline.

  6. GitHub Webhooks: Integrate automated triggers from GitHub.

  7. Stage View: Visualize pipeline stages in Jenkins.

  8. Outcome: Achieve an efficient and automated CI/CD workflow.

Today, we'll walk you through a quick guide on how to effortlessly log in to Docker Hub using Jenkins and smoothly push your program to the repository.


Install Plugins

  1. As we need to log in to Docker Hub using Groovy scripts, it is important to install the "Environment Injector" plugin. This plugin is used to inject or set env. variables into the build env. of Jenkins job

  2. In Jenkins, begin by accessing the Dashboard and then selecting "Manage Jenkins" from the menu on the left.

  3. Next, navigate to "Manage Plugins" and choose the "Available" tab.

  4. Search for the "Environment Injector" plugin, install it and then restart Jenkins.

Create Credentials

Here's a concise guide on creating credentials in Jenkins:

  1. Manage Credentials: Click "Manage Jenkins" in the left menu.

  2. Add Credentials: Choose either "Global credentials (unrestricted)" or "Add Credentials" based on your needs.

  3. Select Credential Type: Pick the type of credential (e.g., "Username with password" ).

  4. Provide Details: Fill in the required information, like username, password, or sensitive text.

  5. Optional Description: Add a description for reference if needed.

  6. Save: Click "OK" or "Add" to securely store the credential.

By following these steps, you can handle sensitive information securely within your CI/CD pipeline.

Accessing DockerHub via Groovy

To log into Docker Hub using Jenkins Groovy syntax, we can utilize the installed earlier "Env injector" plugin. This plugin allows you to log into DockerHub directly within your pipeline script. Here's an example of how to log in to Docker Hub using Jenkins Groovy syntax with the Docker Pipeline plugin:

DockerHub Logged In

We have been able to log into to docker hub using the above script:

Tagging and Pushing

Using the 3rd stage (i.e. Login & Push Image), we can now log in, tag and push image to docker hub successfully:

stage('Login & Push Image') {
            steps {
                withCredentials([usernamePassword(credentialsId: "dockerhub" ,passwordVariable:"Pass" ,usernameVariable: "User")]){
                    sh "docker login -u ${env.User} -p ${env.Pass}"
                    sh "docker tag node-todo-app ${env.User}/node-todo-app:latest"
                    sh "docker push ${env.User}/node-todo-app:latest"
                }    
            }
        }

Achieving a feat

We have finally been able to log in to DockerHub and push the image successfully.


Conclusion:

In this project, we harnessed Jenkins to streamline our development process. Through Jenkins jobs, we seamlessly retrieved files from GitHub, empowered by a dedicated node agent for efficient execution. Orchestrating a well-crafted pipeline, we compiled code, generated pristine Docker images, and utilized Groovy syntax to securely log into Docker Hub, seamlessly pushing our creations.

Hope you like my post. Don't forget to like, comment, and share.