如何在Azure DevOps的YAML阶段之间共享文件

Tec*_*Rik 5 azure-devops azure-pipelines-build-task azure-pipelines

我正在尝试使用Azure DevOps将node.js代码部署到Azure Function App。我已经使用YAML创建了以下Azure DevOps管道。

我面临的问题是,在部署步骤中,我的管道失败了,因为它无法找到package。当我查看日志时,我相信在作业/阶段之间的清理活动期间,将清理文件夹。我试过使用其他预定义变量,例如,Build.ArtifactStagingDirectory但没有用。

trigger:
  - master

variables:
  azureServiceConnection: 'mySvcCon'
  azureFuncApp: myFApp

stages:
  - stage: Build_1
    displayName: 'Build Stage'
    jobs:
      - job: build
        displayName: 'Build Node.js app'
        pool:
          vmImage: 'Ubuntu-16.04'

        steps:
          - task: NodeTool@0
            displayName: 'Install Node.js'
            inputs:
              versionSpec: '8.x'

          - script: |
              npm install
            displayName: 'npm install and build'

          - task: CopyFiles@2
            displayName: 'Copy necessary files'
            inputs:
              SourceFolder: '$(System.DefaultWorkingDirectory)'
              Contents: |
                **/*
                !.vscode/**/*
              TargetFolder: '$(System.DefaultWorkingDirectory)/copied'

          - task: PublishBuildArtifacts@1
            displayName: 'Publish artifact'
            enabled: true
            inputs:
              PathtoPublish: '$(Build.ArtifactStagingDirectory)/copied'
              publishLocation: filePath
              targetPath: '$(System.DefaultWorkingDirectory)/publish'

  - stage: Deploy_2
    displayName: 'Deploy Stage'
    jobs:
      - job: Deploy
        displayName: 'Deploy to Function App'
        pool:
          vmImage: 'Ubuntu-16.04'

        steps:
          - task: AzureRMWebAppDeployment@4
            displayName: 'AzureRM Function App deploy'
            inputs:
              ConnectionType: 'AzureRM'
              ConnectedServiceName: $(azureServiceConnection)
              WebAppKind: 'Function App'
              WebAppName: $(azureFuncApp)
              Package: '$(System.DefaultWorkingDirectory)/publish'
Run Code Online (Sandbox Code Playgroud)

如何在各个阶段之间共享我的工件?如果我将所有步骤都放在同一工作中,则同一管道可以工作。但是我想把它们分开。

rsy*_*rsy 35

正如答案和一些评论中提到的,现在可以下载以前发布的工件。

在下面的代码中,我将该scripts文件夹发布为名为 的工件dropScripts,该工件位于解决方案的根目录中。这样我就可以在管道的后续阶段使用该文件夹中包含的脚本。在另一个阶段,我下载该dropScripts工件,然后运行script20.ps1该文件夹中包含的 powershell 脚本 ( ) scripts

stages:
- stage: 'Build'
  jobs: 
  - job: 'Build'
    pool:
      vmImage: 'ubuntu-16.04'
    steps:

    (...)

    - task: CopyFiles@2
      displayName: 'Copy powershell scripts for later use in the pipeline'
      inputs:
        contents: 'scripts/**'
        targetFolder: '$(Build.ArtifactStagingDirectory)'
    - publish: '$(Build.ArtifactStagingDirectory)/scripts'
      displayName: 'Publish powershell scripts for later use'
      artifact: dropScripts

- stage: DoSomethingWithTheScriptsStage
  dependsOn: Build
  jobs: 
  - job: DoSomethingWithTheScriptsJob
    pool: 
      vmImage: 'windows-2019'
    steps:
      - download: current
        artifact: dropScripts
      - task: PowerShell@2
        inputs:
          filePath: '$(Pipeline.Workspace)\dropScripts\script20.ps1'
Run Code Online (Sandbox Code Playgroud)

  • “download”步骤只是“DownloadPipelineArtifact”任务的快捷方式。 (7认同)
  • $(Pipeline.Workspace) 拯救了我的一天...一直使用错误的变量!!!!!! (7认同)

Sli*_*SFT 7

通常,创建工件通常由Build Pipeline完成,而部署工件则在Release Pipeline 中完成。根据您的使用情况,绝对有机会在单个构建管道中执行这两个操作。当您刚开始使用Azure Pipelines时,组合尤其有意义,因为生态系统可能会因可用功能的数量而不堪重负。有一些关于将发布功能合并到构建功能中以简化入门的公开工作

如果第一次部署失败,分离管道确实会给你带来重试部署的好处——这真的取决于你的构建时间有多快。如果您想手动触发环境或环形发布传播,则支持跨环境部署相同位也更容易。一旦您深入了解发布阶段的一些高级用户功能,用于分离构建/部署的列表就会呈指数增长

对于您的工作方法 - 您可以利用dependsOnYAML 元素将后续作业链接到具有输出依赖性

构建管道 - 依赖链

jobs:
- job: InitialA
  steps:
  - script: echo hello from initial A
- job: InitialB
  steps:
  - script: echo hello from initial B
- job: Subsequent
  dependsOn:
  - InitialA
  - InitialB
  steps:
  - script: echo hello from subsequent
Run Code Online (Sandbox Code Playgroud)

19 年 11 月 15 日更新

Devops 最近发布了跨 CI/CD 边界使用文件的下载任务管道工件现在也可用于跨阶段共享文件。

  • 我不明白这是如何工作的 - 作业“后续”肯定在其他 2 个之后运行,但是,这如何回答 OP,因为它共享文件?据我所知,“后续”在新的 VM 上进行了完整的结帐,因此无法共享任何文件... (3认同)
  • 这些链接都没有按照原始问题解释_如何在 Azure DevOps 中的 YAML 阶段之间共享文件_。他们建议最好的是在第 1 阶段复制然后发布,然后在第 2 阶段下载。如果您在不同阶段生成多个工件并且需要在最后一步发布,这会非常复杂。 (2认同)