-
Notifications
You must be signed in to change notification settings - Fork 9
265 lines (219 loc) · 7.68 KB
/
ci.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
name: API Lint, Build, Test, Deploy
on:
push:
branches:
- master
- dev
pull_request:
types:
- opened
- synchronize
- reopened
- labeled
workflow_dispatch:
inputs:
deploy:
description: "Which environment to deploy to"
required: true
default: "none"
type: choice
options:
- prod
- stage
- none
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
DEPLOY: ${{ (inputs.deploy != 'none' && inputs.deploy) || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'prod') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master') && 'prod') || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'stage') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'dev' && 'stage') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) && 'stage') || 'none' }}
jobs:
ci:
name: API Lint, Build, Test, Deploy
runs-on: seventv
env:
GOLANGCI_LINT_CACHE: /home/runner/.cache/golangci-lint
concurrency:
group: ${{ github.workflow }}-ci-${{ github.ref }}
cancel-in-progress: true
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- uses: actions/setup-node@v3
with:
node-version: "18"
- name: Install Yarn
run: npm install -g yarn
- name: Checkout code
uses: actions/checkout@v3
with:
submodules: recursive
- id: cache-paths
run: |
echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT
echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT
- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-build-${{ hashFiles('go.sum') }}
- name: Go Mod Cache
uses: actions/cache@v3
with:
path: ${{ steps.cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-mod-${{ hashFiles('go.sum') }}
- name: GoLint Cache
uses: actions/cache@v3
with:
path: ${{ env.GOLANGCI_LINT_CACHE }}
key: ${{ runner.os }}-go-lint-ci
- name: Node Modules Cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock') }}
- name: Make
run: sudo apt-get install make
- name: Install Dependencies
run: make deps
- name: Install Developer Dependencies
run: make dev_deps
- name: Generate structures
run: make generate
- name: Build Dev Portal Frontend
run: make ${{ (env.DEPLOY == 'prod' && 'portal') || 'portal_stage' }}
- name: Run Linter
run: make lint
- name: Run Tests
run: make test
- name: Build App
run: make build
- name: Make build context
if: env.DEPLOY != 'none'
run: |
docker context create builders
- name: Setup buildx
uses: docker/setup-buildx-action@v2
if: env.DEPLOY != 'none'
with:
install: true
endpoint: builders
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Build docker image
uses: docker/build-push-action@v4
if: env.DEPLOY != 'none'
with:
context: .
file: docker/partial.Dockerfile
tags: |
ghcr.io/seventv/api:${{ env.DEPLOY }}-${{ github.sha }}
ghcr.io/seventv/api:${{ env.DEPLOY }}-latest
push: true
validate:
name: API Deploy Validation
needs: ci
runs-on: ubuntu-latest
permissions:
pull-requests: write
defaults:
run:
working-directory: ./terraform
steps:
- name: Checkout code
id: ok
if: env.DEPLOY != 'none'
uses: actions/checkout@v3
- name: "Setup Terraform"
if: steps.ok.outcome == 'success'
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: "Terraform Init"
if: steps.ok.outcome == 'success'
id: init
env:
TF_WORKSPACE: ${{ env.DEPLOY }}
run: terraform init
continue-on-error: true
- name: "Terraform Workspace"
if: steps.ok.outcome == 'success'
run: terraform workspace select -or-create=true ${{ env.DEPLOY }}
- name: Terraform fmt
if: steps.ok.outcome == 'success'
id: fmt
run: terraform fmt -check
continue-on-error: true
- name: Terraform Validate
if: steps.ok.outcome == 'success'
id: validate
run: terraform validate -no-color
- name: Terraform Variables
if: steps.ok.outcome == 'success'
run: |
cat <<EOF > *.auto.tfvars
image_url="ghcr.io/seventv/api:${{ env.DEPLOY }}-${{ github.sha }}"
image_pull_policy="IfNotPresent"
EOF
- name: "Terraform Plan"
if: steps.ok.outcome == 'success'
id: plan
run: terraform plan -no-color
- uses: actions/github-script@v6
if: steps.ok.outcome == 'success' && github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
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 Format and Style')
})
// 2. Prepare format of the comment
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>
\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`
</details>
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Actor: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}
- name: "Terraform Apply"
if: steps.ok.outcome == 'success'
id: apply
run: terraform apply -no-color -auto-approve
continue-on-error: true