仅当计划不为空时,Terraform 才适用于 GitLab CI/CD

jal*_*aix 7 gitlab gitlab-ci terraform

我使用 Gitlab CI/CD 通过 Terraform 来配置基础设施。我目前有一个 3 阶段管道(initplanapply),非常适合手动应用作业。计划作业与应用作业共享计划工件。

有时计划是空的(没有资源可以更改),但申请工作仍然是强制性的。您知道如何避免在计划为空时运行应用作业吗?或者当计划为空时自动(而不是手动)运行应用作业?

Jai*_*e S 2

最简单的实现方法是-detailed-exitcode在 Terraform 计划步骤中使用。根据 Terraform 文档:

  -detailed-exitcode  Return detailed exit codes when the command exits. This
                  will change the meaning of exit codes to:
                  0 - Succeeded, diff is empty (no changes)
                  1 - Errored
                  2 - Succeeded, there is a diff
Run Code Online (Sandbox Code Playgroud)

例如,在终端中运行该计划:

terraform plan -detailed-exitcode
Run Code Online (Sandbox Code Playgroud)

然后在你的终端中运行:

echo $?
Run Code Online (Sandbox Code Playgroud)

如果计划包含任何更改,您的价值将不会为零。您只需使用 If 语句评估 CI/CD 中的该值即可决定是否要运行 apply 。

  • 感谢您的回答,但这并不能解决我的问题,因为我需要在 Gitlab CI/CD 中进行手动操作来决定是否必须应用该计划。您的解决方案仅适用于执行计划并决定是否应用该计划的单个 Gitlab 作业(因此无需手动验证)。 (2认同)