3
votes

I'm trying to run docker command in my declarative pipeline, to install docker env on my slave machine i'm trying to use docker commons plugin "https://plugins.jenkins.io/docker-commons/", but no success.

Further research i have got below link mentioning how to use this plugin.

https://automatingguy.com/2017/11/06/jenkins-pipelines-simple-delivery-flow/

I have configured docker in manage jenkins -> global tool configuration, but dont find how to use below section in my declarative pipeline of jenkins, i think below structure/syntax will work for scripted jenkins pipeline

def dockerTool = tool name: 'docker', type: 
'org.jenkinsci.plugins.docker.commons.tools.DockerTool'
withEnv(["DOCKER=${dockerTool}/bin"]) {
   stages{}
}

Can someone pls help, how i can use docker common tool in declarative pipeline of jenkins. Note: I cannot switch to scripted pipeline due to standardization with other projects

3
Have you tried enclosing this block in script? Whole pipeline stays as declarative, only the portion will be scripted. pipeline { stages { stage('my stage'){steps { script { dockerTool = tool name: 'docker', type: 'org.jenkinsci.plugins.docker.commons.tools.DockerTool' withEnv(["DOCKER=${dockerTool}/bin"]) { }}}}}} - Ram
Thanks Ram for ur comment, I have added below stage in my script 'stage('my stage'){ steps{ script{ def dockerTool = toolname: 'docker', type: 'org.jenkinsci.plugins.docker.commons.tools.DockerTool' withEnv(["DOCKER=${dockerTool}/bin"]){ sh "docker version" } } } } ' but failed with error org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 40: expecting '}', found ':' @ line 40, column 30. def dockerTool = toolname: 'docker', type: 'org.jenkinsci.plugins.docker.commons.tools.DockerTool' - Kalim
seems the syntax to add the def and scripts to use with withEnv is different in declarative pipeline - Kalim
You cannot use def. remove def and try. - Ram
@Kalim Did you find solution for this? I have the same problem.The official documentation, plugins.jenkins.io/docker-commons/#documentation is not working for me either. - user845279

3 Answers

0
votes

Here is the working example

pipeline{
    agent any
    stages{
        stage('test') {
            steps{
                script{
                    test_env="this is test env"
                    withEnv(["myEnv=${test_env}"]){
                        echo "${env.myEnv}"
                    }
                }
            }
        }
    }
}
0
votes

I have this feeling that you are don't need to use either withEnv or docker commons. Have you seen this? https://www.jenkins.io/doc/book/pipeline/docker/ There are plenty of good examples of how to use docker with Jenkinsfile.

My attempt to answer your question (if I got it right), if you are asking about declarative equivalent for scripted withEnv, then probably you are looking for environment {}? Something like this:

pipeline {
    agent any

    environment {
        DOCKER = "${dockerTool}/bin"
    }

    stages {
        stage('One') {
            steps {
              // steps here
            }
        }
    }
} 
0
votes

Here is a working declarative pipeline solution as of Docker Commons v1.17

Note: the tool name, dockerTool is a keyword and docker-19.03.11 is name I gave my installation in Jenkins > Manage Jenkins > Global Tool Configuration page.

pipeline {
    agent any
    tools {
        dockerTool 'docker-19.03.11'
    }
    stages {
        stage('build') {
            steps {
                sh'''
                    echo 'FROM mongo:3.2' > Dockerfile
                    echo 'CMD ["/bin/echo", "HELLO WORLD...."]' >> Dockerfile
                '''
                script {
                    docker.withRegistry('http://192.168.99.100:5000/v2/') {
                        def image = docker.build('test/helloworld2:$BUILD_NUMBER')
                    }
                }
            }
        }
    }
}

enter image description here