I am working with Azure Pipelines and need to upload a file to Azure artifacts in one stage (build), and then download that same package with that same version in another stage (deploy). So far I have done this by using the Universal Package task for both upload and download, so I upload the package using the option versionOption: 'patch'
in the build stage and download the package in the deploy stage using vstsPackageVersion: '0.0.*'
.
This was an OK solution until I faced the issue of running two builds one after another that upload to the same Azure artifacts feed, and since I have set up an approval between the build and deploy stages, both runs of the pipeline will result in the deployment of the latest artifact pushed to the feed.
After some investigation I have found that the Universal Package upload task has this option called publishedPackageVar that saves the name and version of the uploaded artifact in a variable. I could not find any documentation on this option, but from what I can see it stores the name and version as a string, separated by a space character: <artifact_name> <major>.<minor>.<patch>
, and the vstsPackageVersion requires only the <major>.<minor>.<patch>
part.
My question is: Has anyone used this publishedPackageVar option for a similar reason and what is the best way for it to be passed to the vstsPackageVersion option of the Universal Package download task?
All solutions I can think of require some ugly, multi-step workarounds (like getting the version from the variable with sed
and storing it as a new variable) which will just make my pipeline less readable.
Refer to this doc about Universal Package task
publishedPackageVar
Package Output Variable Provide a name for the variable that contains the published package name and version.
This value is composed of package name and version number by default. We couldn't modify the format in the task.
As you said, one way is to use a script to separate the version number from this variable and store it in a new variable.
I would like to share another method.
You can use the custom Package Version number. Then you can use the counter expression to construct an incremental version number.
The the variable can directly be used in two stages.
Here is an example:
pool:
vmImage: windows-2019
variables:
major: 2
minor: 9
restnumber: 1
patch: $[counter(variables['restnumber'], 0)]
version: $(major).$(minor).$(patch)
stages:
- stage: Build
jobs:
- job: upload
steps:
- task: UniversalPackages@0
inputs:
command: 'publish'
publishDirectory: '$(Build.sourcesdirectory)'
feedsToUsePublish: 'internal'
vstsFeedPublish: 'xx'
vstsFeedPackagePublish: 'xx'
versionOption: 'custom'
versionPublish: '$(version)'
- stage: Stage
jobs:
- job: download
steps:
- task: UniversalPackages@0
inputs:
command: 'download'
downloadDirectory: '$(System.DefaultWorkingDirectory)'
feedsToUse: 'internal'
vstsFeed: 'xx'
vstsFeedPackage: 'xx'
vstsPackageVersion: '$(version)'
In this example, you can reset the counter by changing the value of variable :restnumber.