Search code examples
dockerjenkinsjenkins-pipelinedocker-buildjenkins-docker

Docker build with Jenkins issue


I am trying to write a Jenkins pipeline which first clones a git repository, then builds a Docker image and finally pushes the image to the Docker Hub. My Jenkinsfile is:

pipeline {
    agent { dockerfile true }
    environment {
        APPLICATION = 'connect'
        ENVIRONMENT = 'dev'
        BUILD_VERSION = '0.9.5'
        MAINTAINER_NAME = 'Shoaib'
        MAINTAINER_EMAIL = '[email protected]'
        BUILD_DOCKER_REPO = repo1/images'
        DOCKER_IMAGE_TAG = 'repo1/images:connect_dev_0.9.5'
    }
    stages {
        stage('clone repository') {
            steps {
                checkout Jenkins-Integration
            }
        }
        stage('Build Image') {
            steps {
                image = docker.build("-f Dockerfile.local", "--no-cache", "-t ${DOCKER_IMAGE_TAG}", "--build-arg envior=${ENVIRONMENT} .", "--build-arg build_version=${BUILD_VERSION} .", "--build-arg maintainer_name=${MAINTAINER_NAME} .", "--build-arg maintainaer_email=${MAINTAINER_EMAIL} .")
            }
        }
        stage('Deploy') {
            steps {
                script {
                    docker.withRegistry('https://registry.example.com', 'docker-hub-credentials') {
                        image.push(${DOCKER_IMAGE_TAG})
                    }
                }
            }
        }
    }
}

However, when I run this job (in Blue Ocean) I am getting the following error:

enter image description here

I have tried googling it, but could not find a satisfactory answer. Any help will be appreciated.


Solution

  • Put the docker.build in below stage into a script as following:

    stage('Build Image') {
        steps {
            script {
                def image = docker.build(
                    "-f Dockerfile.local", 
                    "--no-cache", 
                    "-t ${DOCKER_IMAGE_TAG}", 
                    "--build-arg envior=${ENVIRONMENT}", 
                    "--build-arg build_version=${BUILD_VERSION}", 
                    "--build-arg maintainer_name=${MAINTAINER_NAME}", 
                    "--build-arg maintainaer_email=${MAINTAINER_EMAIL} .")
           }
        }
    }