我有一个包含多个阶段的流水线,我想只在“n”个阶段之间重用docker容器,而不是所有阶段:
pipeline {
agent none
stages {
stage('Install deps') {
agent {
docker { image 'node:10-alpine' }
}
steps {
sh 'npm install'
}
}
stage('Build, test, lint, etc') {
agent {
docker { image 'node:10-alpine' }
}
parallel {
stage('Build') {
agent {
docker { image 'node:10-alpine' }
}
// This fails because it runs in a new container, and the node_modules created during the first installation are gone at this point
// How do I reuse the same container created in the install dep step?
steps {
sh 'npm run build'
}
}
stage('Test') {
agent {
docker { image 'node:10-alpine' }
}
steps {
sh 'npm run test'
}
}
}
}
// Later on, there is a deployment stage which MUST deploy using a specific node,
// which is why "agent: none" is used in the first place
}
}
4条答案
按热度按时间qpgpyjmq1#
参见Jenkins Pipeline docker agent的reuseNode选项:
https://jenkins.io/doc/book/pipeline/syntax/#agent
rt4zxlrg2#
您可以使用脚本管道,在其中您可以将多个
stage
步骤放入docker
步骤中,例如。g的。(code未经测试)
neekobn83#
对于Declarative pipeline,一种解决方案可以是在项目的根目录中使用Dockerfile。例如:
Dockerfile
Jenkinsfile
yjghlzjz4#
对于声明性管道,您可以使用这里描述的嵌套阶段
在上面的链接中有一个更好的例子,我只是不想逐字复制粘贴。