Day 19 Task: Jenkins Declarative Pipeline with Docker
Day 18 was all about a Declarative pipeline, now its time to level up things, let's integrate Docker and your Jenkins declarative pipeline
Use your Docker Build and Run Knowledge
docker build -*you can use sh 'docker build . -t <tag>' in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.*
docker run:*you can use sh 'docker run -d <image>' in your pipeline stage block to build the container.*
How will the stages look
stages {
stage('Build') {
steps {
sh 'docker build -t trainwithshubham/django-app:latest'
}
}
}
Task-01
Create a docker-integrated Jenkins declarative pipeline
Use the above-given syntax using
sh
inside the stage blockCreate a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.
Steps:
1 ) Create a New Job item, in Jenkins
2 In the Advanced project Option
pipeline {
agent any
stages {
stage('Code') {
steps {
git url: 'https://github.com/ApurvDevops/node-todo-cicd.git', branch: 'master'
echo 'Code clone gya'
}
}
stage('Build') {
steps {
sh "docker build -t node-app-test-new ."
echo 'build ho gya'
}
}
stage('Push Image') {
steps {
withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubpass",usernameVariable:"dockerhubuser")]){
sh "docker login -u${env.dockerhubuser} -p${env.dockerhubpass}"
sh "docker tag node-app-test-new ${env.dockerhubuser}/node-app-test-new:latest"
sh "docker push ${env.dockerhubuser}/node-app-test-new:latest"
echo 'Push ho gya'
}
}
}
stage('deploy') {
steps {
sh "docker-compose down && docker-compose up -d"
echo 'deploy ho gya'
}
}
}
}
Few Important point that we must know in above scripts are
π Declarative pipeline only understand groovy script so if you only write simple docker command in the scripts it wont work, you always need to find the groovy equivalent of the docker command
eg : sh "docker build -t node-app-test-new ." , using "sh"
in front of the docker command it understands that it is a groovy syntax
π We never share the credentials in the scripts, it is always used in the form of variable that is stored in the in Manage Jenkins - > Credentials - > System (Global)
We always need to store credential here and use it from here in our Pipeline
π In the above Script we have used the Docker hub credentials in same way withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubpass",usernameVariable:"dockerhubuser")])
π withCredentials
is kind of a Function that is used to call the credentials in our code that has scope defined in braces ()
π usernamePassword
binding for Docker Hub credentials.
π credentialsId
refers to the unique identifier of the stored credentials in Jenkins
π passwordVariable
and usernameVariable
specify the environment variables where the corresponding values will be stored during the execution of the pipeline.
Once above steps are done, we need to manually trigger the-pipeline and we ca
In case you face any error , you can Follow this documentation
Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the futureππ.
Subscribe to my newsletter
Read articles from Apurv Samadder directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by