-
-
Notifications
You must be signed in to change notification settings - Fork 50
172 lines (150 loc) · 5.47 KB
/
build_and_deploy.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
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
# Workflow for build and auto-deploy of branches
name: 🔧 Build and Deploy
on:
# Push includes PR merge
push:
branches:
- main
- staging
- development
paths:
# Workflow is triggered only if src changes
- src/**
# Allow manual trigger
workflow_dispatch:
jobs:
backend-test:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
image_name: ghcr.io/${{ github.repository }}/backend
build_context: src/backend
pre_command: docker compose up -d proxy && docker compose exec api /app-entrypoint.sh
compose_service: api
compose_command: pytest
tag_override: ci-${{ github.ref_name }}
coverage: true
secrets: inherit
frontend-unit-test:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
working_dir: src/frontend
frontend-e2e-test:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
playwright: true
cache_image: false
compose_file: docker-compose.yml -f contrib/playwright/docker-compose.yml
compose_service: ui-test
cache_extra_imgs: |
"docker.io/postgis/postgis:${{ vars.POSTGIS_TAG }}"
"docker.io/minio/minio:${{ vars.MINIO_TAG }}"
"mcr.microsoft.com/playwright:${{ vars.PLAYWRIGHT_TAG }}"
secrets: inherit
backend-build:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
needs: [backend-test]
with:
context: src/backend
build_target: prod
image_name: ghcr.io/${{ github.repository }}/backend
frontend-build:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
needs: [frontend-unit-test, frontend-e2e-test]
with:
context: src
dockerfile: Dockerfile.ui.prod
build_target: prod
image_name: ghcr.io/${{ github.repository }}/frontend
smoke-test-backend:
runs-on: ubuntu-latest
needs: [backend-build]
environment:
name: ${{ github.ref_name }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Vars and Secrets to Env
env:
GIT_BRANCH: ${{ github.ref_name }}
TAG_OVERRIDE: ${{ needs.backend-build.outputs.image_tag }}
VARS_CONTEXT: ${{ toJson(vars) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
run: |
# Random delimiter string for security
delim=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
# Parse JSON with multiline strings, using delimiter (Github specific)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$delim\n\(.value)\n$delim\n\""; }
# Set all vars
echo "GIT_BRANCH=${GIT_BRANCH}" >> $GITHUB_ENV
echo "TAG_OVERRIDE=${TAG_OVERRIDE}" >> $GITHUB_ENV
echo "${VARS_CONTEXT}" | to_envs >> $GITHUB_ENV
echo "${SECRETS_CONTEXT}" | to_envs >> $GITHUB_ENV
- name: Create .env file
run: |
# Get a8m/envsubst (required for default vals syntax ${VAR:-default})
echo "Downloading envsubst"
curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst-`uname -s`-`uname -m` -o envsubst
chmod +x envsubst
echo "Substituting variables from .env.example --> .env"
./envsubst < .env.example > .env
echo "GIT_BRANCH=${GIT_BRANCH}" >> .env
echo "TAG_OVERRIDE=${TAG_OVERRIDE}" >> .env
- name: Backend smoke test
env:
# Hardcode replicas=1 for test
API_REPLICAS: 1
run: |
# Migrate db first, so api works
docker compose up migrations --exit-code-from migrations
# Run without migrations (avoid exit code 0)
# Also run within if block to capture logs if failure
if docker compose up --detach \
--no-deps --wait --wait-timeout 60 \
central-db central s3 api
then
docker compose logs api
else
echo "Application not healthy after 1m0s. Exiting."
docker compose logs api
exit 1
fi
smoke-test-frontend:
runs-on: ubuntu-latest
needs: [frontend-build]
environment:
name: ${{ github.ref_name }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create .env file
run: |
# Get a8m/envsubst (required for default vals syntax ${VAR:-default})
echo "Downloading envsubst"
curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst-`uname -s`-`uname -m` -o envsubst
chmod +x envsubst
echo "Substituting variables from .env.example --> .env"
./envsubst < .env.example > .env
- name: Frontend smoke test
run: |
if docker compose up --detach \
--no-deps --wait --wait-timeout 30 \
ui
then
docker compose logs ui
echo "Sleeping 5 seconds to wait for dev server"
sleep 5
curl --fail http://localhost:7051 || exit 1
else
echo "Application not healthy after 30s. Exiting."
docker compose logs ui
exit 1
fi
deploy-containers:
needs:
- smoke-test-backend
- smoke-test-frontend
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
environment: ${{ github.ref_name }}
docker_compose_file: "docker-compose.${{ github.ref_name }}.yml"
secrets: inherit