parameters: - name: VariableName displayName: Variable Name - name: VariableValue displayName: Variable Value steps: - task: PowerShell@2 displayName: Update Pipeline Variable - ${{parameters.VariableName}} env: SYSTEM_ACCESSTOKEN: $(System.AccessToken) inputs: targetType: inline script: | [string]$repositoryUrl = $env:SYSTEM_COLLECTIONURI [string]$repositoryName = $env:SYSTEM_TEAMPROJECT [string]$definitionId = $env:SYSTEM_DEFINITIONID [string]$variableName = ${{parameters.VariableName}} [string]$variableValue = ${{parameters.VariableValue}} [string]$accessToken = $env:SYSTEM_ACCESSTOKEN [string]$debugMode = $env.SYSTEM_DEBUG if ($debugMode -eq 'true') { Write-Host '##[section]Starting: DEBUG INFORMATION' Write-Host "##[debug]repositoryUrl = $repositoryUrl" Write-Host "##[debug]repositoryName = $repositoryName" Write-Host "##[debug]definitionId = $definitionId" Write-Host "##[debug]variableName = $variableName" Write-Host "##[debug]variableValue = $variableValue" Write-Host '##[section]Finishing: DEBUG INFORMATION' Write-Host '' } if($repositoryUrl.EndsWith('/')) { $repositoryUrl = $repositoryUrl.TrimEnd('/') } $headers = @{ 'Authorization' = "Bearer $accessToken" } # Get an overview of the build definition $definitionUrl = "$repositoryUrl/$repositoryName/_apis/build/Definitions/$($definitionId)?api-version=5.0" $definitionResponse = Invoke-RestMethod -Method Get -Uri $definitionUrl -Headers $headers -UseBasicParsing # If the build has the variable, update it. if ($definitionResponse.variables.$variableName) { $oldValue = $definitionResponse.variables.$variableName.value $definitionResponse.variables.$variableName.value = $variableValue Write-Output -InputObject "Updating $variableName from $($oldValue) to $($variableValue)..." $body = ConvertTo-Json -InputObject $definitionResponse -Depth 100 -Compress $response = Invoke-RestMethod -Method Put -Uri $definitionsUrl -Headers $headers -ContentType 'application/json' -Body $body -UseBasicParsing } pwsh: true