【问题标题】:How to copy file from one stage to another stage in azure DevOps pipeline如何在 azure DevOps 管道中将文件从一个阶段复制到另一个阶段
【发布时间】:2021-07-11 18:39:17
【问题描述】:

我为我的应用程序创建了多阶段 azure 构建管道,在第一阶段,我正在构建源代码并执行 test.sh - 这个 shell 脚本创建一个新的 shell 脚本文件叫 data.sh。在我的第二阶段,我正在执行 run.sh 执行这个新创建的文件 data.sh 但我得到 No such file or directory error我的管道在第二阶段失败了。

新创建的文件data.sh不是从我之前的阶段复制的,我如何才能将data.sh进入我的第二阶段,请在下面找到我的代码:

非常感谢有人能帮我解决这个问题,我需要在stage 2 中创建的data.sh 文件,该文件是在stage 1 中创建的

azure-pipeline.yml

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

test.sh

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

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

ls -la

run.sh

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

文件夹结构

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

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

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

第 2 阶段输出日志

Generating script.
Formatted command: exec bash '/home/vsts/work/1/s/myApp/run.sh'
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/087e12fd-0b7b-4bd5-a4a8-7b7879255f3f.sh
hello run file
/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
/home/vsts/work/1/s/myApp/run.sh: line 4: ./data.sh: No such file or directory
##[error]Bash exited with code '127'.
Finishing: Bash

【问题讨论】:

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


    【解决方案1】:

    每个作业都在单独的机器上运行。因此,在一项作业中生成的文件在另一项作业中无法访问。要使其可用,您需要将其发布为工件,然后下载。 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
    

    但是考虑一下你是否真的需要一个单独的阶段。

    【讨论】:

    • 感谢@Krzysztof Madej 的解释。我的“data.sh”文件中有一些敏感的 PII 数据,因此发布工件对于我的用例来说不是一个好的选择。我认为-如果您能想到,请提出任何替代选择。再次感谢!
    • 好吧,如果您不想保留此文件,请考虑在一个作业中运行。为什么需要单独的阶段和工作?
    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2020-04-07
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多