我们有一个管道,其中包括“terraform plan”和“terraform apply”作为单独的 CI 步骤,因此在生产中我们可以在应用之前手动审查更改(但是在审查应用程序/登台时,我们很高兴它们自动运行)。该计划作为作业之间的工件传递。
我们遇到了一些问题,开发人员重新运行“terraform apply”作业而没有重新运行“terraform plan”。我正在尝试找出如何识别并防止它。
我很惊讶 terraform 计划不包括 terraform 状态的哈希,因此 apply 可以识别状态已更改并拒绝继续。
有没有建议的方法来解决这个问题?我们尝试过:
- 在 terraform 中搜索选项以避免这种情况(目前还没有)
- 在 gitlab 中搜索选项以避免这种情况(目前还没有)
我们目前正在考虑在计划阶段获取我们自己的 tfstate 文件校验和,然后在应用阶段开始时对其进行检查——但我不禁觉得这应该已经存在了。
(状态存储在 S3 存储桶中。我们也使用 dynamodb 进行锁定)
删减 .gitlab-ci.yml 以作说明:
stages:
- plan
- apply
terraform plan:
image: hashicorp/terraform:0.12.26
stage: plan
script:
- terraform init
- terraform plan -out terraform.plan
artifacts:
paths:
- terraform.plan
terraform apply:
image: hashicorp/terraform:0.12.26
stage: apply
script:
- terraform apply -auto-approve terraform.plan
rules:
- if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
when: manual
- when: on_success