0
votes

enter image description hereWe have our release definitions in Azure DevOps with multiple sources of artifacts. One of the artifacts being the build artifact and the second being the azure repos Git.

Can the release be triggered whenever there is a new artifact is available (either the build artifact or the azure git repos artifact)? Is this possible in Azure DevOps.

2
@LeoLiu-MSFT Thanks for the information, Could you let me know about the service connection to be provided while configuring the Invoke Rest Api task. - Santhosh
@LeoLiu-MSFT We are unable to select the service connection in the drop-down list. I have build and release administrator permissions. - Santhosh
Sorry, I am not understand what you are doing now, you could describe it in your question, and to use REST API to trigger the release, you can refer this document: developercommunity.visualstudio.com/content/problem/552318/… - Leo Liu-MSFT
@LeoLiu-MSFT I have added the Invoke Rest API task, Connection type is Generic. But the drop-down list is empty for "Generic Service Connection" even though the service connections are available under Service connection settings. - Santhosh
We are creating a service connection following this link: docs.microsoft.com/en-us/azure/devops/pipelines/library/… - Santhosh

2 Answers

0
votes

Yes, you can add 2 artifacts to a release pipeline. No, (according to your question title) you cannot mark more than 1 artifact as Primary. Yes, you can add CD Triggers to any number of artifact sources you desire to add to your pipeline.

Beware, CD triggers on repository changes does not necessitate that the artifact you'll be using from the Build source is different that a previous release. I don't know your situation, but I would probably suggest that you have 2 separate After Release stages, where each stage applies an artifact filter, so that you only do work with builds when new builds are available and only do work with code when commits are available.

enter image description here

0
votes

Can the release be triggered whenever there is a new artifact is available (either the build artifact or the azure git repos artifact)? Is this possible in Azure DevOps.

Sorry for any inconvenience.

Build/Release definitions currently only support a single trigger.

As workaround, you could set the Azure Repos Git artifact as primary artifacts to trigger Azure DevOps release definition.

For the build artifact, we could trigger the release definition from a build task in either build definition through REST API.

Check this ticket for some more details.

Update:

Detail how to use Rest API trigger release from build pipeline.

As I answered above, We could use the Rest API Releases - Update Release to trigger the release:

PATCH https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}?api-version=5.1-preview.6

When you open a release pipeline, then switch to log tab, you could get the releaseId and environmentId

enter image description here

Then, we add a Inline Powershell task in the build pipeline to invoke the API to trigger the release. The scripts like:

$url = "https://vsrm.dev.azure.com/<OrganizationName>/<ProjectName>/_apis/Release/releases/285/environments/370?api-version=5.1-preview.6"

$connectionToken="Your PAT Here"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))


$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$body=@"
  {
    "status":"inProgress",
    "scheduledDeploymentTime":null,
    "comment":""
  }
"@
Write-Host "$url"
$response= Invoke-RestMethod -Uri $url  -ContentType "application/json" -Body $body -headers @{authorization = "Basic $base64AuthInfo"} -Method PATCH 

If you execute this build pipeline, it will trigger the release pipeline.

As test, it works fine on my side.

Note:

When you use above scripts, you need go to the Agent Phase and select Allow Scripts to Access OAuth Token. See Use the OAuth token to access the REST API

Update2:

Powershell script work, but it downloads the old build artifact. However, we need to associate the release with latest build artifact, how can this be accomplished?

You could use another REST API to get the latest Release ID and the environmentId:

Definitions - Get

We could get the all Release ID for the definitionId:

"isDeleted": false,
"lastRelease": {
    "id": 285,
    "name": "xxxxxx",
    "artifacts": [],
    "_links": {},

Then use Select-Object -first 1 get the latest Release ID.

Next, we could use another API Releases - Get Release with releaseId, which we get by above Rest API, we could get the environmentId:

GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}?api-version=5.1

The output like:

"environments": [
    {
        "id": 370,
        "releaseId": 285,
        "name": "Dev",
        "status": "succeeded",
        "variables": {

Now, we get the latest release ID, we could get use the first REST API to trigger the release.

Hope this helps.