在 Jenkins 中使用 govc 容器

Using the govc container in Jenkins

我想在 Jenkins 管道中 运行 govc 命令。 运行 govc 容器手动工作:

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

出于某种原因,短环境变量传递版本 (-e ENV_VAR) 不适用于此图像,我必须输入 GOVC_URL 作为 govc 参数,但除此之外命令 returns 正确的输出。

这是我为命令编写的管道:

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"
                    }
                }
            }
        }
    }
}

现在,无论我尝试使用什么命令 运行 我总是在控制台日志中收到以下错误消息:

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=''`.

我做错了什么?

正如 Zeitounator 指出的那样,问题在于 Jenkins 处理使用 ENTRYPOINT 的容器的方式,因此我切换到 sh 步骤:

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')

对于未来的观众:有 galaad/govc,它是当前 govc 开发的最新版本,未配置 ENTRYPOINT,但我还没有尝试它在 Jenkins 上,我不想在 VMWare 存储库之外使用容器。