如何仅在ADF部署任务失败时运行触发器重新启用阶段?
解决方案
问题背景
我们有三个阶段,第二阶段(ADFDeploy)会先禁用ADF触发器,再执行AzureResourceGroupDeployment@2部署任务。如果该部署任务失败,流水线会直接退出,导致ADF触发器一直处于禁用状态。我们需要实现:
- 仅当第二阶段中**
AzureResourceGroupDeployment@2任务失败**时,才运行触发器重新启用阶段; - 如果第二阶段中除该部署任务外的其他任务失败,流水线直接退出,且不能使用
continueOnError参数。
修改步骤
1. 调整部署阶段(ADFDeploy)
给部署任务添加命名和输出变量,用于标记该任务是否失败:
stages: - stage: ADFDeploy displayName: ADF部署阶段 variables: # 你的变量配置 pool: # 你的代理池配置 jobs: - job: ADFDeployJob displayName: ADF部署任务 steps: - task: AzurePowerShell@5 displayName: 'Get All ADFTriggers' inputs: azureSubscription: '${{ parameters.AzureServiceCon }}' ScriptType: 'InlineScript' preferredAzurePowerShellVersion: '2.2.8' Inline: | some code azurePowerShellVersion: 'LatestVersion' - task: PythonScript@0 inputs: scriptSource: 'filePath' scriptPath: '$(System.DefaultWorkingDirectory)/AzurePipelines/YAML/Pipelines/02_ApplicationDeployment/YAML_CD/ADF_CD/reenable-triggers.py' pythonInterpreter: 'C:\Python310\python.exe' displayName: "Get ADF Triggers" - task: PublishPipelineArtifact@1 inputs: targetPath: '$(Build.SourcesDirectory)/triggerName.txt' artifact: 'ADFTrgList' publishLocation: 'pipeline' - task: AzurePowerShell@5 displayName: 'Disable adf triggers' inputs: azureSubscription: '${{ parameters.AzureServiceCon }}' ScriptType: 'FilePath' ScriptPath: '$(System.DefaultWorkingDirectory)/AzurePipelines/YAML/Pipelines/02_ApplicationDeployment/YAML_CD/ADF_CD/PrePostDeployment.ps1' ScriptArguments: '-armTemplate "$(Pipeline.Workspace)/ArmTemplates/ARMTemplateForFactory.json" -ResourceGroupName $(paramResourceGroupName) -DataFactoryName $(paramfactoryName) -predeployment $true -deleteDeployment $false' preferredAzurePowerShellVersion: '2.2.8' azurePowerShellVersion: 'LatestVersion' # 修改后的部署任务:添加命名和输出变量 - task: AzureResourceGroupDeployment@2 displayName: 'Azure Deployment:Create ADF' name: ADFFactoryDeployment # 给任务命名,用于后续引用输出变量 outputs: deploymentFailed: ${{ failed() }} # 输出变量,任务失败时为true,成功时为false inputs: azureSubscription: '${{ parameters.AzureServiceCon }}' resourceGroupName: "$(paramResourceGroupName)" location: "$(paramResourceGroupLocation)" csmFile: '$(Pipeline.Workspace)/**/ARMTemplateForFactory.json' csmParametersFile: '$(Pipeline.Workspace)/**/ARMTemplateParametersForFactory.json' deploymentMode: ${{ parameters.paramDeploymentMode }} enabled: ${{ parameters.paramEnabled }}
2. 调整触发器重新启用阶段
修改阶段依赖和触发条件,确保仅在部署任务失败时运行;同时添加下载artifact的步骤,确保能读取到触发器列表文件:
stages: - stage: ADFTriggers displayName: Re-EnableADFTriggers-${{ parameters.environmentShortName }} dependsOn: ADFDeploy # 改为依赖ADF部署阶段 # 触发条件:仅当部署任务执行且失败,同时环境符合要求时运行 condition: | and( exists(dependencies.ADFDeploy.outputs['ADFDeployJob.ADFFactoryDeployment.deploymentFailed']), eq(dependencies.ADFDeploy.outputs['ADFDeployJob.ADFFactoryDeployment.deploymentFailed'], 'true'), in(variables['environmentName'], 'Prod', 'QA', 'STest') ) variables: - template: ../../../YAML/Variables/${{ parameters.environmentShortName }}/variables-master.yml pool: name: ${{ parameters.agentPoolName }} vmImage: 'windows-latest' demands: - msbuild - visualstudio - vstest jobs: - deployment: ADFTrg_${{ parameters.environmentShortName }} displayName: ADFTriggers ${{ parameters.environmentShortName }} pool: name: ${{ parameters.agentPoolName }} vmImage: 'windows-latest' demands: - msbuild - visualstudio - vstest workspace: clean: all environment: ADF-TRG-${{ parameters.environmentShortName }} strategy: runOnce: deploy: steps: - checkout: 'YAML' # 添加下载artifact的步骤,获取触发器列表文件 - task: DownloadPipelineArtifact@2 displayName: '下载ADF触发器列表' inputs: artifact: 'ADFTrgList' path: '$(System.DefaultWorkingDirectory)' - task: AzurePowerShell@5 displayName: '重新启用ADF触发器' inputs: azureSubscription: '${{ parameters.AzureServiceCon }}' ScriptType: 'InlineScript' preferredAzurePowerShellVersion: '2.2.8' Inline: | # 检查触发器名称文件是否存在 $triggerFile = 'triggerName.txt' if (Test-Path $triggerFile) { # 读取文件中的触发器名称 $triggerNames = Get-Content -Path $triggerFile # 遍历触发器名称并重新启用 foreach ($name in $triggerNames) { Start-AzDataFactoryV2Trigger -DataFactoryName $(paramfactoryName) -ResourceGroupName $(paramResourceGroupName) -TriggerName $name -Force Write-Output "已重新启用触发器: $name" } } else { Write-Output "未找到触发器名称文件。" } azurePowerShellVersion: 'LatestVersion'
逻辑说明
部署阶段:
- 前序任务失败时,流水线会直接终止,不会执行部署任务,也不会触发重新启用阶段;
- 前序任务成功后,执行部署任务,通过
outputs输出任务失败状态。
重新启用阶段:
exists()函数确保部署任务已执行(即前序任务都成功);eq(..., 'true')确保部署任务确实失败;- 只有同时满足这两个条件,才会运行重新启用触发器的逻辑;
- 添加
DownloadPipelineArtifact步骤,确保能获取到之前发布的触发器列表文件。
内容的提问来源于stack exchange,提问作者Abhishek Singh
相关产品推荐
相关产品推荐

