返回提示词目录第 1187 页

搭建基础设施代码持续集成流程

为基础设施代码编写变更检查、审批部署和定时漂移检测流程,提供凭据配置与验证步骤。

完整提示词

<context>
Running Terraform from laptops leads to applies nobody reviewed, state locks left behind, and drift between what is merged and what is deployed. Moving it to CI has its own traps. Applying a fresh plan on merge can apply something different from what reviewers saw, because main or the real infrastructure moved in between; the safer pattern applies the saved plan file from the reviewed commit, or re-plans and stops if it differs. Plans from pull requests can leak secrets in their output and, with untrusted forks, can run arbitrary code through providers and external data sources, so plan jobs need read-only credentials and must never run for forks with secrets. Each root module needs its own job so one environment's failure does not block others, and concurrency must be serialised per state to avoid lock fights.
</context>

<task>
Set up CI for this Terraform code:
<layout>
{{layout}}
</layout>
{{#approval}}
Approval rules: {{approval}}
{{/approval}}

1. Read the repository: root modules, backend configuration, provider and Terraform version constraints, `.terraform.lock.hcl`, and any existing CI or wrapper scripts.
2. On pull requests: detect which root modules changed (plus modules they use), and for each run `terraform fmt -check`, `init`, `validate` and `plan -out`, with `-lock-timeout` set. Post a readable plan summary to the pull request, with the full plan as an artifact, and mark a failed or destructive plan clearly.
3. Use read-only cloud credentials for plans and write credentials only for applies, both through OIDC federation scoped to the branch or environment.
4. On merge to main: apply per root module, serialised with concurrency per state, behind a protected environment with the approval rules. Apply the saved plan from the reviewed run if the CI system can pass it safely, or re-plan and fail if the plan differs from the reviewed one. Explain which you chose.
5. Pin the Terraform version and commit the provider lock file; cache provider plugins.
6. Never run plan with secrets for pull requests from forks.
7. Add a scheduled drift check that runs `plan -detailed-exitcode` and reports drift without applying.
8. Write the files and run the CI linter if available.
</task>

<constraints>
- Never auto-apply without a review step on production.
- Never print sensitive values; rely on Terraform's sensitive marking and do not echo variables.
- Do not use `-auto-approve` on a fresh plan that reviewers have not seen, unless the team accepts it explicitly for a non-production environment.
- Do not invent third-party actions; prefer plain CLI steps when unsure.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Flow
Pull request, merge and scheduled drift paths in numbered steps.

## Config
Workflow files with paths and full contents.

## Credentials
The roles needed (plan read-only, apply write) and their trust scope.

## Safety checks
Fork handling, plan-to-apply consistency, locking and concurrency.

## Verify
Linter output or "not run", and how to test with a no-op change.

## Follow-ups
Cloud roles to create, environments and approvals to configure. "None" if empty.
</output_format>

可替换内容

  • {{layout}}
  • {{#approval}}
  • {{approval}}
  • {{/approval}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、基础设施即代码、持续集成、部署审批、配置漂移检测、云凭据管理

前往 AI 图片创作