[英]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.我可以想到几种方法来解决这个问题。
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 加载。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.