Azure DevOps:如何从发布管道中的PowerShell脚本从构建Azure管道中检索构建工件?

时间:2020-01-16 10:48:03

标签: azure powershell azure-pipelines release artifact

我有一个发布到$(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\_temp\6d682160-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脚本中指定该路径,或者该路径到底是什么...?

2 个答案:

答案 0 :(得分:2)

Azure DevOps:如何从发布管道中的PowerShell脚本从构建Azure管道中检索构建工件?

您的帖子中有三个问题会导致此问题。

首先,由于您选择的工件发布位置为Azure Pipeline,因此无法设置targetPath。您可以检查文档Publish Build Artifacts task

enter image description here

我假设您所说的应该将pathtoPublish设置为$(Build.ArtifactStagingDirectory)/dro p,并带有“ some_sidebar”等人工名称,例如:

enter image description here

但是pathtoPublish用于设置要发布的文件夹或文件路径 ,换句话说,它是工件源位置,而不是目标位置。

因此,我们无需在powershell脚本中使用\drop即可获得工件。

第二,MS提供了一系列Release variables,以便我们可以直接使用它们。

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

enter image description here

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

第三,当您使用发布管道获取工件时,它将把工件设置为包含Source alias的文件夹:

enter image description here

作为测试,我使用以下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在工件中列出文件:

enter image description here

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

注意:,您可以尝试使用通配符获取工件,例如:

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

希望这会有所帮助。

答案 1 :(得分:0)

您应该可以使用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相同。