Azure DevOps:如何从 Release Pipeline 中的 PowerShell 脚本构建 Azure Pipeline 中检索构建工件?

Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline?

我已经发布了一个构建项目,发布到 $(Build.ArtifactStagingDirectory)/drop,项目名称为 "some_sidebar",项目发布位置是 Azure Pipeline。

如果我在发布任务中只有一个 PowerShell Skript,现在如何在我的发布管道中检索该工件?

这是代码的具体部分:

$path = ".\_some_sidebar\drop"
#$path = $(Build.Repository.LocalPath)
$SPFolderName = "Style Library/_some_sidebar";

# Upload template list
$status = "Uploading template list to Location: " + $SPFolderName
Write-Host $status
$te = Add-PnPFile -Path $path"\some_sidebar.js" -Folder $SPFolderName -Checkout
Set-PnPFileCheckedIn -Url $te.ServerRelativeUrl

我收到以下错误:

 Uploading template list to Location: Style Library/_some_sidebar
2020-01-16T09:51:20.5062033Z Add-PnPFile : Local file was not found.
2020-01-16T09:51:20.5062546Z At D:\_work\_tempd682160-e8a7-4c56-ab30-7ff8c40f2958.ps1:51 char:7
2020-01-16T09:51:20.5062832Z + $te = Add-PnPFile -Path $path"\some_sidebar.js" -Folder $SPFolderName  ...

我假设 azure 管道中的构建工件路径是虚拟机中的某个路径...但我不知道如何在 shell 脚本中指定该路径,或者该路径是什么...?

您应该可以使用 System.ArtifactsDirectory。

这是我的管道,其中包含我如何使用上一步中的工件的示例。应该可以在 powershell 脚本中使用相同的变量。 (此示例来自用于构建和发布的 yaml 管道。)

stages:
- stage: build
  displayName: 'Build and package solution'
  jobs:
  - job: buildsteps
    displayName: 'Steps to build and package'
    pool: 'PrivateVS2017'
    steps:
    - task: ArchiveFiles@2
      inputs:
        rootFolderOrFile: '$(Build.SourcesDirectory)/Web'
        includeRootFolder: true
        archiveType: 'zip'
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
        replaceExistingArchive: true
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'it-service-wiki-build'
        publishLocation: 'Container'

- stage: deploy_to_development
  displayName: 'Deploy to development Environment'
  dependsOn: build
  jobs:
  - deployment: deploy
    displayName: 'Deploy the solution to Dev'
    pool: 'PrivateVS2017'
    environment: 'ITServiceWiki-Dev'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: 'current'
              buildVersionToDownload: 'latest'
              downloadType: 'single'
              ArtifactName: 'it-service-wiki-build'
              downloadPath: '$(System.ArtifactsDirectory)'
          - task: ExtractFiles@1
            inputs:
              archiveFilePatterns: '../a/**/$(Build.BuildId).zip'
              destinationFolder: '$(Build.DefaultWorkingDirectory)/$(Build.BuildId)'
              cleanDestinationFolder: true

下载工件后搜索 zip 时请注意 ../a/**/。不确定 /a/ 是否相同,因为所有构建代理也可能在这里使用 system.artifactsDirectory。

Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline?

您的 post 中有三个问题导致了此问题。

首先,因为你select工件发布位置是Azure Pipeline,你无法设置targetPath。你可以查看文档 Publish Build Artifacts task:

我假设你所说的应该将 pathtoPublish 设置为 $(Build.ArtifactStagingDirectory)/drop with artifactName "some_sidebar" like:

但是这个pathtoPublish用来设置文件夹或文件路径发布,换句话说,它是神器源位置,而不是目标。

因此,我们不需要使用 powershell 脚本中的 \drop 来获取工件。

其次,MS提供了一系列Release variables方便我们直接使用

您可以使用 System.DefaultWorkingDirectorySystem.ArtifactsDirectoryAgent.ReleaseDirectory:

因此,我们可以在 powershell 脚本中使用以上三个变量之一来获取工件,但该变量不是文件的完整路径,它是发布管道中工件的路径,我们需要再做一步。

第三种,当您使用发布管道获取工件时,会将工件设置到包含Source alias:

的文件夹

作为测试,我使用以下 powershell 脚本创建了一个示例:

$path = "$(System.DefaultWorkingDirectory)\<SourceAliasVlaue>\<AartifactName>"
#$path = $(Build.Repository.LocalPath)
$SPFolderName = "Style Library/_some_sidebar";

# Upload template list
$status = "Uploading template list to Location: " + $SPFolderName
Write-Host $status

Get-ChildItem -Path $path

我使用 powershell 脚本 Get-ChildItem -Path $path 列出工件中的文件:

现在,我可以在 powershell 任务中获取工件文件 some_sidebar.js

注意:您可以尝试使用通配符来获取神器,例如:

$te = Add-PnPFile -Path "$(System.DefaultWorkingDirectory)\**\some_sidebar.js"

希望这对您有所帮助。