Skip to content

Commit 993388c

Browse files
committed
chore: Enhance Terraform CI/CD workflow with artifact management
- Updated the GitHub Actions workflow to save the Terraform plan as an artifact during the main push, ensuring consistency between the plan and apply stages. - Added steps to download the saved tfplan artifact before applying changes in both dev and prod environments. - Revised documentation to reflect the new artifact management process and clarify the execution flow for plan and apply operations.
1 parent 2275efa commit 993388c

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

.github/workflows/terraform.yml

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,17 @@ jobs:
175175
TF_VAR_cloudflare_zone_id: ${{ secrets.CLOUDFLARE_ZONE_ID || '' }}
176176
run: |
177177
terraform plan -no-color -out=tfplan 2>&1 | tee plan_output.txt
178-
echo "exitcode=$?" >> $GITHUB_OUTPUT
178+
echo "exitcode=${PIPESTATUS[0]}" >> $GITHUB_OUTPUT
179+
180+
# plan ジョブで作成した tfplan を apply ジョブに引き渡すためアーティファクトとして保存
181+
# main push 時のみアップロード(PR では apply は実行しない)
182+
- name: Upload plan artifact
183+
uses: actions/upload-artifact@v3
184+
if: steps.plan.outcome == 'success' && github.ref == 'refs/heads/main' && github.event_name == 'push'
185+
with:
186+
name: tfplan-${{ matrix.environment }}
187+
path: terraform/environments/${{ matrix.environment }}/tfplan
188+
retention-days: 1
179189

180190
# PR作成時にプラン結果をコメントとして追加
181191
# レビュアーがインフラ変更内容を確認できるようにする
@@ -242,16 +252,16 @@ jobs:
242252
working-directory: terraform/environments/dev
243253
run: terraform init
244254

255+
# plan ジョブで保存した tfplan をダウンロード(PR で表示した内容と同一の変更を適用)
256+
- name: Download plan artifact
257+
uses: actions/download-artifact@v3
258+
with:
259+
name: tfplan-dev
260+
path: terraform/environments/dev
261+
245262
- name: Terraform Apply
246263
working-directory: terraform/environments/dev
247-
env:
248-
TF_VAR_env: dev
249-
TF_VAR_domain_name: dev.note-app.kanare.dev
250-
TF_VAR_api_domain_name: api-dev.note-app.kanare.dev
251-
TF_VAR_enable_cloudflare_dns: ${{ secrets.ENABLE_CLOUDFLARE_DNS || 'false' }}
252-
TF_VAR_cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN || '' }}
253-
TF_VAR_cloudflare_zone_id: ${{ secrets.CLOUDFLARE_ZONE_ID || '' }}
254-
run: terraform apply -auto-approve
264+
run: terraform apply tfplan
255265

256266
# ========================================
257267
# Job 5: Terraform Apply(prod環境)
@@ -288,13 +298,13 @@ jobs:
288298
working-directory: terraform/environments/prod
289299
run: terraform init
290300

301+
# plan ジョブで保存した tfplan をダウンロード(PR で表示した内容と同一の変更を適用)
302+
- name: Download plan artifact
303+
uses: actions/download-artifact@v3
304+
with:
305+
name: tfplan-prod
306+
path: terraform/environments/prod
307+
291308
- name: Terraform Apply
292309
working-directory: terraform/environments/prod
293-
env:
294-
TF_VAR_env: prod
295-
TF_VAR_domain_name: note-app.kanare.dev
296-
TF_VAR_api_domain_name: api.note-app.kanare.dev
297-
TF_VAR_enable_cloudflare_dns: ${{ secrets.ENABLE_CLOUDFLARE_DNS || 'false' }}
298-
TF_VAR_cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN || '' }}
299-
TF_VAR_cloudflare_zone_id: ${{ secrets.CLOUDFLARE_ZONE_ID || '' }}
300-
run: terraform apply -auto-approve
310+
run: terraform apply tfplan

docs/cicd-guide.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,14 @@ git push
250250
└─ Cloudflare変数(オプション)
251251
252252
4. Planの実行
253-
└─ 実行計画をplan_output.txtに保存
253+
├─ 実行計画をplan_output.txtに保存(PRコメント用)
254+
└─ バイナリ形式のtfplanを-outオプションで保存
254255
255256
5. PRコメント投稿(PR時のみ)
256257
└─ 変更内容をコメントに表示
258+
259+
6. tfplanのアーティファクト保存(main push時のみ)
260+
└─ tfplan-dev / tfplan-prod としてアップロード(有効期限: 1日)
257261
```
258262

259263
**PRコメント例**:
@@ -288,16 +292,20 @@ Plan: 0 to add, 1 to change, 0 to destroy.
288292
目的: mainマージ時にdev環境を自動適用
289293
実行タイミング: mainへのpush時のみ(PRでは実行しない)
290294
依存関係: terraform-plan(dev + prod 両方の成功が必要)
291-
実行内容: terraform init && terraform apply -auto-approve
295+
実行内容: terraform init → tfplanダウンロード → terraform apply tfplan
292296
```
293297

298+
> **plan/apply の一貫性**: apply ジョブは plan ジョブで生成・保存した tfplan を
299+
> アーティファクト経由でダウンロードし、そのまま適用する。
300+
> これにより PR でレビューした plan と実際の apply が必ず一致する。
301+
294302
#### 5. terraform-apply-prod(prod 手動承認 apply)
295303

296304
```
297305
目的: dev適用成功後、手動承認を経てprod環境に適用
298306
実行タイミング: mainへのpush時、apply-dev成功後
299307
承認: GitHub Environment "production" の Required Reviewers が承認
300-
実行内容: terraform init && terraform apply -auto-approve
308+
実行内容: terraform init → tfplanダウンロード → terraform apply tfplan
301309
```
302310

303311
**GitHub Environment 承認フロー**:

0 commit comments

Comments
 (0)