Jenkins Project with Declarative Pipeline
For Jenkins Installation see my blog at below link:
react-django-to-do-app-using-jenkins
- Select 'Create a job'
- Select Pipeline
Select GitHub project
-
Select GitHub hook trigger for GITScm polling
Pipeline script in Groovy Syntax
-
pipeline {
agent any
stages{
stage("Code"){
steps{
git url: "https://github.com/rushikesh-rawool10/node-todo-cicd.git", branch: "master"
}
}
stage("Build & Test"){
steps{
sh "docker build . -t node-app-test-new"
}
}
stage("Push to DockerHub"){
steps{
withCredentials([usernamePassword(credentialsId:"DockerHub",passwordVariable:"DockerHubPassword",usernameVariable:"DockerHubUser")]){
sh "docker login -u ${env.DockerHubUser} -p ${env.DockerHubPassword}"
sh "docker tag node-app-test-new ${env.DockerHubUser}/node-app-new:latest"
sh "docker push ${env.DockerHubUser}/node-app-new:latest"
}
}
}
stage("Deploy"){
steps{
sh "docker-compose down && docker-compose up -d"
}
}
}
}
- GitHub Code URL: https://github.com/rushikesh-rawool10/node-todo-cicd.git
Clone this code to the local server...
git clone https://github.com/rushikesh-rawool10/node-todo-cicd.git
- How to create credentials in Jenkins
i> Go to manage Jenkins
ii> In the Security section, select Credentials.
iii> Select global
iv> Add Credentials
v>Provide username, password, id
- First, we can check whether our code is running fine or not by manually using the delivery.
Before deploying your code make sure of the following points:
Docker and docker-compose should be installed on your machine.
Your Jenkins user should be added to the docker group.
port 8000 (the port on which app will run)should be open from the ec2 instance.
Jenkins Pipeline using a script is running fine...
To make it a fully Deployment project we can use 'GitHub hook trigger for GITScm polling' & 'pipeline script from scm'
How to set the GitHub hook trigger
Go to the current repository settings
go to Webhooks
Add webhook
Provide Payload URL like below:
Webhook is now activated...
As soon we make any changes to the GitHub code, the pipeline will triggered automatically, which is Jenkins Deployment.
Here we go...!
Node app using Jenkins declarative Pipeline is Ready...!!!
Subscribe to my newsletter
Read articles from Rushikesh Rawool directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rushikesh Rawool
Rushikesh Rawool
Passionate ORACLE DATABASE ADMINISTRATOR | DevOps Enthusiast with a strong background in cloud architecture and solutions engineering.