Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Created March 20, 2022 12:13
Show Gist options
  • Save aimtiaz11/0c122269d424c88c4563477178781a80 to your computer and use it in GitHub Desktop.
Save aimtiaz11/0c122269d424c88c4563477178781a80 to your computer and use it in GitHub Desktop.
Azure DevOps - Validate that build branch is not behind master
steps:
- task: PowerShell@2
displayName: Validate - Build branch is not behind master
condition: or(eq(variables['Build.SourceBranchName'], 'develop'),startsWith(variables['Build.SourceBranchName'], 'release-'),startsWith(variables['Build.SourceBranchName'], 'bugfix-'),startsWith(variables['Build.SourceBranchName'], 'hotfix-'))
inputs:
targetType: 'inline'
failOnStderr: true
script: |
$Uri = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/stats/branches?name=$(Build.SourceBranchName)&api-version=6.0"
Write-Host $Uri
$Response = Invoke-RestMethod -Uri $Uri -Method Get -Headers @{ Authorization = "Bearer $(System.AccessToken)" }
Write-Host $Response
$BehindCount = $Response.behindCount
if($BehindCount -eq 0) {
Write-Host Validation Passed
} else {
Write-Error -Message "Your $(Build.SourceBranchName) is $BehindCount commits behind of the default branch in your repository" -ErrorAction Stop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment