简体   繁体   English

带有来自 SCM 的 Docker/Dockerfile 代理的 Jenkins 声明式管道

[英]Jenkins declarative pipeline with Docker/Dockerfile agent from SCM

With Jenkins using the Declarative Pipeline Syntax how do i get the Dockerfile ( Dockerfile.ci in this example) from the SCM (Git) since the agent block is executed before all the stages?对于使用詹金斯的Declarative Pipeline Syntax我如何得到Dockerfile( Dockerfile.ci在这个例子中)从SCM(GIT),因为agent块之前的所有阶段执行?

pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.ci'
        }
    }
    stage ('Checkout') {
        steps {
            git(
                url: 'https://www.github.com/...',
                credentialsId: 'CREDENTIALS',
                branch: "develop"
            )
        }
    }
    [...]
}

In all the examples i've seen, the Dockerfile seems to be already present in the workspace.在我看到的所有示例中,Dockerfile 似乎已经存在于工作区中。

You could try to declare agent for each stage separately, for checkout stage you could use some default agent and docker agent for others.您可以尝试分别为每个阶段声明代理,对于结帐阶段,您可以为其他阶段使用一些默认代理和 docker 代理。

pipeline {
    agent none
    stage ('Checkout') {
        agent any
        steps {
            git(
                url: 'https://www.github.com/...',
                credentialsId: 'CREDENTIALS',
                branch: "develop"
            )
        }
    }
    stage ('Build') {
        agent {
            dockerfile {
            filename 'Dockerfile.ci'
        }
        steps {
            [...]
        }
}
    }
    [...]
}

If you're using a multi-branch pipeline it automatically checks out your SCM before evaluating the agent.如果您使用的是多分支管道,它会在评估代理之前自动检查您的 SCM。 So in that case you can specify the agent from a file in the SCM.因此,在这种情况下,您可以从 SCM 中的文件中指定代理。

The answer is in the Jenkins documentation on the Dockerfile parameter :答案在关于 Dockerfile 参数Jenkins 文档中

In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM.为了使用这个选项,Jenkinsfile必须从 Multibranch Pipeline 或来自 SCM 的 Pipeline 加载。

Just scroll down to the Dockerfile section, and it's documented there.只需向下滚动到 Dockerfile 部分,它就记录在那里。

The obvious problem with this approach is that it impairs pipeline development.这种方法的明显问题是它损害了管道的开发。 Now instead of testing code in a pipeline field on the server, it must be committed to the source repository for each testable change.现在,不是在服务器上的管道字段中测试代码,而是必须为每个可测试的更改提交到源存储库。 NOTE also that the Jenkinsfile checkout cannot be sparse or lightweight as that will only pick up the script -- and not any accompanying Dockerfile to be built.还要注意Jenkinsfile 结帐不能是稀疏或轻量级的,因为这只会拾取脚本——而不是任何要构建的随附 Dockerfile。

I can think of a couple ways to work around this.我可以想到几种方法来解决这个问题。

  1. Develop against agents in nodes with the reuseNode true directive.使用reuseNode true指令针对节点中的代理进行开发。 Then when code is stable, the separate agent blocks can be combined together at the top of the Jenkinsfile which must then be loaded from the SCM.然后当代码稳定时,单独的代理块可以在 Jenkinsfile 的顶部组合在一起,然后必须从 SCM 加载。
  2. Develop using the dir() solution that specs the exact workspace directory, or alternately use one of the other examples in this solution .使用指定确切工作区目录的dir()解决方案进行开发,或者交替使用此解决方案中的其他示例之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将配置文件从 Dockerfile 插入到声明性 Jenkins 管道 Docker 容器中 - Insert a config file into a declarative Jenkins pipeline Docker container from Dockerfile Jenkins 总是从声明性管道代理定义中提取 docker 镜像 - Jenkins always pull docker image from a declarative pipeline agent definition 声明性管道中的 Jenkins docker 代理:找不到文件 - Jenkins docker agent in declarative pipeline: file not found 使用 Jenkins 声明式管道为 dockerfile 代理设置构建参数 - Setting build args for dockerfile agent using a Jenkins declarative pipeline 声明式 Jenkins 管道和 Docker - Declarative Jenkins pipeline and Docker 无法通过 Jenkins 声明性管道在 Docker 映像中作为代理安装 pip - Unable to pip install in Docker image as agent through Jenkins declarative pipeline Jenkins 管道 docker 代理,使用预模式从 Z3254677A7917C6C01F55212FZ6C 启动 docker 容器 - Jenkins pipeline docker agent, Start docker conatiner from Dockerfile with previliged mode 如何在 Jenkins 声明性管道中将人工制品从一个代理复制到另一个代理? - How to copy an artefact from one agent to another in a Jenkins declarative pipeline? Jenkins 声明性 docker 管道与 angular - Jenkins declarative docker Pipeline with angular Jenkins 无法在具有特定运行 arguments 的声明性管道中启动 docker 代理容器 - Jenkins cannot start a docker agent container in declarative pipeline with specific run arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM