我想不出一个解决方案,当Terraform返回退出代码2时,我将继续ADO Pipeline构建。无论是否检测到更改,Terraform通常返回退出代码0,但使用-deteailed-exitcode附在plan命令上:退出代码0不变,退出代码2不变。
我面临的问题是,如果管道没有计划的地形变更,那么管道将跳过其余阶段并报告成功,如果检测到变更,将继续在管道上运行。
- script: |
terraform plan -input=false -no-color -out=plan.tfplan -detailed-exitcode
# on changes detected, succeed; otherwise fail
if [ $? -eq 2 ]; then
exit 0
else
exit 1
fi
displayName: "Planning Terraform Changes"
name: TerraformPlan
condition: succeeded()
continueOnError: true
workingDirectory: "$(Build.SourcesDirectory)/terraform/$(ENVIRONMENT_NAME)"
env:
TF_IN_AUTOMATION: true
ARM_CLIENT_ID: $(NONPROD_ARM_CLIENT_ID)
ARM_CLIENT_SECRET: $(NONPROD_ARM_CLIENT_SECRET)
ARM_SUBSCRIPTION_ID: $(NONPROD_ARM_SUBSCRIPTION_ID)
ARM_TENANT_ID: $(NONPROD_ARM_TENANT_ID)
- script: |
echo "Nothing to do"
displayName: "No changes detected"
name: NoChanges
condition: failed()
- script: |
echo "Changes detected"
displayName: "Changes Detected"
name: Changes
condition: succeeded()
- stage: ...
使用上述管道yaml,始终运行“检测到的更改”脚本。我不确定我是否要按照正确的方式进行操作,因此不胜感激。
您认为采用删除文件和使用管道变量的方法会更好吗,例如:Skip stage with manual approval in Azure DevOps Pipelines (YAML)
更新:
如果删除continueOnError: true
,则整个管道将在该阶段之后停止,并拾取“未检测到更改”。我正在尝试使管道进入另一个阶段。
答案 0 :(得分:0)
如果您可以将任务的输出捕获到变量中,则可以在下一个阶段利用custom conditional来选择执行。
steps:
- script: echo I did a thing
condition: and(succeeded(), eq('${{ SkipSteps }}', false))
答案 1 :(得分:0)
你实际上需要的是一个集合变量,然后在条件中使用它:
- script: |
terraform plan -input=false -no-color -out=plan.tfplan -detailed-exitcode
# on changes detected, succeed; otherwise fail
if [ $? -eq 2 ]; then
echo '##vso[task.setvariable variable=changedDetected]true'
fi
displayName: "Planning Terraform Changes"
name: TerraformPlan
condition: succeeded()
continueOnError: true
workingDirectory: "$(Build.SourcesDirectory)/terraform/$(ENVIRONMENT_NAME)"
env:
TF_IN_AUTOMATION: true
ARM_CLIENT_ID: $(NONPROD_ARM_CLIENT_ID)
ARM_CLIENT_SECRET: $(NONPROD_ARM_CLIENT_SECRET)
ARM_SUBSCRIPTION_ID: $(NONPROD_ARM_SUBSCRIPTION_ID)
ARM_TENANT_ID: $(NONPROD_ARM_TENANT_ID)
- script: |
echo "Nothing to do"
displayName: "No changes detected"
name: NoChanges
condition: and(succeeded(), ne(variables['changedDetected'], 'true'))
- script: |
echo "Changes detected"
displayName: "Changes Detected"
name: Changes
condition: and(succeeded(), eq(variables['changedDetected'], 'true'))