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

2

7

I have a published a build artifact published to $(Build.ArtifactStagingDirectory)/drop with artifactName "some_sidebar" and the artifact publish location is Azure Pipeline.

How can I retrieve that artifact now inside my release Pipeline if I have only a PowerShell Skript in release task?

here is the code specific part:

$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

I get the following error:

 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  ...

I assume the build artifact path in azure pipeline is some path in the Virtual machine... but I don't know how to specify that path inside the shell script, or what that path is anyway...?

Calabria answered 16/1, 2020 at 10:48 Comment(0)
G
6

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

There are three questions in your post that cause this issue.

First, since you select the artifact publish location is Azure Pipeline, you could not set the targetPath. You could check the document Publish Build Artifacts task:

enter image description here

I assume what you said should be set the pathtoPublish to $(Build.ArtifactStagingDirectory)/drop with artifactName "some_sidebar"like:

enter image description here

But this pathtoPublish is used to set The folder or file path to publish, in other words, it is the artifact source location, not the target.

So, we do not need to use the \drop in the powershell scripts to get the artifact.

Second, MS provides a series of Release variables so that we can use them directly.

You could use the System.DefaultWorkingDirectory, System.ArtifactsDirectory or Agent.ReleaseDirectory:

enter image description here

So, we could use one of above three variables in the powershell scripts to get the artifact, but the variable not the full path to the file, it is the path for the artifact in the release pipeline, we need to do one more step.

Third, when you use release pipeline to get the artifact, which will set the artifact to the folder contain the Source alias:

enter image description here

As test, I create a sample with following powershell scripts:

$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

I use the powershell scripts Get-ChildItem -Path $path to list the file in the artifact:

enter image description here

Now, I could get artifact file some_sidebar.js in the powershell task.

Note: You could try to use the wildcard to get the artifact, like:

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

Hope this helps.

Ginger answered 17/1, 2020 at 6:32 Comment(0)
P
0

You should be able to use System.ArtifactsDirectory.

Here are my pipeline with example how I use the artifact from previous step. Same variable should be possible to use in powershell script. (This example is from a yaml pipeline for build and release.)

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

Note the ../a/**/ when searching for the zip after downloading the artifact. Not sure if /a/ is the same om all build agents can prob use system.artifactsDirectory here too.

Proffitt answered 16/1, 2020 at 12:45 Comment(1)
hmm I tried to use $(System.DefaultWorkingDirectory)/$Env:RELEASE_PRIMARYARTIFACTSOURCEALIAS/**/packagename.sppkg but it somehow does not recognize the ** pattern in powershell inline script and does not find the file -.-Calabria

© 2022 - 2024 — McMap. All rights reserved.