Search code examples
dockerjenkinsvspherejenkins-declarative-pipeline

Using the govc container in Jenkins


I'd like to run govc commands in a Jenkins pipeline. Running the govc container manually works:

docker pull vmware/govc && docker run -e GOVC_USERNAME=$GOVC_USERNAME -e GOVC_PASSWORD=$GOVC_PASSWORD -e GOVC_INSECURE=$GOVC_INSECURE vmware/govc find -u=$GOVC_URL / -type m

For some reason the short environment variable passing version (-e ENV_VAR) doesn't work with this image and I must enter GOVC_URL as a govc argument, but besides these the command returns the right output.

This is the pipeline I wrote for the command:

pipeline {
    agent {
        docker { image 'vmware/govc' }
    }
    stages {
        stage('test govc') {
            steps {
                script {
                    def govcEnvVars = [
                        GOVC_INSECURE: 1,
                        GOVC_USERNAME: 'my_user',
                        GOVC_PASSWORD: 'my_password',
                        GOVC_URL: 'my_url'
                    ]
                    withEnv(govcEnvVars.collect { k, v -> "${k}=${v}" }) {
                        sh "govc find -u=$GOVC_URL / -type m"
                    }
                }
            }
        }
    }
}

Now, no matter what command I try to run I always get the following error message in the console log:

ERROR: The container started but didn't run the expected command. Please double check your ENTRYPOINT does execute the command passed as docker run argument, as required by official docker images (see https://github.com/docker-library/official-images#consistency for entrypoint consistency requirements).
Alternatively you can force image entrypoint to be disabled by adding option `--entrypoint=''`.

What am I doing wrong?


Solution

  • As Zeitounator pointed out, the issue is the way Jenkins handles containers that use ENTRYPOINT, so I switched to sh steps:

    def image = 'vmware/govc'
    def govcEnvVars = [
        GOVC_INSECURE: 1,
        GOVC_USERNAME: params.User,
        GOVC_PASSWORD: params.Password,
        GOVC_URL: params.vCenter
    ].collect { k, v -> "-e ${k}='${v}'" }.join(' ')
    sh "docker pull ${image}"
    def vms = sh(
        script: "docker run ${govcEnvVars} '${image}' find -u=${params.vCenter} / -type m",
        returnStdout: true
    ).split('\n')
    

    For future viewers: there's galaad/govc, which is up-to-date with current govc development and isn't configured with an ENTRYPOINT, but I haven't tried it on Jenkins yet and I don't want to use containers outside the VMWare repository.