是否可以加载如下所示的 json 文件(范围为作业):
{
"first-api": {
"subscriptionId": {
"dev": "1234",
"test": "5678",
}
}
}
并使用它有点像:
- template: 'templates/deploySomething.yml'
parameters:
subscriptionId: loadedJsonData.[parameters.api].subscriptionId.[parameters.stage]
提前谢谢你
恐怕您想要做的事情在Azure管道中是不可能的。
你可以这样做:
考虑json文件
$(System.DefaultWorkingDirectory)/json-data.json
:
{
"first-api": {
"subscriptionId": {
"dev": "1234",
"test": "5678"
}
}
}
以下管道解析 json 文件并根据所选 API 名称和环境提取订阅 ID:
name: json_parse_$(Date:yyMMdd)$(Rev:rr)
parameters:
- name: apiName
displayName: API Name
type: string
default: 'first-api'
- name: environment
displayName: Environment
type: string
default: 'dev'
values:
- 'dev'
- 'test'
trigger: none
variables:
- name: jsonFilePath
value: $(System.DefaultWorkingDirectory)/json-data.json
- name: jsonPath
value: '."${{ parameters.apiName }}".subscriptionId.${{ parameters.environment }}'
steps:
- checkout: self
- script: |
ENVIRONMENT='${{ parameters.environment }}'
JSON_FILE_PATH='${{ variables.jsonFilePath }}'
JSON_PATH='${{ variables.jsonPath }}'
# Debug: Print the actual jq command to be executed
echo "jq command: jq -r \"$JSON_PATH\" \"$JSON_FILE_PATH\""
# Use jq to extract the subscription ID for the given environment
SUBSCRIPTION_ID=$(jq -r "$JSON_PATH" "$JSON_FILE_PATH")
echo "Subscription ID for $ENVIRONMENT environment: $SUBSCRIPTION_ID"
echo "Creating pipeline variable SUBSCRIPTION_ID"
echo "##vso[task.setvariable variable=SUBSCRIPTION_ID]$SUBSCRIPTION_ID"
displayName: Parse environment subscription ID
- template: /pipelines/steps/do-something-steps.yaml
parameters:
subscriptionId: $(SUBSCRIPTION_ID)
运行管道:
请注意,为了使用 jq 访问名称中带有连字符 (
-
) 的属性,您应该使用语法 ."property-name"
:
jq -r '."first-api".subscriptionId.dev' json-data.json