Day 27 Task: Jenkins Declarative Pipeline with Docker
Table of contents
🔹Create Declarative Pipeline with Docker
From the Jenkins dashboard, click on "New Item" to create a new project.
Enter a project name and select "Pipeline" Click "OK" to create the project.
Under Source Code Management, select Git and Added the repository URL (my forked repository).
Enabled GitHub hook trigger for GITScm polling.
GitHub WebHooks Setup:
Accessed my repository on GitHub.
Went to "Settings" > "Webhooks".
Added a new webhook with my Jenkins job's URL Selected events to trigger (e.g., push, pull request).
Saved the webhook.
Create Environment variable credential for docker hub:
Click on "manage jenkins" > Credentials.
Click on "Systum" > "Global Credentials(unrestricted)" > "Add Credentials".
Running the Application:
Add the current user to the
docker
group:sudo usermod -aG docker $USER sudo reboot
Add the Jenkins user to the
docker
group:sudo usermod -aG docker jenkins sudo systemctl restart jenkins
Inside the Dashboard "todo-app" job > Within the Jenkins job's Pipeline build step.
pipeline { agent any stages{ stage('Clone Code'){ steps { echo "Cloning the code" git url:"https://github.com/GOPALGTM/node-todo-app.git", branch:"main" } } stage('Build'){ steps { echo "Building the Image" sh "docker build -t todo-app ." } } stage('push to dockerhub'){ steps { echo "Pushing the code to dockerhub" withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){ sh "docker tag todo-app ${env.dockerHubUser}/node-app:latest" sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}" sh "docker push ${env.dockerHubUser}/node-app:latest" } } } stage('Deploy'){ steps { echo "Deploying the application" sh "docker-compose down" sh "docker-compose up -d --build" } } } }
Save and click on Build Now.