'Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline?
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...?
Solution 1:[1]
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:
I assume what you said should be set the pathtoPublish
to $(Build.ArtifactStagingDirectory)/drop
with artifactName "some_sidebar
"like:
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
:
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
:
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:
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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CodeWarrior |
Solution 2 | OscarNahoj |