Azure devops 检索已发布Azure DevOps工件的正确方法

Azure devops 检索已发布Azure DevOps工件的正确方法,azure-devops,Azure Devops,我正在尝试为Angular应用程序设置CI/CD管道。 我从一个教程中获得了灵感(真不好意思,我关闭了浏览器选项卡,忘了在代码中添加kudo注释),但最终还是安排了这样做: # Node.js with Angular # Build a Node.js project that uses Angular. # Add steps that analyze code, save build artifacts, deploy, and more: # https://docs.microsoft

我正在尝试为Angular应用程序设置CI/CD管道。 我从一个教程中获得了灵感(真不好意思,我关闭了浏览器选项卡,忘了在代码中添加kudo注释),但最终还是安排了这样做:

# Node.js with Angular
# Build a Node.js project that uses Angular.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript


variables:
  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: 'xxx'

  # Environment name
  environmentName: 'xxx'

  # Web app name
  webAppName: 'xxx'

stages:
  - stage: Build
    displayName: Build stage
    jobs:
      - job: BuildJob
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: NodeTool@0
            inputs:
              versionSpec: '14.x'
            displayName: 'Install Node.js'
          - task: Npm@1
            inputs:
              command: 'ci'
            displayName: 'NPM CI'
          - task: Npm@1
            inputs:
              command: 'custom'
              customCommand: 'install -g @angular/cli'
            displayName: 'Install Angular'

          - script: ng build --prod
            displayName: 'build Angular'
          - task: ArchiveFiles@2
            displayName: 'Archive files'
            inputs:
              rootFolderOrFile: '$(System.DefaultWorkingDirectory)/dist/App-FE'
              includeRootFolder: false
              archiveType: zip
              archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
              replaceExistingArchive: true

          - task: PublishBuildArtifacts@1
            displayName: "Upload Artifacts"
            inputs:
              PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
              ArtifactName: 'APP-FE'
              publishLocation: 'Container'

  - stage: Deploy
    displayName: 'Deploy Web App'
    dependsOn: Build
    condition: succeeded()
    jobs:
      - deployment: DeploymentJob
        pool:
          vmImage: 'ubuntu-latest'
        environment: $(environmentName)
        strategy:
          runOnce:
            deploy:
              steps:
                - task: AzureWebApp@1
                  displayName: 'Deploy Azure Web App : $(webAppName)'
                  inputs:
                    azureSubscription: $(azureSubscription)
                    appName: $(webAppName)
                    appType: webAppLinux
                    package: $(Pipeline.Workspace)/$(Build.BuildId).zip
问题发生在部署阶段

理想情况下,我已经创建了一个zip工件来使用

2021-03-04T16:48:35.1679914Z File upload succeed.
2021-03-04T16:48:35.1680158Z Upload '/home/vsts/work/1/a/4406.zip' to file container: '#/7437349/APP-FE'
然后,在部署阶段检索工件

2021-03-04T16:48:56.3711666Z Downloading items from container resource #/7437349/APP-FE
2021-03-04T16:48:56.3712634Z Downloading artifact APP-FE from: https://dev.azure.com/xxx//_apis/resources/Containers/7437349?itemPath=APP-FE&isShallow=true&api-version=4.1-preview.4
2021-03-04T16:48:56.3725395Z Downloading APP-FE/4406.zip to /home/vsts/work/1/APP-FE/4406.zip
2021-03-04T16:48:56.3726090Z Downloaded APP-FE/4406.zip to /home/vsts/work/1/APP-FE/4406.zip
但这会在
/home/vsts/work/1/APP-FE/
中下载一个与
ArtifactName
匹配的文件。下一个作业失败

2021-03-04T16:48:57.8428994Z ##[error]Error: No package found with specified pattern: /home/vsts/work/1/4406.zip<br/>Check if the package mentioned in the task is published as an artifact in the build or a previous stage and downloaded in the current job.
2021-03-04T16:48:57.8428994Z##[error]错误:未找到具有指定模式的包:/home/vsts/work/1/4406.zip
检查任务中提到的包是作为构建中的工件发布还是作为前一阶段发布,并在当前作业中下载。
正确的路径应该是
/home/vsts/work/1/APP-FE/4406.zip
。我可能会硬编码
$(Pipeline.Workspace)/APP-FE/$(Build.BuildId).zip
路径,事实上我这样做是为了尝试,但我想更好地理解引用新上传的工件的正确语法应该是什么

请注意,如果我通过硬编码路径来更改管道,我将修复此错误并进入下一个错误


问题在于理解如何构建一个干净、简单、正确的管道,工件名称将始终是路径的一部分,因为可以想象,此部署之前的构建可能会发布多个工件

因此,您的部署作业将需要提供工件路径,但您可以在变量部分尝试:

变量:
...
工件名称:“APP-FE”
然后在发布阶段:

-任务:PublishBuildArtifacts@1
displayName:“上载工件”
投入:
PathtoPublish:“$(Build.ArtifactStagingDirectory)/$(Build.BuildId.zip”
工件名称:$(工件名称)
publishLocation:“容器”
然后在部署阶段:

-任务:AzureWebApp@1
displayName:'部署Azure Web应用:$(webAppName)'
投入:
azureSubscription:$(azureSubscription)
appName:$(webAppName)
appType:webAppLinux
包:$(Pipeline.Workspace)/$(artifactName)/$(Build.BuildId.zip
当使用下载具有指定工件名称的工件时,如前面提到的@WaitingForGuacamole所述,下载的构建工件将保留工件名称作为父文件夹。因此,在您的情况下,下载的工件文件的路径应该是“$(Pipeline.Workspace)/APP-FE/$(Build.BuildId.zip

如果不希望将工件名称保留为父文件夹,可以尝试使用。当您使用此任务下载单个工件时,工件名称将不会保留为父文件夹