8

In onprem Azure DevOps (Version 17.143.28621.4) is there a way to automatically promote a nuget package from one view to another using a task in a release definition?

When we trigger a release for a build, the artefacts (i.e. nuget packages) go through two stages

  1. PreRelease: The packages are pushed to the feed (ending up in the @Local view).
  2. Release: The packages should be promoted from the @Local to the @Release view.

Unfortunately stage 2. is currently a manual step in the Azure DevOps web UI. Can it be automated via the release definition?

If this is not possible, is there a better way to organise our release pipeline and package feeds/views to make a release of nuget packages fully automatic?

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
bitbonk
  • 48,890
  • 37
  • 186
  • 278

3 Answers3

4

Edit: Now I see you are with on-prem server so the extension will not work for you, so you must use the Update Package Version Rest API.

Add a PowerShell task to execute the API, something like this (it's for update work item, just change the body and the URL to the correct Rest API):

enter image description here

Don't forget to check the checkbox on the agent job options: Allow scripts to access the OAuth token.

If you use Azure DevOps you can install the Promote package to Release View extension and add it to your pipeline:

enter image description here

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • Thanks for pointing me into right direction. I am a bit confused by the first screenshot. It seems to show a completely different API (work items). What did you want to show with that screenshot? – bitbonk Jun 18 '19 at 08:04
  • 1
    Is just an example how to use the powershell inline to invoke rest api, you just need to change the body and the request url for your purpose according the correct rest api docs. – Shayki Abramczyk Jun 18 '19 at 08:05
  • I think this might be better as two separate answers. It's two very different approaches, using a powershell task or using the 3rd-party extension. – Vince Bowdren Jul 05 '19 at 12:27
  • I don’t like to separate answer from the same user. It’s looks like the user want more reputation.... – Shayki Abramczyk Jul 05 '19 at 14:28
3

The following powershell script you can use to promote multiple packages. This script assumes that all packages have the same version (one product consisting of several packages). It is working fine with our "DevOps Server 2019".

param(
  [Parameter(Mandatory=$True)]
  [string]$tfsCollectionUri,
  [Parameter(Mandatory=$True)]
  [string]$feedName,
  [Parameter(Mandatory=$True)]
  [string]$packageVersion,
  [Parameter(Mandatory=$True)]
  [string]$packageQuality
)

$ErrorActionPreference = "Stop"

[regex]$nameExpression = "(?<name>[^0-9]*)\."
$json = '{ "views": { "op":"add", "path":"/views/-", "value":"' + $packageQuality + '" } }'
Write-Verbose -Message $json

Get-ChildItem . -Filter *.nupkg | Foreach-Object {
  $matches = $nameExpression.Match($_.Name)
  $packageName = $matches.groups['name']
  $requestUri = $tfsCollectionUri + "/_apis/packaging/feeds/$feedName/nuget/packages/$packageName/versions/$packageVersion" + "?api-version=5.0-preview.1"
  Write-Verbose -Message $requestUri
  $reponse = Invoke-RestMethod -Uri $requestUri -UseDefaultCredentials -ContentType "application/json" -Method Patch -Body $json
  Write-Verbose -Message "Response: '$reponse'"
}

The parameter packageQuality should be e.g. "Release" or "Prerelease", without leading "@".

Many thanks to Shayki Abramczyk for the tip in the right direction.

lg2de
  • 616
  • 4
  • 16
  • I am accepting this as the answer, even though [Shayki Abramczyk](https://stackoverflow.com/a/56617111/3621055) gave the right initial pointer, because this answer contains the actual sample code. – bitbonk Jun 18 '19 at 09:36
0

Thanks all for sharing the code above. With minor modifications, it helped me get through my objective to promote the nuget packages to the prerelease/release views on Azure Feed. Mino but I believe would be helpful for those new: the format of the base URI for sending the PATCH command to is following -

https://pkgs.dev.azure.com/<azure-org-name>/<azure-project-name>

So the full URL will look something like -

https://pkgs.dev.azure.com/<azure-org-name>/<azure-project-name>/_apis/packaging/feeds/<feed-name>/nuget/packages/<nuget-package-name>/versions/<nuget-package-version>?api-version=5.0-preview.1

Hope this helps someone to achieve the promotion of packages quickly :)

RA7
  • 1
  • 1