Quick Recap:
In the last session, we saw the below topics:
EC2 Setup: Configure Master and Agent EC2 instances.
Secure Connection: Link instances using SSH keys.
Install Java & Jenkins: Set up Java and Jenkins on Master.
Master-Agent Link: Connect Jenkins Master with an Agent.
Pipeline Creation: Design a CI/CD pipeline.
GitHub Webhooks: Integrate automated triggers from GitHub.
Stage View: Visualize pipeline stages in Jenkins.
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
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
In Jenkins, begin by accessing the Dashboard and then selecting "Manage Jenkins" from the menu on the left.
Next, navigate to "Manage Plugins" and choose the "Available" tab.
Search for the "Environment Injector" plugin, install it and then restart Jenkins.
Create Credentials
Here's a concise guide on creating credentials in Jenkins:
Manage Credentials: Click "Manage Jenkins" in the left menu.
Add Credentials: Choose either "Global credentials (unrestricted)" or "Add Credentials" based on your needs.
Select Credential Type: Pick the type of credential (e.g., "Username with password" ).
Provide Details: Fill in the required information, like username, password, or sensitive text.
Optional Description: Add a description for reference if needed.
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.