-
Notifications
You must be signed in to change notification settings - Fork 2
185 lines (160 loc) · 6.02 KB
/
email_service.yaml
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
name: "Email Service Infrastructure Change Management Pipeline with GitHub Actions"
on:
pull_request:
paths:
- src/email_service/**
push:
branches:
- main
- dev
paths:
- src/email_service/**
env:
TF_LOG: INFO
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
TF_DIR: ./src/email_service/terraform
permissions:
contents: write
issues: write
pull-requests: write
actions: read
checks: write
jobs:
preview:
name: "Deploy Preview Environment"
if: github.event_name == 'pull_request' && github.event.action != 'closed'
runs-on: ubuntu-latest
steps:
- name: "Print Debug Information"
run: |
echo "Event Name: ${{ github.event_name }}"
echo "Event Action: ${{ github.event.action }}"
echo "Merged: ${{ github.event.pull_request.merged }}"
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.8.3
- name: Terraform Init
id: init
run: terraform init -backend-config="preview.tfbackend" -reconfigure
working-directory: ${{ env.TF_DIR }}
- name: Terraform Destroy (Cleanup)
id: destroy
run: terraform destroy -auto-approve -var-file='preview.tfvars'
working-directory: ${{ env.TF_DIR }}
continue-on-error: true
- name: Terraform Plan
id: plan
run: terraform plan -var-file='preview.tfvars'
working-directory: ${{ env.TF_DIR }}
continue-on-error: true
- name: Terraform Apply
id: apply
run: terraform apply -var-file='preview.tfvars' -auto-approve
working-directory: ${{ env.TF_DIR }}
continue-on-error: true
- uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
APPLY: "terraform\n${{ steps.apply.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Apply Results');
});
const output = `#### Terraform Apply Results 🌟
Preview environment has been deployed. You can now test your changes.
<details><summary>Show Actual Apply</summary>
\`\`\`\n
${{ steps.apply.outputs.stdout }}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output,
});
} else {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output,
});
}
cleanup_preview:
name: "Cleanup Preview Environment"
if: github.event_name == 'pull_request' && github.event.action == 'closed' || github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- name: "Print Debug Information"
run: |
echo "Event Name: ${{ github.event_name }}"
echo "Event Action: ${{ github.event.action }}"
echo "Merged: ${{ github.event.pull_request.merged }}"
- uses: actions/checkout@v3
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.8.3
- name: Terraform Init
run: terraform init -backend-config="preview.tfbackend" -reconfigure
working-directory: ${{ env.TF_DIR }}
- name: Terraform Destroy
run: terraform destroy -auto-approve -var-file='preview.tfvars'
working-directory: ${{ env.TF_DIR }}
deploy:
name: "Deploy to Target Environment"
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev')
runs-on: ubuntu-latest
needs: [cleanup_preview]
steps:
- name: "Print Debug Information"
run: |
echo "Event Name: ${{ github.event_name }}"
echo "Event Action: ${{ github.event.action }}"
echo "Merged: ${{ github.event.pull_request.merged }}"
- uses: actions/checkout@v3
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.8.3
- name: Set Environment Variables
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "ENVIRONMENT=prod" >> $GITHUB_ENV
echo "TF_VARS_FILE=prod.tfvars" >> $GITHUB_ENV
elif [[ "${{ github.ref }}" == "refs/heads/dev" ]]; then
echo "ENVIRONMENT=dev" >> $GITHUB_ENV
echo "TF_VARS_FILE=dev.tfvars" >> $GITHUB_ENV
else
echo "Unsupported branch: ${{ github.ref }}"
exit 1
fi
- name: Terraform Init
id: init
run: terraform init -backend-config="${{ env.ENVIRONMENT }}.tfbackend" -reconfigure
working-directory: ${{ env.TF_DIR }}
- name: Terraform Destroy (Cleanup)
id: destroy
run: terraform destroy -auto-approve -var-file='${{ env.TF_VARS_FILE }}'
working-directory: ${{ env.TF_DIR }}
continue-on-error: true
- name: Terraform Plan
id: plan
run: terraform plan -var-file='${{ env.TF_VARS_FILE }}' -out=tfplan
working-directory: ${{ env.TF_DIR }}
continue-on-error: true
- name: Terraform Apply
id: apply
run: terraform apply -var-file='${{ env.TF_VARS_FILE }}' -auto-approve
working-directory: ${{ env.TF_DIR }}
continue-on-error: true