0

I am running an azure pipeline to deploy applications to the cloud. Before deploying I want to check if the current version deployed on the cloud is not same as the version the user wants to deploy. I was looking into getting release name of the build which was deployed before. I am using Azure devops git as my repository.

Is there any way I can get the desired information using the Azure Devops Rest API?

Thanks in advance.

Nisha
  • 25
  • 3
  • Your only source of truth is your application. You should have app version exposed via endpoint and then you can make a call as a part of the deployment process. If app version is the same as the version you are going to deploy, break the process and that's it. Using information from Azure Rest could make a trouble, as if someone deploy application via another pipeline etc, you won't be able to detect this. – Krzysztof Madej May 19 '21 at 13:02
  • @KrzysztofMadej I agree.. I was thinking in the same lines, if I do go ahead and use REST API then it would be difficult to get the version information if the application is deployed via another pipeline. Thanks for the suggestion, I will look into this option. – Nisha May 20 '21 at 04:52

1 Answers1

1

I was looking into getting release name of the build which was deployed before. I am using Azure devops git as my repository.

We could use the REST API Releases - List with argument $top to get the latest release pipeline:

GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?definitionId={definitionId}&`$top={$top}&api-version=6.0

We could get the latest release pipeline Id:

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

$ReleasePipelineUrl = "https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?definitionId={definitionId}&`$top={$top}&api-version=6.0" 

Write-Host "URL: $ReleasePipelineUrl"

$ReleasePipelineInfo = (Invoke-RestMethod -Uri $ReleasePipelineUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 

$LatestReleaseId = $ReleasePipelineInfo.value.id | ConvertTo-Json -Depth 100

Write-Host "LatestReleaseId = $LatestReleaseId"

After getting the LatestReleaseId, we could use the REST API Releases - Get Release:

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

to get the detailed info, like artifact name about the latest pipeline:

enter image description here

enter image description here

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


$ReleaseArtifactUrl = "https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/$($LatestReleaseId)?api-version=5.1" 

Write-Host "URL: $ReleaseArtifactUrl"

$ReleaseArtifactInfo = (Invoke-RestMethod -Uri $ReleaseArtifactUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 


$ReleaseArtifactName = $ReleaseArtifactInfo.artifacts.definitionReference.version.name | ConvertTo-Json -Depth 100

Write-Host "ReleaseArtifactName = $ReleaseArtifactName"

Now, we get the release name of the build which was deployed latest time.

To resolve your question, we could add a powershell task in your build pipeline to invoke above REST API to check the Release name.

And add another task to compare whether the release name is the same as the artifact name generated by this build, we could use REST API to get it Artifacts - Get Artifact. If they are different, call the REST API to trigger the release pipeline. If they are the same, do nothing.

Therefore, we can judge whether there is an update by the name or version of each artifact generated, but it seems difficult to do it if we want to directly compare the version of the app. Unless we throw out this information about the app when building our pipeline, and then use the REST API to get it, for example, store it in a file. In any case, these methods require a bit of familiarity with the REST API and hope to help you.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thanks Leo, I was looking into something like this. The problem for me is that when I query the releases list, I get an empty list. That is where I am stuck. All I have are build pipelines. Is there any way where I can get which build pipeline deployed the artifacts to the cloud or do I explicitly need a release pipeline to get the artifact name? – Nisha May 20 '21 at 10:25
  • @Nisha, You could share your scripts in your question, so that I could check it, Erase personal information. – Leo Liu May 21 '21 at 09:41
  • I have used a different approach, I am storing the value of the latest release version in the variable groups. Thanks a lot for your inputs. Even though I have not used this approach, I am giving this answer my vote as I think this can be used in case of release pipelines. – Nisha Jun 03 '21 at 06:30
  • @Nisha, Glad to know you have resolved this issue with another way. And thank you for your affirmation of my answer. Have a nice day : ). – Leo Liu Jun 03 '21 at 06:33
  • And what about current and active deployment? For example, you have several releases: Release-1(succeeded), Release-2(failed), Release-3(succeeded), Release-4(notStarted). Then, consider that after that, I go to Release-1 and re-deploy. Your script will return Release-4 (or 3 if filter in url by deploymentStatus), but I don't know how to get current active deployment – Alejandro Galera Aug 19 '22 at 11:24