-1

I want to read data from a json file into my azure devops as variable.

I saw this post: import Azure Devops pipeline variables from json file

But the answer here is only powershell compatible.

Is ther any solution that works with linux and windows agents?

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

1 Answers1

1

First, powershell should not only works on windows but also works on linux.

Install PowerShell on Linux

Second, for your requirement, I write a demo YAML for you(based on python):

1, Get the value of the specific route of json, and set it as variables in pipeline concept.

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import json
      
      def get_json_content_of_specific_route(json_file_path, json_route):
          length = len(json_route)
          # print(length)
          
          #open json file
          with open(json_file_path, 'r') as f:
              data = json.load(f)
              content = data
              #get the content f data[json_route[0]][json_route[1]]...[json_route[length-1]]
              for i in range(length):
                  if i == 0:
                      content = data[json_route[i]]
                  else:
                      content = content[json_route[i]]
              
              #get the content
              return content
      
      json_file_path = 'Json_Files/parameters.json' #This is the path of the json file.
      route = ['parameters', 'secretsPermissions', 'value'] #This is the route of the content you want to get.
      
      content = get_json_content_of_specific_route(json_file_path, route)
      
      print(content)
      print("##vso[task.setvariable variable=myJobVar]"+str(content))
  
- powershell: |
    Write-Host $(myJobVar) #you can get the content after you using logging command to set the variables.

The above variables getting from json can be Temporary set and use in pipeline run.

See using logging command to set the variables.

This is the structure of my repository:

enter image description here

Set and get the variable successfully in pipeline run lifecycle:

enter image description here

2, If you want permanent variables, there are two situations.

1' When based on classic pipeline, you need to use these REST API to achieve your requirements:

Get pipeline definition

Change pipeline definition

A python script that can change the variables of the classic pipeline:

import json
import requests


org_name = "xxx"
project_name = "xxx"
pipeline_definition_id = "xxx"
personal_access_token = "xxx"

key = 'variables'
var_name = 'BUILDNUMBER'

url = "https://dev.azure.com/"+org_name+"/"+project_name+"/_apis/build/definitions/"+pipeline_definition_id+"?api-version=6.0"

payload={}
headers = {
  'Authorization': 'Basic '+personal_access_token
}

response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
json_content = response.text
def get_content_of_json(json_content, key, var_name):
    data = json.loads(json_content)
    return data[key][var_name].get('value')

def change_content_of_json(json_content, key, var_name):
    data = json.loads(json_content)
    data[key][var_name]['value'] = str(int(get_content_of_json(json_content,key,var_name)) + 1)
    return data

json_data = change_content_of_json(json_content, key, var_name)


url2 = "https://dev.azure.com/"+org_name+"/"+project_name+"/_apis/build/definitions/"+pipeline_definition_id+"?api-version=6.0"

payload2 = json.dumps(json_data)
headers2 = {
  'Authorization': 'Basic '+personal_access_token,
  'Content-Type': 'application/json'
}

response2 = requests.request("PUT", url2, headers=headers2, data=payload2)

2' If your pipeline is based on non-classic pipeline, then you need to change the variables part of YAML file definition.

After you change the YAML definition during pipeline run, you can follow this to push back the changes:

Push Back Changes to Repository

Also, you can make your pipeline based on variables group and update the variables group via REST API:

Variablegroups - Update

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10