Terraform提供以下退出代码:
0 = Succeeded with empty diff (no changes)
1 = Error
2 = Succeeded with non-empty diff (changes present)
只有在需要添加任何新资源或需要应用任何更改时,我才需要执行terraform apply
。
if [ $? -eq 0 ]; then
echo "No changes, not applying"
elif [ $? -eq 1 ]; then
echo "Terraform plan failed"
exit 1
elif [ $? -eq 2 ]; then
echo "Terraform apply needed"
fi
但我得到退出代码0
,即使terraform plan
命令输出:
Plan: 9 to add, 0 to change, 0 to destroy.
有什么问题吗?
对于terraform plan
返回那些退出代码,您必须像文档中详述的那样提供-detailed-exitcode
选项。
-detail -exitcode -在命令退出时返回详细的退出代码。
这应该是您的terraform plan
命令的样子,否则无论输出如何,您都会得到0
的退出代码:
terraform plan -detailed-exitcode ...