-
Notifications
You must be signed in to change notification settings - Fork 2
396 lines (350 loc) · 13.3 KB
/
ci.yml
File metadata and controls
396 lines (350 loc) · 13.3 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
---
name: CI
on: push
# on:
# push:
# branches: [main]
# pull_request:
# branches: [main]
# types: [labeled, synchronize, reopened, unlabeled]
env:
# IMAGE_NAME: ${{ github.repository }}
IMAGE_NAME: absinthe-app
IMAGE_OWNER: ${{ github.repository_owner }}
AWS_ROLE_TO_ASSUME: arn:aws:iam::770916339360:role/absinthe-dev-ecr-github-action-role
AWS_REGION: ap-northeast-1
DOCKER_BUILDKIT: '1'
COMPOSE_DOCKER_CLI_BUILD: '1'
jobs:
build-test:
name: Build test image
runs-on: ubuntu-latest
permissions:
# Push to ghcr.io repository
packages: write
steps:
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# https://github.com/marketplace/actions/docker-login
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v2
- name: Set variables
id: vars
run: |
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-7)" >> $GITHUB_ENV
echo "::set-output name=run_id::${GITHUB_RUN_ID}"
echo "::set-output name=run_num::${GITHUB_RUN_NUMBER}"
- name: Get branch name for push
if: github.event_name != 'pull_request'
run: |
echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
- name: Get branch name for pull_request
if: github.event_name == 'pull_request'
run: |
echo "BRANCH=$(echo $GITHUB_HEAD_REF | tr '//\\' '.' | cut -c -55)" >> $GITHUB_ENV
# - name: Set up QEMU for multi-platform builds
# id: qemu
# uses: docker/setup-qemu-action@v2
# with:
# platforms: linux/amd64,linux/arm64
# - name: Display available platforms
# run: echo "${{ steps.qemu.outputs.platforms }}"
- name: Set up Docker buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: network=host
# https://github.com/docker/build-push-action
- name: Build test image
uses: docker/build-push-action@v3
env:
REGISTRY: ghcr.io/
PUBLIC_REGISTRY: ""
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
target: test-image
# https://github.com/moby/buildkit#export-cache
cache-from: type=gha,scope=${{ github.workflow }}-test
cache-to: type=gha,scope=${{ github.workflow }}-test,mode=max
tags: |
ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:test
test:
name: Run tests
runs-on: ubuntu-latest
# permissions: write-all
permissions:
# Interact with GitHub's OIDC Token endpoint for AWS
id-token: write
contents: read
# Read from ghcr.io repository
packages: read
# Upload JUnit report files
# https://github.com/EnricoMi/publish-unit-test-result-action#permissions
checks: write
pull-requests: write
issues: read
needs: [build-test]
steps:
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v2
- name: Run tests
env:
REGISTRY: ghcr.io/
# PUBLIC_REGISTRY: "${{ steps.login-ecr.outputs.registry }}/"
PUBLIC_REGISTRY: ""
run: |
docker pull "${REGISTRY}${IMAGE_OWNER}/${IMAGE_NAME}:test"
docker-compose -f docker-compose.gha.yml up test
docker-compose -f docker-compose.gha.yml run test mix test
docker-compose -f docker-compose.gha.yml run test mix do format --check-formatted, credo, deps.audit, sobelow
- name: Publish unit test results to GitHub
uses: EnricoMi/publish-unit-test-result-action@v2
# Run even if tests fail
if: always()
with:
# Volume mounted from local filesystem into build
junit_files: junit-reports/*.xml
test-dialyzer:
name: Run Dialyzer
runs-on: ubuntu-latest
needs: [build-test]
# permissions: write-all
permissions:
# Interact with GitHub's OIDC Token endpoint for AWS
id-token: write
contents: read
# Read from ghcr.io repository
packages: read
# Upload JUnit report files
# https://github.com/EnricoMi/publish-unit-test-result-action#permissions
checks: write
pull-requests: write
issues: read
steps:
# https://github.com/marketplace/actions/docker-login
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v2
- name: Run Dialyzer
env:
REGISTRY: ghcr.io/
# PUBLIC_REGISTRY: "${{ steps.login-ecr.outputs.registry }}/"
PUBLIC_REGISTRY: ""
run: |
docker pull "${REGISTRY}${IMAGE_OWNER}/${IMAGE_NAME}:test"
docker-compose -f docker-compose.gha.yml up test
docker-compose -f docker-compose.gha.yml run test mix dialyzer --no-check --halt-exit-status --format github
test-scan:
name: Security scan code
runs-on: ubuntu-latest
needs: [build-test]
# permissions: write-all
permissions:
# Interact with GitHub's OIDC Token endpoint for AWS
id-token: write
contents: read
# Read from ghcr.io repository
packages: read
# Upload JUnit report files
# https://github.com/EnricoMi/publish-unit-test-result-action#permissions
checks: write
pull-requests: write
issues: read
# Upload SARIF report files
security-events: write
steps:
# https://github.com/marketplace/actions/docker-login
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout source
uses: actions/checkout@v2
- name: Trivy code
env:
REGISTRY: ghcr.io/
# PUBLIC_REGISTRY: "${{ steps.login-ecr.outputs.registry }}/"
PUBLIC_REGISTRY: ""
run: |
mkdir -p ./sarif-reports
mkdir -p ./trivy-cache
docker pull "${REGISTRY}${IMAGE_OWNER}/${IMAGE_NAME}:test"
docker-compose -f docker-compose.gha.yml up test
docker-compose -f docker-compose.gha.yml run test trivy fs --no-progress --debug --cache-dir /trivy-cache .
docker-compose -f docker-compose.gha.yml run test trivy fs --format sarif -o /sarif-reports/trivy-code.sarif --no-progress --debug --cache-dir /trivy-cache .
# TODO:
# * Don't scan Elixir deps dir
# * Scan standard deps like Yarn
# * Scan application code and put warnings inline using JUnit?
# Fail build on security issues in our code
# * Configure trivy options via file
# https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning
# https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/uploading-a-sarif-file-to-github
- name: Upload scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
if: always()
with:
sarif_file: './sarif-reports/'
category: trivy
build-prod:
name: Build prod image
runs-on: ubuntu-latest
# runs-on: ubuntu-20.04
permissions:
# Interact with GitHub's OIDC Token endpoint for AWS
id-token: write
contents: read
# Push to ghcr.io repository
packages: write
# Upload JUnit report files
# https://github.com/EnricoMi/publish-unit-test-result-action#permissions
checks: write
pull-requests: write
issues: read
steps:
# https://github.com/aws-actions/configure-aws-credentials
# https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ env.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ env.AWS_REGION }}
# https://github.com/aws-actions/amazon-ecr-login
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
# https://github.com/marketplace/actions/docker-login
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v2
- name: Set variables
id: vars
run: |
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-7)" >> $GITHUB_ENV
echo "::set-output name=run_id::${GITHUB_RUN_ID}"
echo "::set-output name=run_num::${GITHUB_RUN_NUMBER}"
- name: Get branch name for push
if: github.event_name != 'pull_request'
run: |
echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
- name: Get branch name for pull_request
if: github.event_name == 'pull_request'
run: |
echo "BRANCH=$(echo $GITHUB_HEAD_REF | tr '//\\' '.' | cut -c -55)" >> $GITHUB_ENV
- name: Set up Docker buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
driver-opts: network=host
- name: Build deploy image and push
# if: github.event_name != 'pull_request'
uses: docker/build-push-action@v3
env:
REGISTRY: "${{ steps.login-ecr.outputs.registry }}/"
PUBLIC_REGISTRY: ""
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
cache-from: type=gha,scope=${{ github.workflow }}
cache-to: type=gha,scope=${{ github.workflow }},mode=max
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ env.IMAGE_NAME }}:latest
${{ steps.login-ecr.outputs.registry }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
# ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:latest
- name: Build scan image
uses: docker/build-push-action@v3
env:
REGISTRY: ghcr.io/
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
target: deploy-scan
# https://github.com/moby/buildkit#export-cache
cache-from: type=gha,scope=${{ github.workflow }}-scan
cache-to: type=gha,scope=${{ github.workflow }}-scan,mode=max
tags: |
ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:scan
scan:
name: Security scan image
runs-on: ubuntu-latest
needs: [build-prod]
# permissions: write-all
permissions:
# Interact with GitHub's OIDC Token endpoint for AWS
id-token: write
contents: read
# Read from ghcr.io repository
packages: read
# Upload JUnit report files
# https://github.com/EnricoMi/publish-unit-test-result-action#permissions
checks: write
pull-requests: write
issues: read
# Upload SARIF report files
security-events: write
steps:
# https://github.com/marketplace/actions/docker-login
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v2
- name: Security scan image
env:
REGISTRY: ghcr.io/
# PUBLIC_REGISTRY: "${{ steps.login-ecr.outputs.registry }}/"
PUBLIC_REGISTRY: ""
run: |
mkdir -p ./sarif-reports
mkdir -p ./trivy-cache
docker pull "${REGISTRY}${IMAGE_OWNER}/${IMAGE_NAME}:scan"
docker-compose -f docker-compose.gha.yml run scan trivy fs --no-progress --debug --cache-dir /trivy-cache /
docker-compose -f docker-compose.gha.yml run scan trivy fs --format sarif -o /sarif-reports/trivy-deploy.sarif --no-progress --debug --cache-dir /trivy-cache /
# docker-compose -f docker-compose.gha.yml run scan trivy fs --severity CRITICAL --exit-code 1 --format sarif -o /sarif-reports/trivy.sarif --no-progress /
# https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning
# https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/uploading-a-sarif-file-to-github
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
if: always()
with:
sarif_file: './sarif-reports/'
category: trivy