【问题标题】:How to specify JSON array transformation in azure-pipelines.yaml如何在 azure-pipelines.yaml 中指定 JSON 数组转换
【发布时间】:2022-11-04 17:47:36
【问题描述】:

如何在 azure-pipelines.yaml 中指定 JSON 数组转换 https://github.com/MicrosoftDocs/azure-devops-docs/issues/11032

我尝试分配对象类型的参数,但看到意外的序列错误。

【问题讨论】:

  • 共享您正在使用的任务定义,或者您可以使用代码直接处理 JSON 文件。看我的回答。 :)

标签: azure azure-pipelines pipeline


【解决方案1】:

该任务有限制:

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/file-transform?view=azure-devops

如果可能,请分享您的管道(或任务定义)的 YAML 定义。

我写了一个python脚本,它可以实现处理JSON文件中的数组(添加、更新、删除、获取):

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      #get json file parameters.json from Json_Files folder
      
      import os
      import json
      
      def add_json_content_of_specific_route(json_file_path, something_to_add, 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]]
              
              #add something to the content
              content.append(something_to_add)
      
              #write the data to json file
              with open(json_file_path, 'w') as f:
                  json.dump(data, f, indent=4)
      
      
      
              
      def remove_json_content_of_specific_route(json_file_path, something_to_remove, 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]]
              
              #remove something from the content
              content.remove(something_to_remove)
      
              #write the data to json file
              with open(json_file_path, 'w') as f:
                  json.dump(data, f, indent=4)
      
      def update_json_content_of_specific_route(json_file_path, something_to_update_from, something_to_update_to, 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]]
              try:
                  #update something from the content
                  content[content.index(something_to_update_from)] = something_to_update_to
              except:
                  print("something_to_update_from is not in the content")
      
              #write the data to json file
              with open(json_file_path, 'w') as f:
                  json.dump(data, f, indent=4)
      
      def get_json_content_of_specific_route_in_array_or_bicep_format(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
      #==========================================Define the parameters=====================================#
      
      json_file_path = 'Json_Files/parameters.json'
      something_to_add = 'something_to_add'
      something_to_remove = 'something_to_add'
      
      something_to_update_from = 'list'
      
      something_to_update_to = 'something_to_add2'
      
      # route = ['parameters', 'secretsPermissions', 'value']
      route = ['parameters', 'secretsPermissions', 'value']
      
      
      #==========================================Run the function==========================================#
      
      add_json_content_of_specific_route(json_file_path,something_to_add=something_to_add,json_route=route)
      remove_json_content_of_specific_route(json_file_path,something_to_remove=something_to_remove,json_route=route)
      update_json_content_of_specific_route(json_file_path,something_to_update_from=something_to_update_from,something_to_update_to=something_to_update_to,json_route=route)
      print(get_json_content_of_specific_route_in_array_or_bicep_format(json_file_path,json_route=route))

我这边的测试 JSON 文件结构:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多