Build & Push Docker Image with Jenkins

Setting credentials Manage Jenkins → Security (Credentials). Masukan Username dan Password Docker Hub

Create New Job Pipeline

pipeline {
    agent any

    environment {
        REGISTRY_CREDENTIALS = credentials('dockerhub-cehamot')
        DOCKER_IMAGE_NAME = "cehamot/rssejahterapp"
        DOCKER_IMAGE_TAG = "${env.BUILD_NUMBER}"
        REGISTRY_URL = "https://index.docker.io/v1/"
        REPO_URL = "https://github.com/mdrdani/appointmentApp.git"
        REPO_BRANCH = "master"
    }

    stages {
        stage('Clone Git Repository') {
            steps {
                git branch: REPO_BRANCH, url: REPO_URL
            }
        }

        stage('Build Docker Image') {
            steps {
                sh "docker build -t ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} ."
            }
        }

        stage('Push Docker Image') {
            steps {
                script {
                    withDockerRegistry([credentialsId: 'dockerhub-cehamot', url: REGISTRY_URL]) {
                        sh "docker push ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}"
                    }
                }
            }
        }
    }
}

Install Juga plugin Jenkins > Docker Pipeline.

Build Now.