Day 26 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 moudekarvivek/django-app:latest'
}
}
}
✔Task-01:Create a Docker-Integrated Jenkins Declarative Pipeline
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
📍For Jenkins Installation You need to Install Java.
sudo apt update #update index file
sudo apt install fontconfig openjdk-17-jre #Java Installation
📍 Install Jenkins, Docker & Docker-compose.
# This Installation command also available on official documentation
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
# Docker & Docker-Compose
sudo apt-get install docker.io docker-compose -y
📍Enable Jenkins & Docker to run them even after we restart our system
sudo systemctl enable jenkins
sudo systemctl enable docker
📍Add User and jenkins in docker group other wise while building it will through errors
sudo usermod -aG docker $USER
sudo usermod -aG docker jenkins
cat /etc/group #command use to check users are added or not
📍Go to security group, open port 8080 for Jenkins & 8000 for Docker.
📍Access Jenkins Portal using your instance's public-ip:8080.
📍Now, creating new pipeline job. Go to Create a job -> name of Project ->select Pipeline->Click OK.
📍Configure you pipeline.
📍Go to last, select Pipeline script-> write this script-> Save it.
pipeline {
agent any
stages {
stage("code") {
steps {
git url: "https://github.com/moudekarvivek/node-todo-cicd.git", branch: "master"
echo "code cloned successfully"
}
}
stage("build") {
steps {
sh 'docker build . -t todo-app'
echo "code build successfully"
}
}
stage("deploy") {
steps {
sh "docker run -p 8000:8000 -d todo-app"
echo "Node-app deployed successfully"
}
}
}
}
Click on Build Now.
📍On First Click your Pipeline runs successfully.
📍But you will face errors(port no. 8000 is already allocated) in case of running a job twice, as the docker container will be already created.
For that kill the container first and then build it will work
✔Task 2: Enhance Pipeline with Docker Groovy Syntax
- Create a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.
📍Now you just go back to the configuration of your pipeline job.
📍Modify the Pipeline Script.
pipeline {
agent any
stages {
stage("code") {
steps {
git url: "https://github.com/moudekarvivek/node-todo-cicd.git", branch: "master"
echo "code cloned successfully"
}
}
stage("build and test"){
steps{
sh "docker build -t node-app-test-new ."
echo 'Code Build Successfully'
}
}
stage("deploy") {
steps {
sh "docker-compose down && docker-compose up -d"
echo "Node-app deployed successfully"
}
}
}
}
Congratulations, your pipeline is running successfully.🎊
🚧Conclusion
In conclusion, using a declarative CI/CD pipeline with Docker commands in Groovy syntax offers a efficient way to automate your build, test, and deployment processes. By defining your pipeline stages and steps within a Jenkinsfile, you can easily manage and version control your CI/CD workflow.
Happy Learning :)