Skip to content

Instantly share code, notes, and snippets.

@vtml
Last active March 30, 2019 05:16
Show Gist options
  • Save vtml/cc6d010440ac2795e0486a1081b4ec3e to your computer and use it in GitHub Desktop.
Save vtml/cc6d010440ac2795e0486a1081b4ec3e to your computer and use it in GitHub Desktop.
param(
[string] $LastGitCommitId,
[string] $AccessToken,
[string] $AzureDevOpsAccountName,
[string] $TeamProjectId,
[string] $ReleaseDefinitionId
)
if ([string]::IsNullOrEmpty($LastGitCommitId))
{
$hdrs = @{}
$hdrs.Add("Authorization","Bearer $AccessToken")
try
{
$LatestSuccessfulRelease = Invoke-RestMethod -Method Get -Uri "https://$AzureDevOpsAccountName.vsrm.visualstudio.com/$TeamProjectId/_apis/release/releases?definitionId=$ReleaseDefinitionId&resultFilter=succeeded&`$top=1" -ContentType 'application/json' -Headers $hdrs
if ($LatestSuccessfulRelease.count -eq 1)
{
$ReleaseDetailUrl = $LatestSuccessfulRelease.value[0].url
try
{
$ReleaseArtifactDetail = Invoke-RestMethod -Method Get -Uri "$ReleaseDetailUrl" -ContentType 'application/json' -Headers $hdrs
if ($ReleaseArtifactDetail.PSobject.Properties.name -notcontains "innerException")
{
$artifacts = $ReleaseArtifactDetail.artifacts | where {$_.definitionReference.project.id -eq $TeamProjectId}
if ($artifacts -ne $null)
{
$GitCommitId = $artifacts[0].definitionReference.pullRequestMergeCommitId.id
Write-Host "##vso[task.setvariable variable=LastGitCommitId;]$GitCommitId"
}
}
else
{
Write-Host "##vso[task.complete result=Failed;]Error in getting Release Details"
}
}
catch
{
Write-Host "##vso[task.complete result=Failed;]Error in getting Release Details"
}
}
else
{
Write-Host "##vso[task.complete result=Failed;]Error in getting the latest successful release."
}
}
catch
{
Write-Host "##vso[task.complete result=Failed;]Error in getting the latest successful release."
}
}
@neetiks
Copy link

neetiks commented Mar 30, 2019

Hi Vincent,
Can you please help with how to use this script in VSTS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment