我试图将一个变量从一个阶段传递到另一个阶段,但不知何故它不起作用,我确实在 Stackoverflow 上检查了其他答案,但它不起作用(或者也许我做错了什么)
我试图将变量 $NEW_VERSION 传递到
Cleanup
阶段,但输出为空:
这是我的 .yaml 管道:
stages:
- stage: ImageDeployment
jobs:
- job: NewImageDeployment
timeoutInMinutes: 180
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureKeyVault@2
displayName: 'Fetch Secrets'
inputs:
connectedServiceName: '$(GLOBAL_SERVICE_CONNECTION)'
keyVaultName: $(GLOBAL_KV_NAME)
secretsFilter: "Packer-Secret"
runAsPreJob: false
- task: AzureCLI@2
displayName: 'Check image version'
retryCountOnTaskFailure: 0
inputs:
azureSubscription: '$(GLOBAL_SERVICE_CONNECTION)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
while IFS=$'\t' read -a arr_image_versions; do
version=${arr_image_versions[0]}
date=${arr_image_versions[1]}
if [[ $date > $old_date ]]; then
last_version=$version
fi
old_date=$date
done <<< "$(az sig image-version list --gallery-image-definition xxxxxxx --gallery-name xxxxxxxx --resource-group xxxxxxxxx --query "[].[name, publishingProfile.publishedDate]" -o tsv)"
echo "Last image version: $last_version"
echo "##vso[task.setvariable variable=last_version]$last_version"
- task: Bash@3
name: CheckImageVersion
displayName: 'Increment image version'
inputs:
targetType: 'inline'
script: |
IFS='.' read -ra VERSION_PARTS <<< "$(last_version)"
PATCH_VERSION=${VERSION_PARTS[2]}
PATCH_VERSION=$((PATCH_VERSION + 1))
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$PATCH_VERSION"
echo "New image version: $NEW_VERSION"
echo "##vso[task.setvariable variable=NEW_VERSION;isOutput=true]$NEW_VERSION"
- template: tests/build_functionapp_testcase2.yaml@self
- template: tests/datafactory_testcase3.yaml@self
- stage: Cleanup
condition: failed()
jobs:
- job: DeleteImage
pool:
vmImage: 'ubuntu-latest'
variables:
NEW_VERSION: $[ stageDependencies.ImageDeployment.NewImageDeployment.outputs['CheckImageVersion.NEW_VERSION'] ]
steps:
- script: |
echo "NEW_VERSION in Cleanup stage: $(NEW_VERSION)"
- task: AzureCLI@2
displayName: 'Delete Packer Image'
inputs:
azureSubscription: '$(GLOBAL_SERVICE_CONNECTION)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "Deleting image version: $(NEW_VERSION)"
az sig image-version delete --gallery-image-definition xxxxxxxxxxxxxxxxxxxxxxx
在阶段“
Cleanup
”上,您错过了选项“dependsOn
”让阶段“Cleanup
”依赖于阶段“ImageDeployment
”。
如果未设置“
dependsOn
”关系,阶段“Cleanup
”无法从阶段“ImageDeployment
”获取输出。