Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: Jenkins Declarative Pipeline with Docker

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 trainwithshubham/django-app:latest'
            }
        }
    }

Task-01

  • Create a docker-integrated Jenkins declarative pipeline

  • Use the above-given syntax using sh inside the stage block.

pipeline {
    agent { label 'dev-agent' }

    stages{
        stage('Code'){
            steps{
                git url: 'https://github.com/gsbarure/node-todo-cicd.git', branch: 'master' 
            }
        }
        stage('Build and Test'){
            steps{
                sh 'docker build . -t gajananbarure/node-todo-app-cicd:latest'
            }
        }
        stage('Login and Push Image'){
            steps {
                echo 'login in docker hub and pushing'
                withCredentials([usernamePassword(credentialsId:'dockerhub', passwordVariable:'dockerhubpassword', usernameVariable:'dockerhubuser')]) {
                 sh "docker login -u ${env.dockerhubuser} -p ${env.dockerhubPassword}"
                 sh 'docker push gajananbarure/node-todo-app-cicd:latest'
                }
            }
        }
        stage('Deploy'){
            steps{
                sh "docker-compose down && docker-compose up -d"
            }
        }
}
    }

Task-02

  • Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

  • Complete your previous projects using this Declarative pipeline approach.

  • Dockerfile:

Build Script:Groovey Syntax

Github webhook for automated changes in code.

Pushed docker image to dockerhub under same script.

Output for all the Build stages:

Thank you for reading!! Hope you find this helpful.

#day27#90daysofdevops#devopscommunity#

Always open for suggestions..!!