All,
I have the below Azure DevOps pipeline setup that copy keyvault secrets from one KV to another. As you can see, I have two tasks: 1) one to read the secrets and 2) one to write the secrets. I am having difficulties figuring out how to pass the "$secrets" variable (thru "echo "##vso[task.setVariable variable=sourceSecrets]$json") from the first task to the second task.
stages:
- stage: "Test1"
displayName: "Test1 - Copy KV"
jobs:
- deployment : "Deploy"
timeoutInMinutes: 120
variables:
sourceSecrets: ""
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI@2
inputs:
azureSubscription: $(ServiceConnection1)
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
if ("$(mysubscription1)"){
az account set --subscription "mysubscription1"
}
$secNames = az keyvault secret list --vault-name "kvName1" -o json --query "[].name" | ConvertFrom-Json
Write-Host 'Reading secrets...'
$secrets = $secNames | % {
$secret = az keyvault secret show --name $_ --vault-name "kvName1" -o json | ConvertFrom-Json
[PSCustomObject]@{
name = $_;
value = $secret.value;
}
}
$json = $($secrets | ConvertTo-Json)
echo "##vso[task.setVariable variable=sourceSecrets]$json"
- task: AzureCLI@2
inputs:
azureSubscription: $(ServiceConnection2)
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
if ("$(mysubscription2)"){
az account set --subscription $(mysubscription2)
}
$secrets = "$(sourceSecrets)" | ConvertFrom-Json
$secrets.foreach{
Write-Host 'Writing secrets:'
az keyvault secret set --vault-name $(kvName2) --name $_.name --value $_.value --output none
Write-Host '---->' $_.name
}
When the pipeline executes, tasks one executes fine. However, the 2nd task errored out with the following:
ConvertFrom-Json : Conversion from JSON failed with error: Error reading JArray from JsonReader. Path '', line 1, position 1.
At /home/vsts/work/_temp/azureclitaskscript1620360635888_inlinescript.ps1:4 char:18
+ $secrets = "[" | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
I did some checking, it appears the $(sourceSecrets) variable contain only "[" instead of the entire json content. This means the "echo "##vso[task.setVariable variable=sourceSecrets]$json" line from the first task is excluding everything after "[". I can't figure out why it is doing that. Ideas?
Thanks in advance.