Search code examples
azurepowershellazure-pipelinesreleaseartifact

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

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