Jenkins Declarative CI/CD pipeline using groovy syntax and Docker-hub
In the project "django-note-cicd," a Jenkins pipeline has been set up using an Amazon EC2 instance. This pipeline is configured to clone code directly from a GitHub repository. The connection between GitHub and the server is established through a webhook. This webhook serves as the bridge enabling GitHub to communicate with the server. When changes are made to the code in the GitHub repository, the webhook automatically triggers the Jenkins pipeline on the EC2 instance. This setup allows for seamless automation of code integration and deployment, ensuring that the latest code changes are efficiently deployed to the environment.
Launch an EC2 Instance on AWS:
Sign in to AWS Console.
Go to the EC2 dashboard.
Launch an instance using an Amazon Linux 2 AMI, t2.micro type, and default settings.
Create or select a key pair for SSH access.
Connect to the EC2 Instance:
- Use SSH to connect to the instance using your private key.
Install Java:
Amazon Linux 2 typically has OpenJDK preinstalled.
If needed, install Java using the package manager:
sudo apt update sudo apt install fontconfig openjdk-17-jre java -version
Install Jenkins:
Add Jenkins repository and GPG key.
Install Jenkins and start the service:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins
Access Jenkins:
Jenkins runs on port 8080.
Access Jenkins in a web browser using your instance's IP address.
Retrieve the admin password from the instance's file:
/var/lib/jenkins/secrets/initialAdminPassword
.Follow on-screen instructions to complete setup.
Now, you have a running EC2 instance with Java and Jenkins installed.
once jenkins is installed propoerly go to public ip_address of ec2 instance:8080 eg. 15.156.80.21:8080.
It will look like this. then go to new item and create job. enter the name note2-app-cicd and select Pipeline and ok.
Configure the project according to the requirements:
Add git hub url: clone the git-hub code
Add github hook trigger : A webhook is a mechanism for automatically triggering actions or events in one system when a specific event occurs in another system. It enables real-time communication and data exchange between different web applications or services over the internet. Webhooks are commonly used for various purposes, such as automation, integration, and event-driven workflows. webhooks are a powerful mechanism for enabling real-time communication and automation between different systems and services, improving efficiency and enhancing the capabilities of web applications and APIs.
Go to github > settings > webhooks > payload url :
add the ip_address of server http://15.156.80.21:8080/github-webhook/
When you see the checkmark, it indicates that the webhook has been successfully activated.
Write a pipeline:
pipeline {
agent any
stages {
stage("code") {
steps {
echo "code cloned"
git url: "https://github.com/Pardeep32/django-notes-app.git", branch: "main"
}
}
stage("build") {
steps {
echo "building the image"
sh "docker build . -t my-note-app"
}
}
stage("push to docker hub") {
steps {
echo "pushing the image to Docker Hub"
withCredentials([usernamePassword(credentialsId: "docker-hubid", passwordVariable: "dockerHubPass", usernameVariable: "dockerhubuser")]) {
sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
sh "docker login -u \$dockerhubuser -p \$dockerHubPass"
sh "docker push ${env.dockerHubUser}/my-note-app"
}
}
}
stage("deploy") {
steps {
echo "deploye the container"
sh "docker-compose down && docker-compose up -d"
}
}
}
}
pipeline {
: Starts defining a Jenkins pipeline.agent any
: Specifies that the pipeline can be run on any available agent (Jenkins worker node).stages {
: Begins defining stages within the pipeline.stage("code") {
: Starts the "code" stage.steps {
: Begins defining steps within the "code" stage.echo "code cloned"
: Prints the message "code cloned" to the console.git url: "
https://github.com/Pardeep32/django-notes-app.git
", branch: "main"
: Clones the Git repository located at the specified URL and branch.
}
: Ends the steps section for the "code" stage.
stage("build") {
: Starts the "build" stage.steps {
: Begins defining steps within the "build" stage.echo "building the image"
: Prints the message "building the image" to the console.sh "docker build . -t my-note-app"
: Executes a shell command to build a Docker image tagged as "my-note-app."
}
: Ends the steps section for the "build" stage.
stage("push to docker hub") {
: Starts the "push to docker hub" stage.steps {
: Begins defining steps within the "push to docker hub" stage.echo "pushing the image to Docker Hub"
: Prints the message "pushing the image to Docker Hub" to the console.withCredentials([...]) { ... }
: Allows you to use credentials securely. This block defines Docker Hub credentials for authentication.sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
: Tags the Docker image with a new name that includes the Docker Hub username and the "latest" tag.sh "docker login -u \$dockerhubuser -p \$dockerHubPass"
: Logs in to Docker Hub using the provided credentials.sh "docker push ${env.dockerHubUser}/my-note-app"
: Pushes the Docker image to Docker Hub.
}
: Ends the steps section for the "push to docker hub" stage.
stage("deploy") {
: Starts the "deploy" stage.steps {
: Begins defining steps within the "deploy" stage.echo "deploye the container"
: Prints the message "deploye the container" to the console.sh "docker-compose down && docker-compose up -d"
: Uses Docker Compose to stop and then start containers defined in adocker-compose.yml
file.
}
: Ends the steps section for the "deploy" stage.
}
: Ends the stages section for the pipeline.
This Jenkins pipeline consists of four stages: "code," "build," "push to docker hub," and "deploy." Each stage has specific steps that are executed when that stage is run as part of the Jenkins job. The pipeline typically clones code, builds a Docker image, pushes it to Docker Hub, and deploys containers using Docker Compose.
Then save and build now. Build is start in four steps:
four steps of build is completed:
When you've connected GitHub to your server through a webhook, any commits made to the GitHub repository will automatically trigger the execution of a predefined pipeline or set of commands. This ensures that specific actions are taken in response to code changes without manual intervention.
Create a Jenkinsfile in GitHub:
Create a new file in your GitHub repository's root directory and name it "Jenkinsfile."
Edit the Jenkinsfile and paste the entire Groovy syntax for your pipeline into it. Your Jenkinsfile should define the stages and steps of your pipeline.
Here's an example Jenkinsfile:
groovyCopy codepipeline {
agent any
stages {
stage("code") {
steps {
echo "code cloned"
git url: "https://github.com/Pardeep32/django-notes-app.git", branch: "main"
}
}
stage("build") {
steps {
echo "building the image"
sh "docker build . -t my-note-app"
}
}
stage("push to docker hub") {
steps {
echo "pushing the image to Docker Hub"
withCredentials([usernamePassword(credentialsId: "docker-hubid", passwordVariable: "dockerHubPass", usernameVariable: "dockerhubuser")]) {
sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
sh "docker login -u \$dockerhubuser -p \$dockerHubPass"
sh "docker push ${env.dockerHubUser}/my-note-app"
}
}
}
stage("deploy") {
steps {
echo "deploye the container"
sh "docker-compose down && docker-compose up -d"
}
}
}
}
Configure Jenkins:
In your Jenkins server, create a new Pipeline job.
In the job configuration, under "Pipeline," select "Pipeline script from SCM."
Choose "Git" as the SCM (Source Code Management) and provide the GitHub repository URL where your Jenkinsfile is stored.
Specify the branch (e.g., "main") that contains the Jenkinsfile.
Save the job configuration.
Trigger the Pipeline:
When you commit changes to the Jenkinsfile in your GitHub repository, the webhook will automatically trigger the Jenkins job.
Jenkins will fetch the Jenkinsfile from GitHub and execute the pipeline defined in it.
With this setup, your Jenkins pipeline configuration is stored in your GitHub repository, and Jenkins automatically fetches and runs the pipeline whenever changes are made to the Jenkinsfile. This approach simplifies pipeline management and allows you to version-control your pipeline alongside your code.
he step "Declarative:checkout SCM" is a common step used in Jenkins pipelines to retrieve source code from a version control system (SCM) repository, such as Git or Subversion. This step is responsible for checking out the code from the specified SCM repository and making it available for subsequent stages and steps in the pipeline.
Here's what this step does:
Checkout SCM: This step instructs Jenkins to fetch the source code from the defined SCM repository, typically based on the configuration provided in the pipeline script or job configuration.
For Git, it clones the Git repository.
For Subversion, it performs an SVN checkout.
Workspace: Jenkins creates a workspace for the build, where it stores the checked-out code and where subsequent build steps will be executed.
Availability: The code is made available to other stages and steps in the pipeline. This means that any following steps in your pipeline can access and work with the code that has been checked out.
In summary, the "Declarative:checkout SCM" step is an essential starting point in a Jenkins pipeline. It ensures that the pipeline has access to the source code needed for the build or deployment process.
I hope you found this article helpful.
Thank you!