【问题标题】:How to reuse files from workspaces from different parallel stages?如何重用来自不同并行阶段的工作空间中的文件?
【发布时间】:2023-02-02 22:43:53
【问题描述】:
我有一个并行运行多个阶段的 Jenkins 管道。其中一些阶段会生成我想在后面的步骤中重用的中间构建文件:
pipeline {
stages {
stage("Parallel build") {
parallel {
stage("A") { /* produces file A */ }
stage("B") { /* produces file B */ }
stage("C") { /* produces nothing relevant */ }
}
}
stage("Combine") {
/* runs a task that needs files A and B */
}
}
}
据我所知,詹金斯会随机给我一个并行阶段的工作空间。所以我的 Combine 步骤将包含文件 A、B 或两者都没有,但不会同时包含两者。
我该如何解决这个问题?
【问题讨论】:
标签:
jenkins
jenkins-pipeline
parallel-builds
【解决方案1】:
很少有方法可以做到这一点。
-
您可以将文件复制到您想要的目录(您知道路径,您可以创建一个具有唯一构建 ID 的子目录)并从那里访问它们。
-
您可以在初始阶段存储文件,然后取消存储它们并在后期使用它们。 Here 是文档。
stash includes: 'something/A.txt', name: 'filea'
unstash 'filea'
- 将工作区位置保存到全局变量并在阶段中使用它。
pipeline {
agent any
stages {
stage('Run Tests') {
parallel {
stage('Stage A') {
steps {
script {
sh '''
echo "STAGE AAAA"
pwd echo
"ATAGEA" > a.txt
'''
stageAWS = "$WORKSPACE"
}
}
}
stage('Stage B') {
steps {
script {
sh '''
echo "STAGE B"
pwd
'''
stageBWS = "$WORKSPACE" }
}
}
}
}
stage('Stage C') {
steps {
script { echo "$stageAWS" echo "$stageBWS" }
}
}
}
}