该任务有限制:
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 文件结构: