Day 27 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start the 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'
}
}
}
stages {
stage('Run') {
steps {
sh 'docker run -d trainwithshubham/django-app:latest'
}
}
}
Task-01
Create a docker-integrated Jenkins declarative pipeline
Use the above-given syntax using
sh
inside the stage blockYou will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2.
Step 1: Create a New Pipeline Job
Open Jenkins and navigate to the dashboard.
Click on "New Item" to create a new pipeline job.
Step 2: Configure Your Pipeline Script
In the job configuration, scroll down to the "Pipeline" section.
Choose "Pipeline script" from the Definition dropdown.
In the script box, enter the following:
pipeline {
agent any
stages {
stage("Code"){
steps{
git url: "https://github.com/Kritika257/node-todo-cicd.git", branch:"master"
echo "Code Clone"
}
}
stage("Build & Test"){
steps{
sh "docker build -t node-app:latest ."
echo "Code Build & Test"
}
}
stage("Scan"){
steps{
echo "Code scan completed !!!"
}
}
stage("Run"){
steps{
sh "docker run -d -p 8000:8000 node-app"
echo "Run Completed"
}
}
}
}
Save and Run Your Job
Save your configuration.
Run your Jenkins job.
Check whether the application is working on port 8000 or not.
- You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2.
Task-02
- Create a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.
Update Your Pipeline Script
Open the configuration of the same job you created for Task-01.
Update the script box with the following:
pipeline {
agent any
stages {
stage("Code"){
steps{
git url: "https://github.com/Kritika257/node-todo-cicd.git", branch:"master"
echo "Code Clone"
}
}
stage("Build & Test"){
steps{
sh "docker build -t node-app:latest ."
echo "Code Build & Test"
}
}
stage("Scan"){
steps{
echo "Code scan completed !!!"
}
}
stage("Push"){
steps{
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]) {
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
sh " docker tag node-app:latest ${env.dockerHubUser}/node-app:latest"
sh " docker push ${env.dockerHubUser}/node-app:latest"
echo "Code pushed to the DockerHub"
}
}
}
stage("Deploy"){
steps{
sh "docker-compose down"
sh " docker-compose up -d"
}
}
}
}
Save and Run Your Job
Save your configuration.
Run your Jenkins job again.
That's it! You've just completed the task. π
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)