-
Notifications
You must be signed in to change notification settings - Fork 4
1954 lines (1756 loc) · 75.2 KB
/
Copy pathci.yml
File metadata and controls
1954 lines (1756 loc) · 75.2 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
name: CI
# Build and run tests in containers
# Store test and prod images to GitHub Container Registry (GHCR)
# Build prod image and push to AWS ECR
# Run tests and quality checks on test image
# Run security scans on code and prod images
# Use matrix for multiple versions of Elixir, OTP, and OS
# on: push
on:
push:
branches:
- main
# tags: [prod]
paths-ignore:
- README.md
# - '.github/**'
- .vscode
- .gitignore
- "terraform/*"
# Handled by otel.yml GitHub Action
- .github/workflows/otel.yml
- deploy/aws-otel-collector.Dockerfile
- 'otel/*'
# Handled by newman.yml GitHub Action
- deploy/newman.Dockerfile
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
# Name of image
IMAGE_NAME: foo-app
# Name of org in GHCR Docker repository (must be lowercase)
IMAGE_OWNER: ${{ github.repository_owner }}
# IMAGE_OWNER: cogini
# AWS ECR Docker repo "org" name (may be blank, otherwise must have trailing slash)
ECR_IMAGE_OWNER: cogini/
# ECR_IMAGE_OWNER: ''
# Tag for release images, used to find the latest deployed image.
IMAGE_TAG: latest
IMAGE_VER: ${{ github.sha }}
# Registry to pull internal images from
REGISTRY: ghcr.io/
# Registry for public images, default (blank) is docker.io
# PUBLIC_REGISTRY: ''
# When base image has been synced to local registry
PUBLIC_REGISTRY: ghcr.io/
REPO_ORG: ${{ github.repository_owner }}
REPO_ORG_AMAZON: ${{ github.repository_owner }}
REPO_ORG_ELIXIR: ${{ github.repository_owner }}
REPO_ORG_KAFKA: ${{ github.repository_owner }}
REPO_ORG_POSTGRES: ${{ github.repository_owner }}
# Git "main" branch. This might be "master" for old repos
MAIN_BRANCH: main
# GitHub Environment secrets and variables
# Docker Hub credentials to pull base images without rate limits
# secrets.DOCKERHUB_USERNAME
# secrets.DOCKERHUB_TOKEN
# AWS Account
# secrets.AWS_ACCOUNT_ID
# AWS default region
# vars.AWS_REGION
# AWS role allowing GitHub Actions to access resources and deploy
# secrets.AWS_ROLE_TO_ASSUME: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/foo-${{ environment }}-github-action
# S3 bucket where assets are deployed, e.g., for use with CloudFront CDN
# vars.S3_BUCKET_ASSETS: cogini-foo-app-dev-app-assets
# AWS CloudFront distribution ID to invalidate cache after deploy
# secrets.CLOUDFRONT_CDN_DISTRIBUTION_ID
# S3 bucket with data for testing
# vars.S3_BUCKET_CI: cogini-prod-foo-ci
# SSH key to access private repos during build
# secrets.SSH_PRIVATE_KEY
# GitHub access token to access other repositories during build
# secrets.DEVOPS_ACCESS_TOKEN
# AWS ECS deployment role names to put in task definition
# secrets.TASK_ROLE_ARN: "arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/foo-app"
# secrets.EXECUTION_ROLE_ARN: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/foo-ecs-task-execution-role
# AWS Systems Manager Parameter Store prefix for config keys
# vars.AWS_PS_PREFIX: cogini/foo/prod
# AppSignal API key for error reporting
# secrets.APPSIGNAL_PUSH_API_KEY
# DataDog API key for reporting test results
# secrets.ACTIONS_DD_API_KEY
# secrets.DD_API_KEY
# Oban Pro license
# secrets.OBAN_KEY_FINGERPRINT
# secrets.OBAN_LICENSE_KEY
# Sentry.io
# secrets.SENTRY_AUTH_TOKEN
# vars.SENTRY_ORG: cogini
# vars.SENTRY_PROJECT: phoenix-container-example
# secrets.GITLEAKS_LICENSE
# secrets.SNYK_TOKEN
# Target port for prod image tests
APP_PORT: 4000
# Elixir module, used in health checks
ELIXIR_MODULE: PhoenixContainerExample
# Elixir module Erlang app name
ELIXIR_APP: phoenix_container_example
APPSIGNAL_APP_NAME: phoenix-container-example
# Name of environment for resources created by Terraform
# TERRAFORM_ENV: dev
# Docker
DOCKER_BUILDKIT: '1'
COMPOSE_DOCKER_CLI_BUILD: '1'
COMPOSE_FILE: docker-compose.gha.yml
DOCKER_FILE: deploy/debian.Dockerfile
jobs:
config:
name: Configure build
runs-on: ubuntu-latest
outputs:
# Combinations of Elixir/Erlang/OS to run internal tests on
# Generally this will match prod
test-matrix: ${{ steps.common-matrix.outputs.result }}
# Combinations to tests on in parallel, i.e., mix test with partitions
test-matrix-parallel: ${{ steps.test-matrix-parallel.outputs.result }}
# Combinations for prod images
# Used for the final deploy image as well as external tests
prod-matrix: ${{ steps.prod-matrix.outputs.result }}
# Combinations to deploy (to prod or another environment)
# Usually only a single variant
deploy-matrix: ${{ steps.deploy-matrix.outputs.result }}
# Combinations to sync images to GHCR for
sync-matrix: ${{ steps.common-matrix.outputs.result }}
# Select environment to deploy to based on git branch/tag
environment: ${{ (github.ref_name == 'main' && 'staging') || (github.ref_name == 'prod' && 'production') }}
# Whether to deploy. Only standard tags/branches deploy, not other dev branches.
deploy: ${{ contains(fromJson('["main", "staging", "qa", "prod"]'), github.ref_name) }}
# Sync 3rd-party images to GHCR
# sync-images: '${{ steps.get-sync-images.outputs.result }}'
sync-images: '1'
# Sync 3rd-party images to ECR
# sync-images-ecr: '${{ steps.get-sync-images.outputs.result }}'
sync-images-ecr: '0'
# Enable AWS interactions in the build, including deploys.
aws-enabled: '1'
# Run unit tests
test: '1'
# Run dialyzer
test-dialyzer: '0'
# Security scan prod image
scan-prod-image: '0'
# Security scan code image
scan-code: '0'
# Use specific scanning tools
scan-gitleaks: '0'
scan-grype: '1'
scan-snyk: '0'
scan-trivy: '0'
# Use GitHub Advanced Security, free for open source, otherwise a paid feature
# https://docs.github.com/en/get-started/learning-about-github/about-github-advanced-security
# 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
github-advanced-security: '0'
# Upload test results to Datadog
test-datadog: '0'
# Upload assets to S3
upload-assets: '0'
# Create package for deployment, e.g., Erlang release
package: '1'
package-ansible: '1'
package-codedeploy: '1'
package-erlang-release: '1'
# Create GitHub release to store build artifacts, required for packaging
create-release: '1'
# Deploy assets to AWS S3 bucket
deploy-assets-s3: '0'
# Deploy using AWS CodeDeploy
deploy-codedeploy: '0'
# Deploy to AWS ECS
deploy-ecs: '1'
# Push prod image to AWS ECR
deploy-ecr: '1'
# Whether to tag released images
tag-images: '1'
# Tag for GHCR images. The repo is shared between all environments, so use tags to distinguish.
image-tag-ghcr: ${{ (github.ref_name == 'main' && 'test') || (github.ref_name == 'qa' && 'qa') || (github.ref_name == 'prod' && 'latest') || github.ref_name }}
# Tag for ECR images. Repos are separate between environments, so each gets "latest" tag
image-tag-ecr: ${{ contains(fromJson('["main", "staging", "prod"]'), github.ref_name) && 'latest' || github.ref_name }}
image-tag-deploy: ${{ steps.get-image-tag-deploy.outputs.result }}
# Notify AppSignal of new app release
notify-appsignal: '1'
# Notify Sentry of new app release
notify-sentry: '1'
steps:
- name: Configure common-matrix
id: common-matrix
uses: actions/github-script@v9
# Specify versions of Erlang, Elixir, and base OS
# in a combination supported by https://hub.docker.com/r/hexpm/elixir/tags
with:
script: |
return {
include: [
{
os: "debian",
elixir: "1.19.5",
otp: "28.5",
build_os_ver: "trixie-20260518-slim",
prod_os_ver: "trixie-20260518-slim"
},
]
}
# Combinations of Elixir/Erlang/OS to run internal tests on
# Generally this will match prod
- name: Configure test-matrix-parallel
id: test-matrix-parallel
uses: actions/github-script@v9
with:
script: |
let platforms = [
{
os: "debian",
elixir: "1.19.5",
otp: "28.5",
build_os_ver: "trixie-20260518-slim",
prod_os_ver: "trixie-20260518-slim"
},
]
let ci_nodes = [1, 2]
let ci_node_total = ci_nodes.length
let matrix = []
for (let i = 0; i < platforms.length; i++) {
for (let j = 0; j < ci_nodes.length; j++) {
matrix.push(Object.assign({}, platforms[i], {ci_node_total: ci_node_total, ci_node_index: ci_nodes[j]}) )
}
}
return { include: matrix }
# Combinations for prod images
# Used for the final deploy image as well as external tests
- name: Configure prod-matrix
id: prod-matrix
uses: actions/github-script@v9
with:
script: |
return {
include: [
{
os: "debian",
elixir: "1.19.5",
otp: "28.5",
build_os_ver: "trixie-20260518-slim",
prod_os_ver: "trixie-20260518-slim"
},
{
os: "distroless",
elixir: "1.19.5",
otp: "28.5",
build_os_ver: "trixie-20260518-slim",
prod_os_ver: "cc-debian13"
},
]
}
# Combinations to deploy (to prod or another environment)
# Usually only a single variant
- name: Configure deploy-matrix
id: deploy-matrix
uses: actions/github-script@v9
with:
script: |
return {
include: [
{
os: "debian",
elixir: "1.19.5",
otp: "28.5",
build_os_ver: "trixie-20260518-slim",
prod_os_ver: "trixie-20260518-slim"
},
]
}
# Select GitHub Actions environment based on branch or tag name
# This also implicitly selects the AWS deploy environment.
# * `main` branch or `test` tag deploys to test
# * `staging` or `staging-*` tag/branch deploys to staging
# * `qa` or `qa-*` tag/branch deploys to qa
# * `prod` tag deploys to prod and dr
- name: Configure environment
id: get-environment
uses: actions/github-script@v9
env:
GITHUB_REF_NAME: ${{ github.ref_name }}
with:
result-encoding: string
script: |
ref_name = process.env.GITHUB_REF_NAME
var environments = {
'test': 'test',
'main': 'test',
'staging': 'staging',
'qa': 'qa',
'prod': 'prod',
'dr': 'prod',
'media': 'prod',
}
if (ref_name in environments) {
result = environments[ref_name]
} else {
if (ref_name.startsWith('staging-')) {
result = 'staging'
} else if (ref_name.startsWith('qa-')) {
result = 'qa'
} else {
result = 'test'
}
}
return result
# Get image tag for deploy based on environment
# In test, staging, and qa, deploy is based on GitHub SHA
# In prod, deploy is based on staging ECR repo
# In dr, deploy is based on prod ECR repo in DR region
- name: Configure image-tag-deploy
id: get-image-tag-deploy
uses: actions/github-script@v9
env:
GITHUB_REF_NAME: ${{ github.ref_name }}
with:
result-encoding: string
script: |
ref_name = process.env.GITHUB_REF_NAME
if (ref_name == 'prod' || ref_name == 'dr') {
result = 'prod'
} else {
result = context.sha
}
return result
# Determine whether to deploy to AWS based on branch or tag name
- name: Configure deploy
id: get-deploy
uses: actions/github-script@v9
env:
GITHUB_REF_NAME: ${{ github.ref_name }}
with:
result-encoding: string
script: |
ref_name = process.env.GITHUB_REF_NAME
if (ref_name == 'main' || ref_name == 'staging' || ref_name.startsWith('staging-')) {
result = '1'
} else if (ref_name == 'qa' || ref_name.startsWith('qa-')) {
result = '1'
} else if (ref_name == 'testing' || ref_name.startsWith('devops-')) {
result = '1'
} else {
result = '0'
}
return result
- name: Configure sync-images
id: get-sync-images
uses: actions/github-script@v9
env:
GITHUB_REF_NAME: ${{ github.ref_name }}
with:
result-encoding: string
script: |
ref_name = process.env.GITHUB_REF_NAME
if (ref_name == 'main' || ref_name == 'staging' || ref_name.startsWith('staging-')) {
result = '1'
} else if (ref_name == 'testing' || ref_name.startsWith('devops-')) {
result = '1'
} else if (ref_name == 'qa' || ref_name.startsWith('qa-')) {
result = '1'
} else {
result = '0'
}
return result
create-release:
name: Create GitHub release
needs: [config]
if: needs.config.outputs.create-release == '1'
runs-on: ubuntu-latest
outputs:
release-tag: build-${{ github.run_number }}
upload-url: ${{ steps.release-view.outputs.upload-url }}
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: build-${{ github.run_number }}
steps:
- name: Check out source
uses: actions/checkout@v6
# - name: Tag repository
# run: |
# echo "$RELEASE_TAG"
# git tag "$RELEASE_TAG"
# git push origin "$RELEASE_TAG"
- name: Create release
if: ${{ github.run_attempt == '1' }}
run: gh release create "$RELEASE_TAG" -n "Build $RELEASE_TAG"
- name: Get release info
id: release-view
run: |
UPLOAD_URL="$(gh release view "$RELEASE_TAG" --json uploadUrl --jq .uploadUrl | cut -d'{' -f'1')"
echo "upload-url=$UPLOAD_URL" >> "$GITHUB_OUTPUT"
build-test:
name: Build test image
needs: [sync-images-matrix, config]
permissions:
id-token: write
contents: read
packages: write
runs-on: ubuntu-latest
timeout-minutes: 30
environment: ${{ needs.config.outputs.environment }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.config.outputs.test-matrix) }}
env:
DOCKER_FILE: deploy/${{ matrix.os }}.Dockerfile
VAR: ${{ matrix.elixir }}-erlang-${{ matrix.otp }}-${{ matrix.os }}-${{ matrix.build_os_ver }}
TEST_DATA: '0'
steps:
- name: Check out source
uses: actions/checkout@v6
- name: Set variables
shell: bash
run: |
echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> "$GITHUB_ENV"
echo "run_id=${GITHUB_RUN_ID}" >> "$GITHUB_OUTPUT"
echo "run_num=${GITHUB_RUN_NUMBER}" >> "$GITHUB_OUTPUT"
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# - name: Debug environment
# # env.ACTIONS_STEP_DEBUG == 'true'
# run: |
# # echo "github.event_name: ${{ github.event_name }}"
# # echo "ref: ${{ github.ref }}"
# # echo "ref_name: ${{ github.ref_name }}"
# # echo "environment: ${{ needs.config.outputs.environment }}"
# # echo "components: ${{ needs.config.outputs.components }}"
# # echo -n "AWS_ACCOUNT_ID: "
# # echo ${{ secrets.AWS_ACCOUNT_ID }} | sed 's/./& /g'
# # echo "AWS_REGION var: ${{ vars.AWS_REGION }}"
# Pull public images without rate limits
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# - name: Configure ssh keys
# uses: webfactory/ssh-agent@v0.7.0
# # Configure machine key or deploy keys to access private repos from build
# # https://github.com/marketplace/actions/webfactory-ssh-agent
# # https://docs.github.com/en/developers/overview/managing-deploy-keys#deploy-keys
# # ssh-keygen -t ed25519 -m pem -C "git@github.com:reachfh/api-utils.git" -f api-utils
# with:
# ssh-private-key: |
# ${{ secrets.SSH_PRIVATE_KEY }}
- name: Set env vars for builds
run: |
echo 'DATABASE_HOST=postgres' >> ./.env.test
# - 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: Configure AWS credentials
if: env.TEST_DATA == '1'
uses: aws-actions/configure-aws-credentials@v6
# 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
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ vars.AWS_REGION }}
- name: Get test data files
if: env.TEST_DATA == '1'
run: |
mkdir -p test/bert
aws s3 cp "s3://${{ vars.S3_BUCKET_CI }}/test-data.zip" test-data.zip
unzip -o test-data.zip -d test/data
- name: Set up Docker buildx
id: buildx
uses: docker/setup-buildx-action@v4
with:
driver-opts: network=host
- name: Build test image and push to GHCR
uses: docker/build-push-action@v7
with:
file: ${{ env.DOCKER_FILE }}
target: test
build-args: |
ELIXIR_VER=${{ matrix.elixir }}
OTP_VER=${{ matrix.otp }}
BUILD_OS_VER=${{ matrix.build_os_ver }}
PROD_OS_VER=${{ matrix.prod_os_ver }}
PUBLIC_REGISTRY=${{ env.PUBLIC_REGISTRY }}
REPO_ORG_PROD_OS=${{ env.REPO_ORG }}/
REPO_ORG_ELIXIR=${{ env.REPO_ORG_ELIXIR }}
context: .
builder: ${{ steps.buildx.outputs.name }}
push: true
cache-from: |
type=registry,ref=ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:buildcache-test${{ env.VAR }}-${{ env.MAIN_BRANCH }}
type=registry,ref=ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:buildcache-test${{ env.VAR }}-${{ github.ref_name }}
cache-to: |
type=registry,ref=ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:buildcache-test${{ env.VAR }}-${{ github.ref_name }},mode=max
no-cache: ${{ github.run_attempt != '1' }}
# platforms: linux/amd64,linux/arm64
# ssh: default
tags: |
ghcr.io/${{ env.IMAGE_OWNER }}/${{ env.IMAGE_NAME }}:test${{ env.VAR }}${{ env.IMAGE_VER }}
# secrets: |
# "access_token=${{ secrets.DEVOPS_ACCESS_TOKEN }}"
# "oban_key_fingerprint=${{ secrets.OBAN_KEY_FINGERPRINT }}"
# "oban_license_key=${{ secrets.OBAN_LICENSE_KEY }}"
test:
name: Run tests
if: needs.config.outputs.test == '1'
needs: [build-test, config]
environment: ${{ needs.config.outputs.environment }}
permissions:
packages: read
contents: read
issues: read
checks: write
pull-requests: write
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.config.outputs.test-matrix-parallel) }}
env:
DOCKER_FILE: deploy/${{ matrix.os }}.Dockerfile
VAR: ${{ matrix.elixir }}-erlang-${{ matrix.otp }}-${{ matrix.os }}-${{ matrix.build_os_ver }}
steps:
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Pull public images without rate limits
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v6
- name: Pull repos
run: |
docker compose pull --quiet --include-deps test
docker images --no-trunc
- name: Start services
run: docker compose up --detach test
- name: Show docker logs
if: failure()
run: |
docker compose logs --timestamps postgres
# docker compose logs --timestamps kafka
# docker compose logs --timestamps zookeeper
docker compose logs --timestamps test
docker compose ps --format json | jq .
- name: Display health check results
if: failure()
run: |
echo postgres
docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q postgres)" | jq
# echo redis
# docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q redis)" | jq
# echo kafka
# docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q kafka)" | jq
# echo zookeeper
# docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q zookeeper)" | jq
- name: Debug env
if: failure()
run: docker compose run test env
- name: Initialize test database
run: docker compose run test mix 'do' ecto.setup
- name: Run tests
# continue-on-error: true
# run: docker compose run test mix test --cover
run: docker compose run test env MIX_TEST_PARTITION=${{ matrix.ci_node_index }} mix test --partitions ${{ matrix.ci_node_total }}
# run: docker compose run test mix test
- name: Run quality checks
# run: docker compose run test mix 'do' format --check-formatted + deps.unlock --check-unused + credo --mute-exit-status --all + hex.audit + deps.audit + sobelow --exit --quiet --skip -i DOS.StringToAtom,Config.HTTPS
run: docker compose run test mix quality.ci
- name: Publish unit test results to GitHub
uses: EnricoMi/publish-unit-test-result-action@v2
# Run even if tests fail
if: (!cancelled())
with:
# Volume mounted from local filesystem into build by docker compose
files: junit-reports/*.xml
- name: Upload test results to Datadog
if: (needs.config.outputs.test-datadog == '1' && !cancelled())
continue-on-error: true
env:
DD_API_KEY: ${{ secrets.ACTIONS_DD_API_KEY }}
DD_ENV: ci
DD_TAGS: "environment:ci"
run: |
npm install -g @datadog/datadog-ci
datadog-ci junit upload --service api-graphql junit-reports/
test-dialyzer:
name: Run dialyzer
if: needs.config.outputs.test-dialyzer == '1'
needs: [build-test, config]
environment: ${{ needs.config.outputs.environment }}
permissions:
contents: read
packages: read
checks: write
pull-requests: write
issues: read
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.config.outputs.test-matrix) }}
env:
DOCKER_FILE: deploy/${{ matrix.os }}.Dockerfile
VAR: ${{ matrix.elixir }}-erlang-${{ matrix.otp }}-${{ matrix.os }}-${{ matrix.build_os_ver }}
steps:
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Check out source
uses: actions/checkout@v6
- name: Pull repos
run: |
docker compose pull --quiet test
- name: Run dialyzer
run: docker compose run test mix dialyzer --no-check --quiet-with-result --format github
# run: docker compose run test mix dialyzer --no-check --ignore-exit-status --format github
test-scan:
name: Security scan code
needs: [build-test, config]
if: needs.config.outputs.scan-code == '1'
environment: ${{ needs.config.outputs.environment }}
permissions:
contents: read
packages: read
checks: write
pull-requests: write
issues: read
# Upload SARIF report files
security-events: write
runs-on: ubuntu-latest
env:
GITHUB_ADVANCED_SECURITY: ${{ needs.config.outputs.github-advanced-security }}
GITLEAKS: ${{ needs.config.outputs.scan-gitleaks }}
GRYPE: ${{ needs.config.outputs.scan-grype }}
TRIVY: ${{ needs.config.outputs.scan-trivy }}
steps:
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout source
uses: actions/checkout@v6
# with:
# # Fetch all history for all branches and tags for Gitleaks
# fetch-depth: 0
- name: Manual Trivy Setup
if: env.TRIVY == '1'
uses: aquasecurity/setup-trivy@v0.3.1
with:
cache: true
version: v0.68.2
- name: Scan code with trivy
if: env.TRIVY == '1'
uses: aquasecurity/trivy-action@0.69.3
# https://github.com/marketplace/actions/aqua-security-trivy
with:
scan-type: 'fs'
scan-ref: '.'
ignore-unfixed: true
severity: 'CRITICAL'
# trivy-config: trivy.yaml
format: ${{ env.GITHUB_ADVANCED_SECURITY == '1' && 'sarif' || 'table' }}
# output: ${{ env.GITHUB_ADVANCED_SECURITY == '1' && 'trivy-results.sarif' }}
skip-setup-trivy: true
- name: Display scan results
if: ${{ env.TRIVY == '1' && always() && env.GITHUB_ADVANCED_SECURITY == '1' }}
run: cat trivy-results.sarif | jq .
- name: Upload trivy scan results to GitHub Security tab
if: ${{ env.TRIVY == '1' && always() && env.GITHUB_ADVANCED_SECURITY == '1' }}
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: 'trivy-results.sarif'
category: trivy
- name: Scan code with Grype
if: env.GRYPE == '1'
uses: anchore/scan-action@v7
# https://github.com/marketplace/actions/anchore-container-scan
id: scan-grype
with:
path: .
output-format: table
# output-format: 'sarif'
# output-format: ${{ env.GITHUB_ADVANCED_SECURITY == '1' && 'sarif' || 'table' }}
fail-build: false
# severity-cutoff: critical
- name: Display scan results
if: ${{ env.GRYPE == '1' && always() && env.GITHUB_ADVANCED_SECURITY == '1' }}
run: cat ${{ steps.scan-grype.outputs.sarif }} | jq .
- name: Upload grype scan results to GitHub Security tab
if: ${{ env.GRYPE == '1' && always() && env.GITHUB_ADVANCED_SECURITY == '1' }}
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: ${{ steps.scan-grype.outputs.sarif }}
category: grype
- name: Scan with Gitleaks
if: env.GITLEAKS == '1'
uses: gitleaks/gitleaks-action@v3
env:
GITHUB_TOKEN: ${{ github.token }}
GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }} # Only required for Organizations, not personal accounts.
# upload sarif artifact when secrets are detected
GITLEAKS_ENABLE_UPLOAD_ARTIFACT: true
build-prod:
name: Build prod image
needs: [sync-images-matrix, create-release, config]
permissions:
id-token: write
contents: write
packages: write
runs-on: ubuntu-latest
timeout-minutes: 30
environment: ${{ needs.config.outputs.environment }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.config.outputs.prod-matrix) }}
env:
AWS_ENABLED: ${{ needs.config.outputs.aws-enabled }}
DEPLOY_ASSETS_S3: ${{ needs.config.outputs.deploy-assets-s3 }}
DEPLOY_ECR: ${{ needs.config.outputs.deploy-ecr }}
DOCKER_FILE: deploy/${{ matrix.os }}.Dockerfile
PACKAGE_ANSIBLE: ${{ needs.config.outputs.package-ansible }}
PACKAGE_CODEDEPLOY: ${{ needs.config.outputs.package-codedeploy }}
PACKAGE_RELEASE: ${{ needs.config.outputs.package-erlang-release }}
RELEASE_TAG: ${{ needs.create-release.outputs.release-tag }}
VAR: ${{ matrix.elixir }}-erlang-${{ matrix.otp }}-${{ matrix.os }}-${{ matrix.build_os_ver }}
steps:
- name: Debug environment
run: |
echo "github.event_name: ${{ github.event_name }}"
echo "ref: ${{ github.ref }}"
echo "ref_name: ${{ github.ref_name }}"
echo "environment: ${{ needs.config.outputs.environment }}"
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Configure AWS credentials
if: env.AWS_ENABLED == '1'
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ vars.AWS_REGION }}
- name: Log in to Amazon ECR
if: env.DEPLOY_ECR == '1'
id: ecr-login
uses: aws-actions/amazon-ecr-login@v2
# with:
# registries: ${{ (needs.config.outputs.environment == 'qa') && env.AWS_ACCOUNT_STAGING || vars.AWS_ACCOUNT }}
- name: Set vars
if: env.DEPLOY_ECR == '1'
run: echo "ECR_REGISTRY=${{ steps.ecr-login.outputs.registry }}" >> "$GITHUB_ENV"
- name: Check out source
uses: actions/checkout@v6
- name: Set variables
id: vars
shell: bash
run: |
echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> "$GITHUB_ENV"
echo "run_id=${GITHUB_RUN_ID}" >> "$GITHUB_OUTPUT"
echo "run_num=${GITHUB_RUN_NUMBER}" >> "$GITHUB_OUTPUT"
echo "NOW=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_ENV"
- name: Get app version
run: |
APP_VERSION=$(cat mix.exs | grep version: | awk -F'"' '{print $2}')
echo "APP_VERSION=$APP_VERSION" >> "$GITHUB_ENV"
# - name: Configure ssh keys
# uses: webfactory/ssh-agent@v0.7.0
# # https://github.com/marketplace/actions/webfactory-ssh-agent
# # https://docs.github.com/en/developers/overview/managing-deploy-keys#deploy-keys
# # ssh-keygen -t ed25519 -m pem -C "git@github.com:reachfh/api-utils.git" -f api-utils
# with:
# ssh-private-key: |
# ${{ secrets.SSH_PRIVATE_KEY }}
# Build assets on host and copy into container
# Doing it outside of the prod image speeds up the build, as we don't
# have to copy big static files around.
# We do it here instead of a separate task, as we need
# priv/static/cache_manifest.json in the container.
- uses: actions/setup-node@v6
with:
node-version: 24
- name: Enable corepack
run: corepack enable
- name: Install Erlang and Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}
- name: Install tools
run: mix 'do' local.rebar --force + local.hex --force
- name: Cache deps
uses: actions/cache@v6
env:
cache-name: cache-deps-${{ github.ref_name }}
with:
path: deps
key: |
${{env.cache-name}}-${{runner.os}}-${{matrix.otp}}-${{matrix.elixir}}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{env.cache-name}}-${{runner.os}}-${{matrix.otp}}-${{matrix.elixir}}-
- name: Cache hex
uses: actions/cache@v6
env:
cache-name: cache-deps-${{ github.ref_name }}
with:
path: ~/.hex
key: |
${{env.cache-name}}-${{runner.os}}-${{matrix.otp}}-${{matrix.elixir}}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{env.cache-name}}-${{runner.os}}-${{matrix.otp}}-${{matrix.elixir}}-
- name: Cache compiled build
uses: actions/cache@v6
env:
cache-name: cache-build-${{ github.ref_name }}
with:
path: _build
# ${{runner.os}}-mix-${{env.cache-name}}-${{matrix.otp}}-${{matrix.elixir}}-${{hashFiles('**/mix.lock')}}-${{hashFiles( '**/lib/**/*.{ex,eex}', '**/config/*.exs', '**/mix.exs')}}
key: |
${{env.cache-name}}-${{runner.os}}-${{matrix.otp}}-${{matrix.elixir}}-${{hashFiles('**/mix.lock')}}
restore-keys: |
${{env.cache-name}}-${{runner.os}}-${{matrix.otp}}-${{matrix.elixir}}-
- name: Cache node-modules
uses: actions/cache@v6
env:
cache-name: node-modules-${{ github.ref_name }}
with:
path: assets/node_modules
key: |
${{env.cache-name}}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{env.cache-name}}-
# Force full recompile on builds that are retried.
- name: Clean deps to avoid flaky incremental builds
if: github.run_attempt != '1'
run: |
mix deps.clean --all
mix clean
- name: Get Elixir deps
run: mix deps.get
- name: Compile deps
run: |
# https://github.com/elixir-lang/elixir/blob/v1.19/CHANGELOG.md#parallel-compilation-of-dependencies
export MIX_OS_DEPS_COMPILE_PARTITION_COUNT=$((($(getconf _NPROCESSORS_ONLN) + 1) / 2)) ; \
# `mix loadpaths` is better than `mix deps.compile` when dependencies are already fully compiled
# https://elixirforum.com/t/github-action-cache-elixir-always-recompiles-dependencies-elixir-1-13-3/45994/12
# mix deps.compile
mix loadpaths
# Asset building doesn't require compiling the app
# - name: Compile
# # run: mix compile --warnings-as-errors
# run: mix compile
- name: Setup
run: |
mkdir -p ./assets
mix assets.setup
# - name: npm install
# working-directory: assets
# run: npm install
# - name: npm run deploy
# working-directory: assets
# run: npm run deploy
# - name: yarn install