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

7

我已经在Azure管道中发布了一个构建产物,名称为“some_sidebar”,并将其发布到$(Build.ArtifactStagingDirectory)/drop。

如果我只有一个发布任务中的PowerShell脚本,如何在我的发布管道中检索该构建产物呢?

以下是代码特定部分:

$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 Pipeline中的构建工件路径是虚拟机中的某个路径...但我不知道如何在shell脚本中指定该路径,或者那个路径到底是什么...

2个回答

6

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

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

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

enter image description here

我认为你说的应该是将pathtoPublish设置为$(Build.ArtifactStagingDirectory)/drop,并使用artifactName "some_sidebar",类似于以下内容:

enter image description here

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

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

其次,微软提供了一系列 发布变量,以便我们可以直接使用它们。

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

enter image description here

因此,我们可以在PowerShell脚本中使用以上三个变量之一来获取artifact,但该变量并非文件的完整路径,而是发布管道中artifact的路径,我们需要再进行一步操作。

第三步,当您使用发布管道获取artifact时,它会将artifact设置为包含源别名的文件夹:

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任务中获取artifact文件some_sidebar.js注意:您可以尝试使用通配符来获取artifact,例如:
$te = Add-PnPFile -Path "$(System.DefaultWorkingDirectory)\**\some_sidebar.js"

希望这有所帮助。

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。

我尝试使用 $(System.DefaultWorkingDirectory)/$Env:RELEASE_PRIMARYARTIFACTSOURCEALIAS/**/packagename.sppkg,但在 Powershell 内联脚本中,它并不识别 ** 模式,无法找到该文件 -.- - MMMM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接