For the past couple weeks I’ve been playing a lot with YAML Azure Pipelines. And it’s not as straightforward as I would like I can say. It takes a lot of work to align all tasks, templates, variables and so on.
This post is to talk about one minor thing, but that took me a couple hours to fix. It’s about using the tasks to publish and download artifacts.
I read several blog posts and the Microsoft documentation and all examples are pointing out that I can use the system location $(Build.ArtifactStagingLocation) when doing the publishing during my build phase.
So that’s what I did as you can see in the code below:
1 2 3 4 5 |
- task: PublishBuildArtifacts@1 displayName: 'Publish Package' inputs: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: 'DebugReport-Function' |
And according to these blog posts and documentation I can use the same location to retrieve the artifact during my deployment phase.
So I did the following code to do so:
1 2 3 4 5 6 7 8 9 |
steps: - task: AzureFunctionApp@1 displayName: 'Deploy Azure Function' inputs: azureSubscription: 'mycredential' appType: 'functionApp' appName: 'myApp' package: '$(Build.ArtifactStagingDirectory)/**/*.zip' deploymentMethod: 'auto' |
But this does not work. It keeps saying that it cannot find the file, but checking the logs I could see it was being generated properly.
Below you can see the error message I was getting:
I added a few debug steps to check the location of know system variables to understand what was going on using a powershell script as below:
1 2 3 4 5 6 7 8 9 10 |
- task: PowerShell@2 displayName: 'Degug parameters' inputs: targetType: Inline script: | Write-Host "$(Build.ArtifactStagingDirectory)" Write-Host "$(System.DefaultWorkingDirectory)" Write-Host "$(System.ArtifactsDirectory)" Write-Host "$(Pipeline.Workspace)" Write-Host "$(System.ArtifactsDirectory)" |
With that I could see that the location it was looking was not the same as the location where the package was being published.
The $(Build.ArtifactStagingDirectory) was returning d:\a\1\a but the file was being downloaded to d:\a\1.
With the debugging script above, I could see that this location is the one present at $(Pipeline.Workspace), which was happening automatically.
The Microsoft documentation is very confusing but finally I found one that helped me in pointing to the solution at: https://docs.microsoft.com/en-us/azure/devops/pipelines/artifacts/pipeline-artifacts?view=azure-devops&tabs=yaml-task#artifact-selection.
In there is explaining that I can define where I can download the artifacts to with the following task:
1 2 3 4 5 6 7 |
steps: - download: none - task: DownloadPipelineArtifact@2 displayName: 'Download Build Artifacts' inputs: patterns: '**/*.zip' path: '$(Build.ArtifactStagingDirectory)' |
Now I can define exactly where I want to download the artifacts to. Please be aware that the download happens automatically to $(Pipeline.Workspace), so if you don’t want you deployment to download the files twice, you need to specify the “download: none” in your steps.
Summary
I don’t know if this was a recent change or not, but all blog posts pointing to the same system variable with no mention to this finding that I got seems very strange to me. It would be really good to know if someone out there is having the same problem as me.