如何在Azure DevOps多阶段流水线中实现跨阶段复制生成的文件
问题根因
Azure DevOps 的每个阶段默认运行在独立的干净代理节点上,上一阶段运行时产生的临时文件不会自动保留到后续阶段,因此第二阶段执行run.sh时找不到第一阶段生成的data.sh文件,触发127错误码。
解决方法
通过 Azure DevOps 流水线工件实现跨阶段文件传递,只需对流水线配置做两处修改:
- 第一阶段生成
data.sh后,将其发布为流水线工件 - 第二阶段执行
run.sh前,先下载前序阶段发布的工件到对应目录
修改后的完整流水线配置
trigger: - none pool: vmImage: 'Ubuntu-latest' stages: - stage: TestFilecopy displayName: 执行测试脚本 jobs: - job: myJob1 displayName: 执行myJob1 steps: - task: Bash@3 inputs: targetType: filePath filePath: 'myApp/test.sh' workingDirectory: myApp # 发布生成的data.sh为流水线工件 - task: PublishPipelineArtifact@1 inputs: targetPath: 'myApp/data.sh' artifact: 'generated_scripts' publishLocation: 'Container' - stage: TestFilecopy1 displayName: 执行新脚本 dependsOn: TestFilecopy jobs: - job: myJob2 displayName: 执行myJob2 steps: # 下载上一阶段发布的工件到myApp目录 - task: DownloadPipelineArtifact@2 inputs: buildType: 'current' artifactName: 'generated_scripts' targetPath: 'myApp' - task: Bash@3 inputs: targetType: filePath filePath: 'myApp/run.sh' workingDirectory: myApp
修改后重新运行流水线,第二阶段即可正常读取到data.sh文件,不会再触发文件不存在的错误。
内容的提问来源于stack exchange,提问作者Datadog Learning
相关产品推荐
相关产品推荐

