forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterraform-ci.yml
More file actions
110 lines (90 loc) · 3.08 KB
/
Copy pathterraform-ci.yml
File metadata and controls
110 lines (90 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Terraform CI
on:
pull_request:
paths:
- "infrastructure/terraform/**"
push:
branches: [main]
paths:
- "infrastructure/terraform/**"
concurrency:
group: terraform-${{ github.head_ref || github.ref_name }}
cancel-in-progress: false # queue runs; never clobber state
env:
TF_VERSION: "1.7.5"
WORKSPACE: pr-${{ github.event.pull_request.number || github.run_id }}
jobs:
terraform:
name: Plan & Apply
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: write
defaults:
run:
working-directory: infrastructure/terraform
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }}
aws-region: us-east-1
- name: Create isolated workspace
run: bash ../../infrastructure/scripts/create-workspace.sh "${{ env.WORKSPACE }}"
- name: Terraform fmt check
run: terraform fmt -check -recursive
- name: Terraform validate
run: terraform validate
- name: Terraform plan
id: plan
run: |
terraform plan -input=false -no-color -out=tfplan 2>&1 | tee plan.txt
echo "exitcode=${PIPESTATUS[0]}" >> "$GITHUB_OUTPUT"
- name: Upload plan artifact
uses: actions/upload-artifact@v4
with:
name: tfplan-${{ env.WORKSPACE }}
path: infrastructure/terraform/tfplan
retention-days: 7
- name: Post plan to PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const plan = fs.readFileSync('infrastructure/terraform/plan.txt', 'utf8');
const body = `### Terraform Plan — \`${{ env.WORKSPACE }}\`\n\`\`\`hcl\n${plan.slice(0, 60000)}\n\`\`\``;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
- name: Terraform apply (main only)
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve -input=false tfplan
cleanup:
name: Cleanup workspace
runs-on: ubuntu-latest
needs: terraform
if: always() && github.event_name == 'pull_request'
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_CI_ROLE_ARN }}
aws-region: us-east-1
- name: Cleanup workspace
run: bash infrastructure/scripts/cleanup-workspace.sh "${{ env.WORKSPACE }}"