Search code examples
jsonazure-devopsazure-pipelinesazure-repos

Azure Pipeline: Read values from json file in the repo and store it in pipeline variable (without powershell)


I have an azure repo that triggers an azure pipeline on every commit to any branch.

on this repo there is a json file file.json. With sample content:

{
 "key1": "value1",
 "key2": "value2" 
}

how can i read values from this json file in the pipeline and store them in pipeline variables? (I want to avoid powershell)


Solution

  • I found a solution. First you create a bash action with the following syntax:

    - bash: |
        echo "##vso[task.setvariable variable=varName;]$(jq .key1 file.json)"
      name: setVarFromJsonFileValue
    

    With jq .key1 file.json you can read the value of key1.

    jq is a cli tool what seems to be preinstalled at the pipeline agent (i used ubuntu image).

    Now value1 is stored in the pipeline variable varName and you can access it in the whole job like this:

    - script: |
        echo $(varName) 
      displayName: output value of var
    

    The output is value1.