I am forming a JSON dynamically during the pipeline run based on few pipeline parameters and pre-defined environment variables and trying to pass this JSON as an input in subsequent pipeline task.
jobs:
- job: PayloadCreation
pool: linux-agent (or windows)
steps:
- ${{ each app in apps }}:
- bash: |
payload=$(jq .artifact = [{"name": "${{ app.name}}, "version":"$(Build.BuildId)"}]' artifact.json)
echo $payload > artifact.json
echo "##vso[task.setvariable variable=payload]$payload"
I am getting the output of artifact.json
as well as variable $payload as follows -
"artifacts": [
{
"name":"service-a",
"version":"1.0.0"
},
{
"name":"service-b",
"version": "1.0.1"
}
]
}
Subsequently, I am trying to use this JSON variable to pass it as input in the following job and unable to do so.
- job: JobB
steps:
- task: SericeNow-DevOps-Agent-Artifact-Registration@1
inputs:
connectedServiceName: 'test-SC'
artifactsPayload: $(payload)
It is unable to read the JSON as input variable. I get the below error -
Artifact Registration could not be sent due to the exception: Unexpected token $ in JSON at position 0
Is there any other way a JSON could be passed as input variable?
CodePudding user response:
By default, variables are not available between jobs. In JobB
, the $(payload)
variable is not defined.
When setting the variable, you need to provide isOutput
: echo "##vso[task.setvariable variable=payload;isOutput=true]$payload"
When referencing the variable, you need to use the appropriate runtime expression:
variables:
payload: $[ dependencies.PayloadCreation.outputs['payload'] ]
CodePudding user response:
Is there any other way a JSON could be passed as input variable?
Strictly, no. Variables under the concept of DevOps pipeline doesn't support JSON object.
Why no?
Variables are always strings.
But this doesn't mean you can't pass the JSON information, if you want, passing string is the only way.
Is the task designed by yourself?
Convert string object to JSON object is not a difficult:
//convert string object to json object
var str = `
{
"artifacts": [
{
"name":"service-a",
"version":"1.0.0"
},
{
"name":"service-b",
"version": "1.0.1"
}
]
}
`;
var obj = JSON.parse(str);
console.log(obj.artifacts[0].name);
console.log(obj.artifacts[0].version);
Not sure how your task design, but Daniel's method of passing variables is correct.
You can do operations in your extension task code after convert the string object to JSON object.
Here I add other relevant information of the logging command:
By the way, in your question, the json is
"artifacts": [
{
"name":"service-a",
"version":"1.0.0"
},
{
"name":"service-b",
"version": "1.0.1"
}
]
}
Shouldn't it be like this?
{
"artifacts": [
{
"name":"service-a",
"version":"1.0.0"
},
{
"name":"service-b",
"version": "1.0.1"
}
]
}