jenkins - 在多个阶段之间重用 Jenkins 中的代理(docker 容器)
问题描述
我有一个具有多个阶段的管道,我想在仅“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
}
}
解决方案
请参阅Jenkins Pipeline docker代理的重用节点选项:
https ://jenkins.io/doc/book/pipeline/syntax/#agent
pipeline {
agent any
stages {
stage('NPM install') {
agent {
docker {
/*
* Reuse the workspace on the agent defined at top-level of
* Pipeline, but run inside a container.
*/
reuseNode true
image 'node:12.16.1'
}
}
environment {
/*
* Change HOME, because default is usually root dir, and
* Jenkins user may not have write permissions in that dir.
*/
HOME = "${WORKSPACE}"
}
steps {
sh 'env | sort'
sh 'npm install'
}
}
}
}
推荐阅读
- r - 如何在 ggplot2 中标记蜂群图?
- c# - 我无法在主菜单上保存多个 PlayerPrefs
- c# - 从 F# 调用 C# dll
- regex - 在 curl/grep 组合中无法识别正则表达式
- soapui - Soapui 5.6.0:ORA-28040:从 JDBC 测试步骤连接时没有匹配的身份验证协议
- r - 绘制方差分布
- python - Pyplot 按钮小部件悬停事件不起作用
- javascript - 是否可以使用 Chrome 的文件系统访问 API 附加到现有文件
- python - 将颜色条的范围添加到熊猫子图
- postgresql - 在干净的 Ubuntu 版本上安装后如何运行 PostgreSQL?