简体   繁体   中英

Using a dockerfile with Jenkins Scripted Pipeline Syntax

Using Jenkins Declarative Pipeline , one can easily specify a Dockerfile, agent label, build args and run args as follows:

Jenkinsfile (Declarative Pipeline)

agent {
    dockerfile {
        dir './path/to/dockerfile'
        label 'my-label'
        additionalBuildArgs  '--build-arg version=1.0'
        args '-v /tmp:/tmp'
    }
}

I am trying to achieve the same using the scripted pipeline syntax. I found a way to pass the agent label and run args, but was unable to to pass the directory and build args . Ideally, I would write something like this (label and run args are already working):

Jenkinsfile (Scripted Pipeline)

node ("my-label"){
    docker.dockerfile(
        dir: './path/to/dockerfile',
        additionalBuildArgs:'--build-arg version=1.0'
    ).inside('-v /tmp:/tmp') {
        \\ add stages here
    }
}

The documentation shows how this can be done using an existing docker image, ie, with the image directive in the pipeline.

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent {
        docker { image 'node:7-alpine' }
    }
    stage('Test') {
        //...
    }
}

Jenkinsfile (Scripted Pipeline)

node {
    docker.image('node:7-alpine').inside {
        stage('Test') {
            //...
        }
    }
}

However, the scripted pipeline syntax for the dockerfile directive is missing. The workaround I am using at the moment is building the image myself.

node ("my-label"){
    def testImage = docker.build(
        "test-image",
        "./path/to/dockerfile",
        "--build-arg v1.0"
    )

    testImage.inside('-v /tmp:/tmp') {
        sh 'echo test'
    }
}

Any help is much appreciated!

I personally put the docker cli arguments before the image folder path and would specify the docker filename with -f argument

Apart from that, you are doing this the right way. agent dockerfile is building a docker image the same way docker.build step is doing. Except you can push your image to a registry by using the docker.build step

Here is I how do

def dockerImage
//jenkins needs entrypoint of the image to be empty
def runArgs = '--entrypoint \'\''
pipeline {
    agent {
        label 'linux_x64'
    }
    options {
        buildDiscarder(logRotator(numToKeepStr: '100', artifactNumToKeepStr: '20'))
        timestamps()
    }
    stages {
        stage('Build') {
            options { timeout(time: 30, unit: 'MINUTES') }
            steps {
                script {
                    def commit = checkout scm
                    // we set BRANCH_NAME to make when { branch } syntax work without multibranch job
                    env.BRANCH_NAME = commit.GIT_BRANCH.replace('origin/', '')

                    dockerImage = docker.build("myImage:${env.BUILD_ID}",
                        "--label \"GIT_COMMIT=${env.GIT_COMMIT}\""
                        + " --build-arg MY_ARG=myArg"
                        + " ."
                    )
                }
            }
        }
        stage('Push to docker repository') {
            when { branch 'master' }
            options { timeout(time: 5, unit: 'MINUTES') }
            steps {
                lock("${JOB_NAME}-Push") {
                    script {
                        docker.withRegistry('https://myrepo:5000', 'docker_registry') {
                            dockerImage.push('latest')
                        }
                    }
                    milestone 30
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM