-
Notifications
You must be signed in to change notification settings - Fork 0
159 lines (136 loc) · 7.8 KB
/
fly-deploy-pr.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
name: Fly Deploy Districtr V2 Pull Request app
on:
pull_request:
types: [opened, reopened, synchronize, closed]
env:
FLY_API_TOKEN: ${{ secrets.FLY_ORG_TOTKEN }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
FLY_REGION: "ewr"
FLY_ORG: "mggg"
jobs:
pr_review_app:
runs-on: ubuntu-latest
concurrency:
group: pr-${{ github.event.number }}
environment:
name: pr-${{ github.event.number }}
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- name: Set shared environment variables
run: |
echo "db_name=${{ github.event.repository.name }}-${{ github.event.number }}-db" >> $GITHUB_ENV
echo "api_app_name=${{ github.event.repository.name }}-${{ github.event.number }}-api" >> $GITHUB_ENV
echo "frontend_app_name=${{ github.event.repository.name }}-${{ github.event.number }}-app" >> $GITHUB_ENV
- name: Destroy Resources
if: github.event.action == 'closed'
run: |
app_name="${{ github.event.repository.name }}-${{ github.event.number }}-api"
frontend_app_name="${{ github.event.repository.name }}-${{ github.event.number }}-app"
db_name="${{ github.event.repository.name }}-${{ github.event.number }}-db"
echo "Destroying app $app_name"
flyctl apps destroy "$app_name" -y
echo "Destroying frontend app $frontend_app_name"
flyctl apps destroy "$frontend_app_name" -y
echo "Destroying database $db_name"
flyctl apps destroy "$db_name" -y
echo "Resources for PR #${{ github.event.number }} have been destroyed."
env:
FLY_API_TOKEN: ${{ secrets.FLY_ORG_TOTKEN }}
# fork new db from existing production db if it doesn't already exist
# eventually we may want to maintain a stage and only fork that
- name: Fork From DB
id: fork-db
if: github.event.action != 'closed'
run: |
if flyctl postgres list | grep -q ${{ github.event.repository.name }}-${{ github.event.number }}-db; then
echo "DB already exists"
else
flyctl postgres create \
--name ${{ github.event.repository.name }}-${{ github.event.number }}-db \
--region ewr \
--initial-cluster-size 1 \
--vm-size shared-cpu-2x \
-p ${{ secrets.FLY_PR_PG_PASSWORD }} \
--org mggg \
--fork-from districtr-v2-db
if [ $? -eq 0 ]; then
echo "Database created successfully."
else
echo "Failed to create database."
exit 1
fi
fi
echo "::set-output name=name::${{ github.event.repository.name }}-${{ github.event.number }}-db"
# manually launch and deploy the api app
- name: Launch API
if: github.event.action != 'closed'
run: |
app="${{ github.event.repository.name }}-${{ github.event.number }}-api"
db_name="${{ github.event.repository.name }}-${{ github.event.number }}-db"
config="fly.toml"
# Check if the app exists
if flyctl apps list | grep -q "$app"; then
echo "App $app already exists. Skipping launch."
else
flyctl launch \
--no-deploy --copy-config --name "$app"
echo "App $app launched successfully."
fi
# Output app name for use in the deploy step
echo "api_app_name=$app" >> $GITHUB_ENV
working-directory: backend
- name: Deploy API
if: github.event.action != 'closed'
run: |
flyctl secrets set \
-a ${{ github.event.repository.name }}-${{ github.event.number }}-api \
POSTGRES_SCHEME="postgresql+psycopg" \
POSTGRES_SERVER="${{ github.event.repository.name }}-${{ github.event.number }}-db.flycast" \
POSTGRES_USER="postgres" \
POSTGRES_PASSWORD=${{ secrets.FLY_PR_PG_PASSWORD }} \
POSTGRES_DB="districtr_v2_api" \
BACKEND_CORS_ORIGINS="https://${{ github.event.repository.name }}-${{ github.event.number }}-app.fly.dev,https://districtr-v2-frontend.fly.dev" \
DATABASE_URL="postgresql://postgres:${{ secrets.FLY_PR_PG_PASSWORD }}@${{ steps.fork-db.outputs.name }}.flycast:5432/districtr_v2_api?sslmode=disable&options=-csearch_path%3Dpublic"
flyctl deploy \
--config fly.toml --app "${{ github.event.repository.name }}-${{ github.event.number }}-api" \
--strategy immediate '--ha=false' --vm-cpu-kind shared --vm-cpus 1 --vm-memory 256 \
working-directory: backend
- name: Check and Launch Frontend App
id: launch
if: github.event.action != 'closed'
run: |
app="${{ github.event.repository.name }}-${{ github.event.number }}-app"
api_app="${{ github.event.repository.name }}-${{ github.event.number }}-api"
config="fly.toml"
# Check if the app exists
if flyctl apps list | grep -q "$app"; then
echo "App $app already exists. Skipping launch."
else
echo "Launching app $app."
# Run the flyctl launch command
flyctl launch \
--no-deploy --copy-config --name "${{ github.event.repository.name }}-${{ github.event.number }}-app" \
--build-arg NEXT_PUBLIC_API_URL="https://${{ github.event.repository.name }}-${{ github.event.number }}-api.fly.dev" \
--build-arg NEXT_PUBLIC_S3_BUCKET_URL=https://districtr-v2-dev.s3.amazonaws.com
echo "App $app launched successfully."
fi
# Output app name for use in the deploy step
echo "frontend_app_name=$app" >> $GITHUB_ENV
working-directory: app
- name: Deploy frontend
if: github.event.action != 'closed'
run: |
app_name="${{ github.event.repository.name }}-${{ github.event.number }}-app"
config="fly.toml"
flyctl secrets set \
-a "${{ github.event.repository.name }}-${{ github.event.number }}-app" \
NEXT_PUBLIC_API_URL="https://${{ github.event.repository.name }}-${{ github.event.number }}-api.fly.dev" \
NEXT_PUBLIC_S3_BUCKET_URL=https://districtr-v2-dev.s3.amazonaws.com
# Deploy the app
flyctl deploy --config "$config" --app "${{ github.event.repository.name }}-${{ github.event.number }}-app" \
--build-arg NEXT_PUBLIC_API_URL="https://${{ github.event.repository.name }}-${{ github.event.number }}-api.fly.dev" \
--build-arg NEXT_PUBLIC_S3_BUCKET_URL=https://districtr-v2-dev.s3.amazonaws.com \
--strategy immediate '--ha=false' \
--vm-cpu-kind shared --vm-cpus 1 --vm-memory 256
working-directory: app