1

i am trying to write a pipeline which first clones a repository, then builds a docker image and after that pushes the docker image to docker hub. following is my jenkins file.

pipeline {
    agent { dockerfile true }
    environment {
        APPLICATION = 'connect'
        ENVIRONMENT = 'dev'
        BUILD_VERSION = '0.9.5'
        MAINTAINER_NAME = 'Shoaib'
        MAINTAINER_EMAIL = 'shoaib@email.com'
        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})
                    }
                }
            }
        }
    }
}

but when i run this job in blue ocean i am getting following error. enter image description here

i have tried googling it but could not find satisfactory answer. any help is appreciated.

Shoaib Iqbal
  • 2,420
  • 4
  • 26
  • 51

1 Answers1

4

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} .")
       }
    }
}
yong
  • 13,357
  • 1
  • 16
  • 27
  • thanks, its working now, another question, is ${ENVIRONMENT} correct way to access vairables declared in environment? – Shoaib Iqbal Jan 02 '19 at 11:36