Last active
July 8, 2019 17:29
-
-
Save DinoSourcesRex/48db15c50635225b970840425e46a3d1 to your computer and use it in GitHub Desktop.
Incrementing a Build Pipeline Variable on AzureDevops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In the Phase definition make sure to check "Allow scripts to access the OAuth token" otherwise this will fail. | |
$VariableName = "ProjectBuildNumber" | |
$defId = $env:SYSTEM_DEFINITIONID | |
# Uses built in environment variables to create the token and api call for us | |
# If you want to hit a release instead just change "build/definitions/" to "release/definitions" | |
$url = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/" + $defId + "?api-version=5.0" | |
Write-Host "URL: $url" | |
# Get the build definition | |
$pipeline = Invoke-RestMethod -Uri $url -Headers @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" | |
} -Method Get | |
# Increment the variable | |
$oldValue = $pipeline.variables.($VariableName).value; | |
$newValue = [int]$oldValue + 1; | |
$pipeline.variables.($VariableName).value = $newValue; | |
# Update the object | |
$updatedPipeline = $pipeline | ConvertTo-Json -Depth 100 | |
$updatedPipeline = [Text.Encoding]::UTF8.GetBytes($updatedPipeline) | |
Write-Host ("Updating build...") | |
$pipeline = Invoke-RestMethod -Uri $url -Headers @{ | |
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" | |
} -Method Put -ContentType "application/json" -Body $updatedPipeline -Verbose -Debug | |
Write-Host "Successfully incremented $VariableName's value from $oldValue to $newValue" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment