【问题标题】:Add the repo root as a Docker volume in a Jenkins declarative pipeline在 Jenkins 声明性管道中将 repo 根添加为 Docker 卷
【发布时间】:2020-11-23 19:46:17
【问题描述】:

我正在使用声明性管道在 Jenkins 中为 repo 运行 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 工作区中运行。


到目前为止我所做的尝试:

使用 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

【问题讨论】:

  • 如果你什么都不做,Jenkins会将$WORKSPACE挂载到一个相同的文件路径;这应该适用于大多数常规的构建和测试任务。或者当你要求 Jenkins 构建镜像时,你可以在 COPY 将应用程序代码注入镜像时指定作为构建上下文的目录(运行时不需要单独注入)。这些方法中的任何一种都适合您吗?如果没有,您正在运行什么 steps 需要不同的东西?
  • @DavidMaze 谢谢——詹金斯的默认行为对我的管道来说已经足够了。

标签: docker jenkins continuous-integration dockerfile jenkins-pipeline


【解决方案1】:

standard Jenkins Docker integration 已经知道如何将工作区目录挂载到容器中。它在不同容器内具有相同的文件系统路径,并且直接在容器外的工作人员上。您不需要自己提供 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 选项。

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 2018-01-24
    • 2018-10-26
    • 1970-01-01
    • 2022-08-05
    • 2019-05-20
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多