在 Jenkins 声明性管道中将 repo root 作为 Docker 卷添加

Add the repo root as a Docker volume in a Jenkins declarative pipeline

我正在 运行使用声明性管道为 Jenkins 中的回购设置 CI 管道。

repo 现在在 .docker/php/Dockerfile 包含自己的 Dockerfile,我想用它来构建容器和 运行 中的管道。

通常,我使用 docker-compose.yaml:

中的卷获取容器中的代码
    volumes:
        - .:/home/wwwroot/comms-and-push

...所以我这样设置我的 Jenkinsfile:

pipeline {

    agent {
        dockerfile {
            dir ".docker/php"
            args "-v .:/home/wwwroot/comms-and-push"
        }
    }

    stages {

    ...

但是,当运行连接管道时,这会导致错误:

Error: docker: Error response from daemon: create .: volume name is too short, names should be at least two alphanumeric characters.

我无法指定完整路径,因为我在这种情况下不知道它——它在某些 Jenkins 工作区中 运行ning。


到目前为止我尝试过的:

使用 WORKSPACE 变量

        args "-v ${WORKSPACE}:/home/wwwroot/comms-and-push"

导致错误:

No such property: WORKSPACE for class: groovy.lang.Binding

在管道之前设置环境变量:

environment {
    def WORKSPACE = pwd()
}

pipeline {

    agent {
        dockerfile {
            dir '.docker/php'
            args "-v ${env.WORKSPACE}/:/home/wwwroot/comms-and-push"
        }
    }
    ...

导致 ${env.WORKSPACE} 解析为 null

standard Jenkins Docker integration 已经知道如何将工作区目录挂载到容器中。它在不同的容器内具有相同的文件系统路径,并且直接在容器外的 worker 上。您不需要自己提供 docker run -v 参数。

agent {
    dockerfile {
        dir ".docker/php"
        // No args
    }
}

stages {
    stage('Diagnostics') {
        sh "pwd"  // Prints the WORKSPACE
        sh "ls"   // Shows the build tree contents
        sh "ls /" // Shows the image's root directory
    }
}

如果您查看扩展的 Jenkins 日志,您会发现它本身提供 -v 选项。