-
Notifications
You must be signed in to change notification settings - Fork 17
107 lines (89 loc) · 3.13 KB
/
terraform.yml
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
name: "Terraform"
on:
workflow_dispatch:
push:
branches:
- master
- production
pull_request:
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
env:
TF_VAR_github_organization: ictsc
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate token
id: generate_token
uses: tibdex/[email protected]
with:
app_id: ${{ secrets.APP_ID }}
installation_id: 31536419
private_key: ${{ secrets.PRIVATE_KEY }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.3.6
- name: Terraform Format
id: fmt
run: terraform fmt -recursive -check
- name: Terraform Init
id: init
run: terraform init -input=false
- name: Terraform Validate
run: terraform validate -no-color
- name: Get latest state
if: github.ref != 'refs/heads/production'
run: wget https://raw.githubusercontent.com/ictsc/ictsc-github-member/production/terraform.tfstate
- name: Terraform refresh state
env:
TF_VAR_github_token: ${{ steps.generate_token.outputs.token }}
run: terraform refresh -no-color -input=false -parallelism=30
- name: Terraform Plan
env:
TF_VAR_github_token: ${{ steps.generate_token.outputs.token }}
id: plan
run: terraform plan -no-color -input=false -parallelism=30 --refresh=false
continue-on-error: true
- uses: actions/github-script@v6
if: github.event_name == 'pull_request' && steps.plan.outcome != 'failure'
env:
PLAN: "${{ steps.plan.outputs.stdout }}"
with:
script: |
const output = `
<details><summary>Terraform Plan</summary>
\`\`\`text
${process.env.PLAN}\`\`\`
</details>`
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
env:
TF_VAR_github_token: ${{ steps.generate_token.outputs.token }}
if: github.ref == 'refs/heads/production' && github.event_name == 'push'
run: |
terraform apply -auto-approve -input=false
- name: Backup tfstate to artifact
if: github.ref == 'refs/heads/production' && github.event_name == 'push'
uses: actions/upload-artifact@v3
with:
name: terraform.tfstate
path: terraform.tfstate
- name : Commit and Push tfstate
if: github.ref == 'refs/heads/production' && github.event_name == 'push'
run: |
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "Github Action"
git add -f terraform.tfstate
git commit -m "update terraform state [ci skip]"
git push origin production