如何在 azure DevOps 管道中将文件从一个阶段复制到另一个阶段

时间:2021-07-11 18:39:17

标签: azure-pipelines azure-pipelines-release-pipeline azure-pipelines-build-task azure-pipelines-yaml azure-pipelines-tasks

我为我的应用程序建立了多阶段 azure 构建管道,在第一阶段,我正在构建源代码并执行 test.sh - 这个 shell 脚本创建了一个新的 shell名为 data.sh 的脚本文件。在我的第二阶段,我正在执行 run.sh 执行这个新创建的文件 data.sh 但我得到 {{1 }} 并且我的管道在第二阶段失败了。

新创建的文件data.sh不是从我上一阶段复制过来的,我怎样才能让No such file or directory error进入我的第二阶段,请找到我的代码下面:

如果您能帮助我解决此问题,我将不胜感激,我的 data.sh 中需要 data.sh 文件,该文件是在 stage 2 中创建的

azure-pipeline.yml

stage 1

test.sh

trigger:
  - none

pool:
 vmImage: 'Ubuntu-latest'

stages:
  - stage: TestFilecopy
    displayName: Extecute test script 
    jobs:
      - job: myJob1
        displayName: Execute myJob1
        steps:
        - task: Bash@3
          inputs:
            targetType: filePath
            filePath: 'myApp/test.sh'
            workingDirectory: myApp

  - stage: TestFilecopy1
    displayName: Extecute new script 
    jobs:
      - job: myJob2
        displayName: Execute myJob2
        steps:
        - task: Bash@3
          inputs:
            targetType: filePath
            filePath: 'myApp/run.sh'
            workingDirectory: myApp

run.sh

#!/bin/bash
echo "hello word"
echo $PWD
ls -la

echo "#!/bin/bash
      echo 'Hello World'" > data.sh

ls -la

文件夹结构

echo "hello run file"
echo $PWD
ls -la
./data.sh

管道控制台输出 第一阶段输出日志

product-pipeline-project/
├─ myApp/
│  ├─ run.sh
│  ├─ test.sh
├─ azure-pipeline.yml

第 2 阶段输出日志

Generating script.
Formatted command: exec bash '/home/vsts/work/1/s/myApp/test.sh'
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/ef037ffc-0ea8-4366-b074-10120c9b1434.sh
hello word
/home/vsts/work/1/s/myApp
total 16
drwxr-xr-x 2 vsts docker 4096 Jul 11 18:25 .
drwxr-xr-x 4 vsts docker 4096 Jul 11 18:25 ..
-rw-r--r-- 1 vsts docker   48 Jul 11 18:25 run.sh
-rw-r--r-- 1 vsts docker  109 Jul 11 18:25 test.sh
total 20
drwxr-xr-x 2 vsts docker 4096 Jul 11 18:25 .
drwxr-xr-x 4 vsts docker 4096 Jul 11 18:25 ..
-rw-r--r-- 1 vsts docker   37 Jul 11 18:25 data.sh
-rw-r--r-- 1 vsts docker   48 Jul 11 18:25 run.sh
-rw-r--r-- 1 vsts docker  109 Jul 11 18:25 test.sh

1 个答案:

答案 0 :(得分:0)

每个作业都在单独的机器上运行。因此,在另一个作业中无法访问在一个作业中生成的文件。要使其可用,您需要发布为工件,然后下载它。 Here 你有这方面的文件。

在你的情况下是:

steps:
- publish: $(System.DefaultWorkingDirectory)/product-pipeline-project/myApp/data.sh
  artifact: WebApp

然后在另一个阶段:

- task: DownloadPipelineArtifact@2
  inputs:
    artifact: 'WebApp'
    path: $(System.DefaultWorkingDirectory)/product-pipeline-project/myApp

但是考虑一下您是否真的需要一个单独的舞台。