skip to Main Content

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?

2

Answers


  1. 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'] ]
    

    Ref: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#share-variables-across-pipelines

    https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#setvariable-initialize-or-modify-the-value-of-a-variable

    Login or Signup to reply.
  2. 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?

    https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#variables

    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:

    Set Variables

    Variables Level

    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"
      }
     ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search