#90daysofdevops #28:Jenkins Declarative Pipeline with Docker

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.

Launch two EC2 Instances.

1.Jenkins Master

2.Jenkins Agent

Installing Java and Jenkins in Both Instances

https://goutamdevops.hashnode.dev/java-jenkins-installation

Jenkins Master Configuration

public_ip:8080

Setup of Jenkins_Agent

Manage credential

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

Jenkinsfile (Scripted Pipeline)

node {  
    stage('Build') { 
        // 
    }
    stage('Test') { 
        // 
    }
    stage('Deploy') { 
        // 
    }
}

Jenkinsfile (Declarative Pipeline)

pipeline { 
    agent any 
    options {
        skipStagesAfterUnstable()
    }
    stages {
        stage('Build') { 
            steps { 
                sh 'make' 
            }
        }
        stage('Test'){
            steps {
                sh 'make check'
                junit 'reports/**/*.xml' 
            }
        }
        stage('Deploy') {
            steps {
                sh 'make publish'
            }
        }
    }
}


pipeline
is Declarative Pipeline-specific syntax that defines a "block" containing all content and instructions for executing the entire Pipeline.

agent is Declarative Pipeline-specific syntax that instructs Jenkins to allocate an executor (on a node) and workspace for the entire Pipeline.

stage is a syntax block that describes a stage of this Pipeline. Read more about stage blocks in Declarative Pipeline syntax on the Pipeline syntax page. As mentioned above, stage blocks are optional in Scripted Pipeline syntax.

steps is Declarative Pipeline-specific syntax that describes the steps to be run in this stage.

sh is a Pipeline step (provided by the Pipeline: Nodes and Processes plugin) that executes the given shell command.

junit is another Pipeline step (provided by the JUnit plugin) for aggregating test reports.

sh is a Pipeline step (provided by the Pipeline: Nodes and Processes plugin) that executes the given shell command.

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent { label any }
    stages {
        stage('code') {
            steps {
                git url: 'https://github.com/devkrgoutam/node-todo-cicd.git',branch:'master'
            }
        }
        stage('build & test') {
            steps {
                sh 'docker build . -t node-todo-app'
            }
        }
        stage('deploy') {
            steps {
                sh 'docker-compose down && docker-compose up'
            }
        }
    }
}
pipeline {
    agent { label 'goutam-node-todo-cicd'}
    stages {
        stage('code') {
            steps {
                git url: 'https://github.com/devkrgoutam/node-todo-cicd.git',branch:'master'
            }
        }
        stage('build & test') {
            steps {
                sh 'docker build . -t node-todo-app'
            }
        }
        stage('deploy') {
            steps {
                sh 'docker-compose down && docker-compose up -d'
            }
        }
    }
}

Manage Jenkins-->Install Plugins-->Enviornment Injector

Jenkins Agent Configuration

Installation of docker : sudo apt-get install docker.io

Installation of docker-compose : sudo apt-get install docker –compose

Addition of docker to user : sudo usermod –aG docker Jenkins/$USER

Reboot the System: sudo reboot

Security -->inbound rule-->edit-->add-->port 8000 -->save