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



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

# Node.js with Angular
# Build a Node.js project that uses Angular.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.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.

正确路径应为/home/vsts/work/1/APP-FE/4406.zip。我可以硬编码$(Pipeline.Workspace)/APP-FE/$(Build.BuildId).zip路径,事实上我做了一个尝试,但是我想更好地理解引用新上传的工件的正确语法应该是什么。

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

问题是关于理解如何构建干净简单正确的管道

工件名称将始终是路径的一部分,因为可以想象,在此部署之前的构建可能会发布多个工件。

因此,您的部署作业将需要提供工件路径,但是您可以在变量部分尝试:
variables:
...
artifactName: 'APP-FE'

然后在发布阶段:

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

,然后在部署阶段:

- task: AzureWebApp@1
displayName: 'Deploy Azure Web App : $(webAppName)'
inputs:
azureSubscription: $(azureSubscription)
appName: $(webAppName)
appType: webAppLinux
package: $(Pipeline.Workspace)/$(artifactName)/$(Build.BuildId).zip

当使用Download Build Artifacts任务下载具有指定工件名称的工件时,如@WaitingForGuacamole如上所述,下载的构建构件将保留构件名称作为父文件夹。因此,在您的情况下,下载的工件文件的路径应该是">$(Pipeline.Workspace)/APP-FE/$(Build.BuildId).zip"。

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

相关内容

最新更新