I have an Azure pipeline for my test branch. I am trying to build a way to automatically increment the build number. The build number is saved in a variable build_number
inside a group BUILD_NUMBER
.
I can read the build number, but I'm trying to update it from the pipeline so it can increment by 1 without having to manually update it myself.
Things Im not sure on:
This is the powershell script I am running:
$authHeader = @{Authorization = "Bearer $(System.AccessToken)"}
$body = @{
description = "Build number"
name = "BUILD_NUMBER_GROUP"
providerData = @{
repository = @{
id = "some_id"
}
}
type = "Vsts"
variables = @{
build_number = @{
isSecret = false
isReadOnly = false
value = "2" # this is programaticaly done in an earlier script
}
}
variableGroupProjectReferences = @(
@{
name = "BUILD_NUMBER_GROUP"
description = "Build number"
projectReference = @{
id = "100"
name = "project_name"
}
}
)
} | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "https://dev.azure.com/{organisation}/{project}/_apis/distributedtask/variablegroups/100?api-version=7.2-preview.2" -Method Put -Body $body -Headers $authHeader -ContentType "application/json"
displayName: "Increment build number"
I am unsure if this is along the right path, but I am getting this error:
Line |
42 | Invoke-RestMethod -Uri "https://dev.azure.com/{organisation}/{project} …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| { "$id": "1", "innerException": null, "message": "Value cannot be
| null.\r\nParameter name: variableGroupParameters", "typeName":
| "System.ArgumentNullException, mscorlib", "typeKey":
| "ArgumentNullException", "errorCode": 0, "eventId": 0 }
Based on your description, you need to use the Rest API: Variablegroups - Update to update the variable group in Azure Pipeline.
You can do the following changes in your PowerShell code:
1.You can simplify your Request Body to the following format:
$body = @{
description = "Build number"
name = "BUILD_NUMBER_GROUP"
type = "Vsts"
variables = @{
build_number = @{
isSecret = "false"
isReadOnly = "false"
value = "2"
}
}
}| ConvertTo-Json -Depth 10
2.You can use the API version:api-version=5.1-preview.1
Rest API url:
PUT https://dev.azure.com/{organization}/{project}/_apis/distributedtask/variablegroups/{groupId}?api-version=5.1-preview.1
Full PowerShell script sample:
$authHeader = @{Authorization = "Bearer $(System.AccessToken)"}
$body = @{
description = "Build number"
name = "BUILD_NUMBER_GROUP"
type = "Vsts"
variables = @{
build_number = @{
isSecret = "false"
isReadOnly = "false"
value = "2"
}
}
}| ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "https://dev.azure.com/{org}/{project}/_apis/distributedtask/variablegroups/100?api-version=5.1-preview.1" -Method Put -Body $body -Headers $authHeader -ContentType "application/json"
On the other hand, if you want to use the API version: api-version=7.2-preview.2
Rest API url:
PUT https://dev.azure.com/{organization}/_apis/distributedtask/variablegroups/{groupId}?api-version=7.2-preview.2
You can use the following Request Body:
$body = @{
description = "Build number"
name = "BUILD_NUMBER_GROUP"
type = "Vsts"
variables = @{
build_number = @{
isSecret = "false"
isReadOnly = "false"
value = "2"
}
}
variableGroupProjectReferences = @(
@{
name = "BUILD_NUMBER_GROUP"
description = "Build number"
projectReference = @{
id = "{ProjectID}"
name = "{Projectname}"
}
}
)
}| ConvertTo-Json -Depth 10
Full PowerShell sample:
$authHeader = @{Authorization = "Bearer $(System.AccessToken)"}
$body = @{
description = "Build number"
name = "BUILD_NUMBER_GROUP"
type = "Vsts"
variables = @{
build_number = @{
isSecret = "false"
isReadOnly = "false"
value = "2"
}
}
variableGroupProjectReferences = @(
@{
name = "BUILD_NUMBER_GROUP"
description = "Build number"
projectReference = @{
id = "{ProjectID}"
name = "{Projectname}"
}
}
)
}| ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "https://dev.azure.com/{orgname}/{projectname}/_apis/distributedtask/variablegroups/100?api-version=7.2-preview.2" -Method Put -Body $body -Headers $authHeader -ContentType "application/json"
You can get the Project ID with the Rest API: Projects - List