5

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)

DaveVentura
  • 612
  • 5
  • 19
  • 2
    Does this answer your question? [import Azure Devops pipeline variables from json file](https://stackoverflow.com/questions/58597755/import-azure-devops-pipeline-variables-from-json-file) – Vince Bowdren Nov 27 '21 at 13:20
  • 1
    @VinceBowdren Thanks for the hint. I saw that too, I should have mentioned that I'm looking for a solution without powershell.... – DaveVentura Nov 27 '21 at 14:13
  • That's a crucial limitation; you'd best Edit the question to make that clear. – Vince Bowdren Nov 27 '21 at 16:40

1 Answers1

9

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.

DaveVentura
  • 612
  • 5
  • 19