CI Pipeline REST call

Hereโ€™s how to call a REST Server to retrieve values. These can also be stored in the pipeline variables, even in the secret ones. Get the server address and endpoint key from the pipeline variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$Server = "$(Server)"
$SecretKey = "$(SecretKey)"

$uri = "https://$($Server)/KeyVault/GetSecretUsingCache?secretKey=$($SecretKey)"
Write-Host "Call from $($uri)"

$response = Invoke-RestMethod `
    -Uri $uri `
    -Method Get `
    -TimeoutSec 30
    
$secretValue = $response.secret

if ([string]::IsNullOrWhiteSpace($secretValue)) {
    throw "Secret was empty or missing in REST response"
}

Write-Host "Secret successfully retrieved"
Write-Host $secretValue # Only for Test usage!!

# Set Azure DevOps pipeline variable (secret)
$pipeSecret = "$(RESTSecret)"
Write-Host "Secret from Pipe Var before: $($pipeSecret.ToCharArray())" # reveals the secret instead of printing ***

Write-Host "##vso[task.setvariable variable=RESTSecret;issecret=true]$secretValue"
Write-Host "Attention: The secret from the pipeline level secured variable can only be used in the next task!"

Write-Host "Done."

Note: You cannot use the overwritten pipeline var right away after the ##vso[task.setvariable variable=RESTSecret;issecret=true]$secretValue call. This can only be accessed in the next task!

1
2
3
4
$pipeSecret = "$(RESTSecret)"
Write-Host "Secret from Pipe Var after: $($pipeSecret.ToCharArray())"

Write-Host "Done."