-
Notifications
You must be signed in to change notification settings - Fork 10
315 lines (280 loc) · 11.1 KB
/
deploy_with_db.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
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
name: Build and deploy with Database
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
type: environment
required: true
env:
OUTPUT_PATH_ENGINE_NODE: './build/engine/'
OUTPUT_PATH_MS_SERVER: './src/management-system-v2/.next/'
DOCKER_PATH_MS_SERVER: './src/management-system-v2'
jobs:
# install:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - name: Set up Node.js
# uses: actions/setup-node@v4
# with:
# node-version: 20
# check-latest: true
# cache: 'yarn'
# - run: yarn install --frozen-lockfile --ignore-engines
# - uses: actions/cache@v4
# timeout-minutes: 2
# id: cache-install
# with:
# path: ./*
# key: ${{ github.sha }}-${{ github.run_number }}
# lint:
# runs-on: ubuntu-latest
# needs: install
# steps:
# - uses: actions/cache@v4
# timeout-minutes: 2
# id: restore-install
# with:
# path: ./*
# key: ${{ github.sha }}-${{ github.run_number }}
# - name: Set up Node.js
# uses: actions/setup-node@v4
# with:
# node-version: 20
# check-latest: true
# cache: 'yarn'
# - run: yarn prettier --check .
# buildMS:
# runs-on: ubuntu-latest
# env:
# IMAGE_TAG: ${{ inputs.environment == 'Production' && 'latest' || 'edge' }}
# outputs:
# tag: ${{ steps.set_tag.outputs.tag }}
# needs: lint
# steps:
# - uses: actions/cache@v4
# timeout-minutes: 2
# id: restore-install
# with:
# path: ./*
# key: ${{ github.sha }}-${{ github.run_number }}
# - name: Set up Node.js
# uses: actions/setup-node@v4
# with:
# node-version: 20
# check-latest: true
# cache: 'yarn'
# - run: yarn build-ms
# - id: set_tag
# run: |
# TAG=$(git describe --tags --always --dirty --abbrev=7)
# if [[ "$GITHUB_REF" == "refs/heads/main" ]]; then
# echo "tag=${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
# else
# echo "tag=$TAG" >> $GITHUB_OUTPUT
# fi
# - name: Kaniko build
# uses: aevea/action-kaniko@master
# with:
# image: proceed/ms-server
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_PASSWORD }}
# path: ${{ env.DOCKER_PATH_MS_SERVER }}
# tag: ${{ steps.set_tag.outputs.tag }}
check-migration-files:
runs-on: ubuntu-latest
#needs: buildMS
outputs:
main_diff: ${{ steps.compare-migration-main.outputs.diff }}
branch_diff: ${{ steps.check-migration-changes.outputs.diff }}
steps:
- name: Checkout the Repository
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check migration changes with main
id: check-migration-changes-main
run: |
git fetch origin main:main
# Get migration files from main and current branch
MAIN_MIGRATIONS=$(git ls-tree -r main --name-only src/management-system-v2/prisma/migrations/ | grep '\.sql$' || echo "")
CURRENT_MIGRATIONS=$(git ls-tree -r HEAD --name-only src/management-system-v2/prisma/migrations/ | grep '\.sql$' || echo "")
if [ "$MAIN_MIGRATIONS" != "$CURRENT_MIGRATIONS" ]; then
echo "diff detected between main and branch"
echo "diff=true" >> $GITHUB_OUTPUT
else
echo "NO diff detected between main and branch"
echo "diff=false" >> $GITHUB_OUTPUT
fi
- name: Check migration changes within branch
id: check-migration-changes-branch
run: |
if git diff --name-only HEAD~1 HEAD | grep 'src/management-system-v2/prisma/migrations/.*\.sql'; then
echo "diff detected within branch"
echo "diff=true" >> $GITHUB_OUTPUT
else
echo "NO diff detected within branch"
echo "diff=false" >> $GITHUB_OUTPUT
fi
generate-db-name:
runs-on: ubuntu-latest
needs: [check-migration-files]
outputs:
dbname: ${{ steps.gen-db-name.outputs.DB_NAME }}
environment: Research
env:
ENV: Research
steps:
- name: Generate database name
id: gen-db-name
run: |
BRANCH_NAME="${{ github.ref_name }}"
BRANCH_NAME_SANITIZED=$(echo "$BRANCH_NAME" | sed 's|/|_|g')
if [ "${{ needs.check-migration-files.outputs.main_diff }}" = "false" ]; then
# No changes between main and branch - use main suffix
DB_NAME="proceed_db_${BRANCH_NAME_SANITIZED}_main"
else
# Changes exist - use branch name
DB_NAME="proceed_db_${BRANCH_NAME_SANITIZED}"
fi
echo "DB_NAME=$DB_NAME" >> $GITHUB_OUTPUT
- name: Configure SSH
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/$ENV.key
chmod 600 ~/.ssh/$ENV.key
cat >>~/.ssh/config <<END
Host $ENV
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/$ENV.key
StrictHostKeyChecking no
END
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_KEY: ${{ secrets.SSH_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST }}
- name: Check and handle database
run: |
DB_EXISTS=$(ssh $ENV "sudo docker exec ${{vars.DB_CONTAINER_NAME}} psql -U ${{secrets.DB_USER}} -d ${{vars.DB_DEFAULT_DB}} -tAc \"SELECT 1 FROM pg_database WHERE datname='${{ steps.gen-db-name.outputs.DB_NAME }}'\"")
if [ "${{ needs.check-migration-files.outputs.main_diff }}" = "true" ] && [ "${{ needs.check-migration-files.outputs.branch_diff }}" = "true" ]; then
# Changes detected - drop existing DB if it exists
ssh $ENV 'sudo docker exec ${{vars.DB_CONTAINER_NAME}} psql -U ${{secrets.DB_USER}} -d ${{vars.DB_DEFAULT_DB}} -c "DROP DATABASE IF EXISTS ${{ steps.gen-db-name.outputs.DB_NAME }};"'
elif [ "$DB_EXISTS" != "1" ]; then
# DB doesn't exist - no action needed (will be created in create-db job)
echo "Database doesn't exist, will be created in next step"
else
# DB exists and no changes detected - keep existing DB
echo "Reusing existing database"
fi
create-db:
needs: [generate-db-name, check-migration-files]
runs-on: ubuntu-latest
outputs:
database_url: ${{ steps.set-db-url.outputs.DATABASE_URL }}
environment: Research
env:
ENV: Research
steps:
- name: Configure SSH
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/$ENV.key
chmod 600 ~/.ssh/$ENV.key
cat >>~/.ssh/config <<END
Host $ENV
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/$ENV.key
StrictHostKeyChecking no
END
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_KEY: ${{ secrets.SSH_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST }}
- name: Check if database needs to be created
id: check-db
run: |
DB_EXISTS=$(ssh $ENV "sudo docker exec ${{vars.DB_CONTAINER_NAME}} psql -U ${{secrets.DB_USER}} -d ${{vars.DB_DEFAULT_DB}} -tAc \"SELECT 1 FROM pg_database WHERE datname='${{ needs.generate-db-name.outputs.dbname }}'\"")
if [ "$DB_EXISTS" != "1" ]; then
echo "create_db=true" >> $GITHUB_OUTPUT
else
echo "create_db=false" >> $GITHUB_OUTPUT
fi
- name: Create database if needed
if: steps.check-db.outputs.create_db == 'true'
run: |
ssh $ENV 'sudo docker exec ${{vars.DB_CONTAINER_NAME}} psql -U ${{secrets.DB_USER}} -d ${{vars.DB_DEFAULT_DB}} -c "CREATE DATABASE ${{ needs.generate-db-name.outputs.dbname }};"'
- name: Set DATABASE_URL as an output
id: set-db-url
run: echo "DATABASE_URL=postgresql://${{secrets.DB_USER}}:UiIpWxw8Usegfsl7LP8%2FuWrKiCyJbU2nLE9wBvW7gSE%3D@${{secrets.SSH_HOST}}:5433/${{ needs.generate-db-name.outputs.dbname }}?schema=public" >> $GITHUB_OUTPUT
- name: Restore Cache
uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Apply Prisma Migrations
if: steps.check-db.outputs.create_db == 'true'
env:
DATABASE_URL: ${{ steps.set-db-url.outputs.DATABASE_URL }}
run: |
yarn dev-ms-db-deploy
# deploy:
# runs-on: ubuntu-latest
# needs: [buildMS, create-db, generate-db-name]
# permissions:
# contents: read
# id-token: write
# pull-requests: write
# issues: write
# environment: Research
# env:
# MS_TAG: ${{ needs.buildMS.outputs.tag }}
# SERVICE_NAME: ${{ inputs.environment == 'Production' && 'ms-server-production' || 'ms-server-staging' }}
# SUBDOMAIN: ${{ inputs.environment == 'Production' && 'app' || 'staging' }}
# DATABASE_URL: postgresql://${{ secrets.DB_USER }}:UiIpWxw8Usegfsl7LP8%2FuWrKiCyJbU2nLE9wBvW7gSE%3D@${{ secrets.SSH_HOST }}:5433/${{ needs.generate-db-name.outputs.dbname }}?schema=public
# steps:
# - uses: 'google-github-actions/auth@v2'
# with:
# project_id: 'proceed-bpms'
# workload_identity_provider: 'projects/1062024918148/locations/global/workloadIdentityPools/github-ci/providers/github'
# service_account: '[email protected]'
# - id: 'deploy'
# if: ${{ github.ref == 'refs/heads/main' }}
# uses: 'google-github-actions/deploy-cloudrun@v2'
# with:
# service: ${{ env.SERVICE_NAME }}
# image: 'docker.io/proceed/ms-server:${{ env.MS_TAG }}'
# env_vars: |
# NEXTAUTH_URL=https://${{ env.SUBDOMAIN }}.proceed-labs.org
# DATABASE_URL=${{ env.DATABASE_URL }}
# region: 'europe-west1'
# revision_traffic: LATEST=100
# - id: 'deploy-preview'
# if: ${{ github.event_name == 'pull_request' }}
# name: Preview Cloud Run Deployment
# uses: anishsapkota/preview-cloudrun@main
# with:
# service: ${{ env.SERVICE_NAME }}
# image: docker.io/proceed/ms-server:${{ env.MS_TAG }}
# token: ${{ secrets.GITHUB_TOKEN }}
# region: 'europe-west1'
# env_vars: '{"DATABASE_URL": "${{env.DATABASE_URL}}", "PROCEED_PUBLIC_DEPLOYMENT_ENV":"local", "PROCEED_PUBLIC_ENABLE_EXECUTION": "true"}'
# githubEnvironment:
# runs-on: ubuntu-latest
# needs: deploy
# if: ${{ github.ref == 'refs/heads/main' }}
# environment:
# name: ${{ inputs.environment || 'Staging' }}
# url: ${{ inputs.environment == 'Production' && 'https://app.proceed-labs.org' || 'https://staging.proceed-labs.org' }}
# steps:
# - run: echo "${{ inputs.environment || 'Staging' }}"