Jenkins Live Project  with Declarative CI/CD Pipeline with Groovy syntax using env. variable

Jenkins Live Project with Declarative CI/CD Pipeline with Groovy syntax using env. variable

·

3 min read

Jenkins Pipeline là gì ? Tổng quan Jenkins Pipeline CI/CD - Technology ...

Launch an EC2 instance and install Jenkins. Use this instance as a Jenkins server using the t2-micro instance type.

ssh -i "jenkins-key.pem" ubuntu@ec2-13-232-61-41.ap-south-1.compute.amazonaws.com(SSH into the jenkins server.)

Use the code from Github "django-notes-app".

“sh” is used for Groovy syntax for shell command

Used the special syntax to use the env. variable to login

withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){

withCredentials-function, which gives the password through the ID (env. variable)

sh "docker tag my-note-app $env.dockerHubUser/my-note-app:latest"

[Tag with your username and upload it to Docker Hub with the latest version.]

sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"

Login to the docker hub with the above command

sh "docker push $env.dockerHubUser/my-note-app:latest"

This will push the code to the Docker hub.

echo "Deploying the container"

sh "docker-compose down && docker-compose up -d"

sh "docker run -d 8000:8000 anuraglovesdocker/my-note-app:latest"

docker run detached mode publish port 8000 system 8000 container

Note: Open port 8000 in NSG and use the public IP for running on the website

echo "Deploying the container"

sh "docker-compose down and docker-compose up -d"

To make the website more robust, since when we run the website, it runs on the same port 8000, which is already allocated. So we need to use Docker Compose, which will shut down the existing port 8000 and open it again.

Docker Compose is a special type of YML file. It can run multiple containers, and it will be managed by Docker Compose.

build : . --- build in the same file

It creates a service called web."

The docker build and docker run commands are managed in build: and ports:

sudo apt-get install docker-compose --to install

At the deploy stage, the docker container is down (removes previous container)

Updated

Then goes up

sh "docker-compose down and docker-compose up -d"

Add the image from the Docker Hub to the Docker compose

$ git clone the repository

$sudo apt install docker

$ sudo apt install docker-compose ( it is a special type of yml file that runs multiple containers.)

change directory and cat docker-compose.yml

Using Docker compose makes a pipeline robust as it builds and pushes it to the Docker Hub (note: you need to have an account in the Docker Hub).

Start and setup Jenkins for CI/CD integration (https://*13.232.61.41:8080 once it is allowed from the security group in AWS)

Comment installer Jenkins sur Ubuntu 16.04

Use the Pipeline script from SCM ( GitHub repository "django-notes-app/Jenkinsfile at main · Anurag2022github/django-notes-app")

Branch -main

Jenkinsfile is added, which contains the declarative pipeline (groovy syntax)

Using the env. variable hides your passwords in Groovy syntax. Use the credential ID.

Webhooks in GitHub allow developers to receive real-time notifications about events occurring within a repository. They enable the automatic triggering of actions, such as updating external systems, integrating with continuous integration and continuous deployment (CI/CD) pipelines, or sending notifications to external services.

payload URL will have the Jenkins URL,

Automates continuous deployment Any change in the GitHub code commit will change the CI/CD deployment at the end-to-end level.

Declarative checkout SCM Clone CodeBuildPush to Docker HubDeploy

http://13.232.61.41:8000/ Use the public IP of the Jenkins server with port 8000 allowed in AWS security groups (inbound rules to allow network traffic )

Congratulations !!! Your application is up and ready!

Did you find this article valuable?

Support Anurag's Blog by becoming a sponsor. Any amount is appreciated!