-
Notifications
You must be signed in to change notification settings - Fork 2
235 lines (228 loc) · 9.82 KB
/
build-push-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
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
name: Build and deploy to Azure Container Apps
on:
push:
branches:
- main
- production
- acceptance
- swarm
workflow_dispatch:
permissions:
contents: read
packages: write
jobs:
variables:
runs-on: ubuntu-latest
outputs:
result: ${{ steps.variables.outputs.result }}
tag: ${{ steps.tag.outputs.result }}
steps:
- uses: actions/checkout@v3
- name: Variables
uses: actions/github-script@v6
id: variables
with:
script: |
const getVariables = require('./.github/workflows/get-variables.js')
return getVariables('${{ github.ref_name }}')
- name: Tag
uses: actions/github-script@v6
id: tag
with:
result-encoding: string
script: return '${{ github.ref_name }}-${{ github.run_number }}'
build-and-push-images:
runs-on: ubuntu-latest
## Ideally we shouldn't expose environment variables for runtime during build time.
## This is necessary just to bundle the backend URL in the browser JavaScript.
needs: variables
steps:
- name: Check out repository
uses: actions/checkout@main
# Create environment variables ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL
# to let docker access the cache.
- name: Expose variables
uses: crazy-max/ghaction-github-runtime@v2
- name: Log in GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Need to use driver --driver=docker-container to use GitHub cache integration.
# The GitHub cache is per-branch.
- name: Docker build and push
run: docker buildx create --use --driver=docker-container
- run: >
docker buildx build ./src
--file ./src/prod.Dockerfile
--target wagtail
--cache-to type=gha,mode=min,scope=${{ github.ref_name }}-wagtail
--cache-from type=gha,scope=${{ github.ref_name }}-wagtail
--tag ghcr.io/zenmo/holon-wagtail:${{ needs.variables.outputs.tag }}
--push
- run: >
docker buildx build ./frontend
--file ./frontend/prod.Dockerfile
--build-arg NEXT_PUBLIC_WAGTAIL_API_URL=https://${{ fromJson(needs.variables.outputs.result).WAGTAIL_HOSTNAME }}/wt/api/nextjs
--cache-to type=gha,mode=min,scope=${{ github.ref_name }}-nextjs
--cache-from type=gha,scope=${{ github.ref_name }}-nextjs
--tag ghcr.io/zenmo/holon-nextjs:${{ needs.variables.outputs.tag }}
--push
## This job takes pretty long and can be split up to parallelize the deploy.
deploy-wagtail-azure:
needs:
- build-and-push-images
- variables
runs-on: ubuntu-latest
steps:
- name: Log in to Azure
uses: azure/login@v1
with:
## https://github.com/Azure/login#configure-a-service-principal-with-a-secret
creds: ${{ secrets.AZURE_CREDENTIALS }}
# - name: Create app environment
# uses: azure/CLI@v1
# with:
# azcliversion: 2.51.0
# inlineScript: >
# az containerapp env create
# --name holon-env
# --location westeurope
# --resource-group HOLON-webapp
- name: Deploy-Wagtail
uses: azure/CLI@v1
with:
## Use generic azure/CLI@v1 instead of specific azure/container-apps-deploy-action@v1
## because it supports the options that we want.
azcliversion: 2.51.0
inlineScript: >
az containerapp create
--resource-group HOLON-webapp
--environment holon-env
--name wagtail-${{ github.ref_name }}
--env-vars
ALLOWED_HOSTS="*"
AZURE_ACCOUNT_NAME=holonstorage
AZURE_STORAGE_KEY=${{ secrets.AZURE_STORAGE_KEY }}
MEDIA_LOCATION=${{ fromJson(needs.variables.outputs.result).MEDIA_LOCATION }}
STATIC_LOCATION=${{ fromJson(needs.variables.outputs.result).STATIC_LOCATION }}
DB_HOST=${{ secrets.DB_HOST }}
DB_USER=${{ fromJson(needs.variables.outputs.result).DB_USER }}
DB_NAME=${{ fromJson(needs.variables.outputs.result).DB_NAME }}
DB_PASSWORD=${{ secrets[fromJson(needs.variables.outputs.result).DB_PASSWORD_KEY] }}
RETURN_SCENARIO=${{ fromJson(needs.variables.outputs.result).RETURN_SCENARIO }}
SECRET_KEY="${{ secrets.SECRET_KEY }}"
SENTRY_DSN=https://764e9f2b886741bcbcfd2acd74a7f7b0@o4505045746384896.ingest.sentry.io/4505045759361024
SENTRY_ENVIRONMENT=${{ fromJson(needs.variables.outputs.result).SENTRY_ENVIRONMENT }}
DOMAIN_HOST=${{ fromJson(needs.variables.outputs.result).DOMAIN_HOST }}
N_WORKERS=${{ fromJson(needs.variables.outputs.result).wagtail.N_WORKERS }}
EMAIL_HOST_PASSWORD=${{ secrets.EMAIL_HOST_PASSWORD }}
WAGTAILADMIN_BASE_URL=https://${{ fromJson(needs.variables.outputs.result).WAGTAIL_HOSTNAME }}
--target-port 8000
--ingress external
--image ghcr.io/zenmo/holon-wagtail:${{ needs.variables.outputs.tag }}
--cpu ${{ fromJson(needs.variables.outputs.result).wagtail.CPU }}
--memory ${{ fromJson(needs.variables.outputs.result).wagtail.MEMORY }}
--min-replicas 1
--max-replicas 2
- name: Bind-Wagtail
uses: azure/CLI@v1
with:
azcliversion: 2.51.0
inlineScript: >
az containerapp hostname bind
--resource-group HOLON-webapp
--environment holon-env
--name wagtail-${{ github.ref_name }}
--hostname ${{ fromJson(needs.variables.outputs.result).WAGTAIL_HOSTNAME }}
deploy-next-azure:
needs:
- build-and-push-images
- variables
runs-on: ubuntu-latest
steps:
- name: Log in to Azure
uses: azure/login@v1
with:
## https://github.com/Azure/login#configure-a-service-principal-with-a-secret
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy-Next.js
uses: azure/CLI@v1
with:
azcliversion: 2.51.0
inlineScript: >
az containerapp create
--resource-group HOLON-webapp
--name next-${{ github.ref_name }}
--environment holon-env
--env-vars
WAGTAIL_API_URL=http://wagtail-${{ github.ref_name }}/wt/api/nextjs
NEXT_PUBLIC_WAGTAIL_API_URL=https://${{ fromJson(needs.variables.outputs.result).WAGTAIL_HOSTNAME }}/wt/api/nextjs
NEXT_PUBLIC_TINY_URL_API_KEY=${{ secrets.TINY_URL_API_KEY }}
--target-port 3000
--ingress external
--tags branch=${{ github.ref_name }}
--image ghcr.io/zenmo/holon-nextjs:${{ needs.variables.outputs.tag }}
--cpu 0.25
--memory 0.5
--min-replicas 1
--max-replicas 1
- name: Bind-Next.js
uses: azure/CLI@v1
with:
azcliversion: 2.51.0
inlineScript: >
az containerapp hostname bind
--resource-group HOLON-webapp
--environment holon-env
--name next-${{ github.ref_name }}
--hostname ${{ fromJson(needs.variables.outputs.result).NEXT_HOSTNAME }}
- name: Bind-www
if: github.ref_name == 'production'
uses: azure/CLI@v1
with:
azcliversion: 2.51.0
inlineScript: >
az containerapp hostname bind
--resource-group HOLON-webapp
--environment holon-env
--name next-${{ github.ref_name }}
--hostname www.holontool.nl
deploy-swarm:
runs-on: ubuntu-latest
environment: ${{ fromJson(needs.variables.outputs.result).GITHUB_ENVIRONMENT }}
needs:
- build-and-push-images
- variables
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
sparse-checkout: docker
- name: Deploy to Docker Swarm
uses: sagebind/docker-swarm-deploy-action@v2
env:
# Shared
TAG: ${{ needs.variables.outputs.tag }}
WAGTAIL_HOSTNAME: ${{ fromJson(needs.variables.outputs.result).WAGTAIL_HOSTNAME }}
# Wagtail
AZURE_STORAGE_KEY: ${{ secrets.AZURE_STORAGE_KEY }}
MEDIA_LOCATION: ${{ fromJson(needs.variables.outputs.result).MEDIA_LOCATION }}
STATIC_LOCATION: ${{ fromJson(needs.variables.outputs.result).STATIC_LOCATION }}
DB_USER: ${{ fromJson(needs.variables.outputs.result).DB_USER }}
DB_NAME: ${{ fromJson(needs.variables.outputs.result).DB_NAME }}
DB_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
RETURN_SCENARIO: ${{ fromJson(needs.variables.outputs.result).RETURN_SCENARIO }}
SECRET_KEY: "${{ secrets.SECRET_KEY }}"
SENTRY_ENVIRONMENT: ${{ fromJson(needs.variables.outputs.result).SENTRY_ENVIRONMENT }}
DOMAIN_HOST: ${{ fromJson(needs.variables.outputs.result).DOMAIN_HOST }}
EMAIL_HOST_PASSWORD: ${{ secrets.EMAIL_HOST_PASSWORD }}
# NextJS
NEXT_HOSTNAME: ${{ fromJson(needs.variables.outputs.result).NEXT_HOSTNAME }}
NEXT_PUBLIC_TINY_URL_API_KEY: ${{ secrets.TINY_URL_API_KEY }}
with:
remote_host: ssh://[email protected]
ssh_private_key: ${{ secrets.SWARM_SSH_PRIVATE_KEY }}
ssh_public_key: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ1E4LUG22qgzc8U7oNYGWCn0cyA31+iyX2pck9wcPMS
args: stack deploy --compose-file ./docker/compose-prod.yaml holon-${{ github.ref_name }}