I'm trying to add a new branch to the target environment, specifically UAT, as part of the pre-condition artifacts. However, when I run the code, the release definition isn't displaying—it's just showing a blank page. Could someone help review and improve my code?
Here is my code snippet
$organization = "org"
$project = "contoso"
$token = "weird_token"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$definitionids = 510
$branchName = "release/24.3"
$targetEnv = "UAT"
foreach ($definitionid in $definitionids)
{
$url = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions/${definitionId}?api-version=7.0"
$Def = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token" } -Method GET -ContentType application/json
$envId = 0
$targetEnvId = 0
foreach ($environment in $Def.environments)
{
$environmentName = $environment.name
$envId++
if ( $environmentName -eq $targetEnv )
{
Write-Host "INFO: Environment Name is $environmentName"
$targetEnvId = $envId
foreach ($condition in $environment.conditions)
{
if ($condition.conditionType -eq 'artifact')
{
$newCondition = @{
sourceBranch = "$branchName"
tagFilter = $null
useBuildDefinitionBranch = $false
createReleaseOnBuildTagging = $false
}
Write-Host "INFO: Target Environment ID is $targetEnvId"
$artifactConditions = $Def.environments[$targetEnvId].conditions
$artifactConditions += $newCondition
$Def.environments[$targetEnvId].conditions = $artifactConditions
$updateDef_body = @($Def) | ConvertTo-Json -Depth 100
$updateDef_body
Write-Host "INFO: adding release branch to release '$branchName' to release pipeline"
$updateDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions?api-version=7.0"
$updateDef = Invoke-RestMethod -Uri $updateDef_uri -Headers @{Authorization = "Basic $token" } -Method PUT -Body $updateDef_body -ContentType application/json
$updateDef
}
}
}
}
}
Here is the updated script to process the filter value for $newCondition
$organization = "org"
$project = "contoso"
$token = "weird_token"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$definitionids = 510 # 38 in my case
$branchName = "release/24.3"
$targetEnv = "UAT"
foreach ($definitionid in $definitionids)
{
$definitionid
$url = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions/${definitionId}?api-version=7.0"
$Def = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token" } -Method GET -ContentType application/json
$Def
$Def | ConvertTo-Json -Depth 100 # | Out-File "C:\Users\Alvin.AZ\Desktop\Def.json"
foreach ($environment in $Def.environments)
{
$environmentName = $environment.name
if ( $environmentName -eq $targetEnv )
{
Write-Host "INFO: Environment Name is $environmentName"
Write-Host "INFO: artifactConditions are:"
$artifactConditions = $environment.conditions
$artifactConditions | ConvertTo-Json -Depth 100
Write-Host "========================================New condition========================================"
$filterVaule = @{
sourceBranch = "$branchName"
tags = @()
useBuildDefinitionBranch = $false
createReleaseOnBuildTagging = $false
}
$filterJsonVaule = $filterVaule | ConvertTo-Json -Depth 100 -Compress
$filterJsonVaule
$newCondition = @{
conditionType = "artifact"
name = "_PublishPipelineArtifacts-CI"
value = "$filterJsonVaule"
}
$newCondition | ConvertTo-Json -Depth 100
Write-Host "========================================Environemnt UAT conditions========================================"
$environment.conditions += $newCondition
$environment.conditions | ConvertTo-Json -Depth 100
Write-Host "========================================Update release pipeline definition ========================================"
$updateDef_body = ConvertTo-Json($Def) -Depth 100 # | Out-File "C:\Users\Alvin.AZ\Desktop\newDef.json"
$updateDef_body
Write-Host "INFO: adding release branch to release '$branchName' to release pipeline"
$updateDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions?api-version=7.0"
$updateDef = Invoke-RestMethod -Uri $updateDef_uri -Headers @{Authorization = "Basic $token" } -Method PUT -Body $updateDef_body -ContentType application/json
$updateDef
}
}
}
It appears you're checking if ($condition.conditionType -eq 'artifact')
and adding a new artifact condition regardless of the existing conditions. This could potentially lead to unintended behavior in your release pipeline definition, when there is no pre-existing artifact conditions. While we don't know what your current release pipeline definition is like, below is a sample PowerShell script for your reference to add new artifact condition for UAT
environment. Please pay attention to the new condition section and the proper indentation and formatting of $newCondition
.
$organization = "org"
$project = "contoso"
$token = "weird_token"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$definitionids = 510 # 38 in my case
$branchName = "release/24.3"
$targetEnv = "UAT"
foreach ($definitionid in $definitionids)
{
$definitionid
$url = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions/${definitionId}?api-version=7.0"
$Def = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token" } -Method GET -ContentType application/json
foreach ($environment in $Def.environments)
{
$environmentName = $environment.name
if ( $environmentName -eq $targetEnv )
{
Write-Host "INFO: Environment Name is $environmentName"
Write-Host "INFO: artifactConditions are:"
$artifactConditions = $environment.conditions
$artifactConditions | ConvertTo-Json -Depth 100
Write-Host "========================================New condition========================================"
$newCondition = @{
"conditionType" = "artifact"
"name" = "_PublishPipelineArtifacts-CI"
"value" = @"
{"sourceBranch":"$branchName","tags":[],"useBuildDefinitionBranch":false,"createReleaseOnBuildTagging":false}
"@
}
$newCondition | ConvertTo-Json -Depth 100
Write-Host "========================================Environemnt UAT conditions========================================"
$environment.conditions += $newCondition
$environment.conditions | ConvertTo-Json -Depth 100
Write-Host "========================================Update release pipeline definition ========================================"
$updateDef_body = ConvertTo-Json($Def) -Depth 100 # | Out-File "C:\Users\Alvin.AZ\Desktop\newDef.json"
$updateDef_body
Write-Host "INFO: adding release branch to release '$branchName' to release pipeline"
$updateDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions?api-version=7.0"
$updateDef = Invoke-RestMethod -Uri $updateDef_uri -Headers @{Authorization = "Basic $token" } -Method PUT -Body $updateDef_body -ContentType application/json
$updateDef
}
}
}