-
Notifications
You must be signed in to change notification settings - Fork 15
296 lines (260 loc) · 10.2 KB
/
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
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
name: Deploy
on:
repository_dispatch:
types: [manual-trigger, deploy-trigger]
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
concurrency:
group: ukcp-api-deploy
cancel-in-progress: false
steps:
- name: Checkout Codes
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.client_payload.ref }}
- name: Extract short commit hash
run: |
echo "::set-env name=COMMIT::$(echo ${GITHUB_SHA} | cut -c1-7)"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
- name: Extract tag
run: |
echo "::set-env name=TAG::$(git describe --tags --abbrev=0)"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
#
# DEPLOYMENT PARAMETERS
#
- name: Set additional deployment variables
uses: allenevans/set-env@v2.0.0
with:
APP_URL: https://ukcp.vatsim.uk
APPLICATION_ROOT: ${{ secrets.APPLICATION_ROOT }}
RELEASE_DIRECTORY: ${{ secrets.APPLICATION_ROOT }}/releases/${{ env.COMMIT }}
SHARED_DIRECTORY: ${{ secrets.APPLICATION_ROOT }}/shared
PHP_PATH: /bin/php8.2
VERSIONS_TO_KEEP: 5
#
# DISCORD NOTIFICATION JOB START
#
- name: Discord Notification (Start)
uses: rjstone/discord-webhook-notify@v1
with:
severity: warn
description: ${{ format('Starting Deployment of **{0}**', github.repository) }}
details: >
${{ format(':rocket: Starting Deployment of commit `{0}` by :technologist: *{1}* to **Production** ({2})', env.COMMIT, github.actor, env.APP_URL) }}
footer: ${{ format('https://{0}/actions/runs/{1}', github.repository, github.run_id) }}
webhookUrl: ${{ secrets.ACTIONS_DISCORD_WEBHOOK }}
#
# GITHUB DEPLOYMENT JOB START
#
- uses: chrnorm/deployment-action@releases/v1
name: Create GitHub Deployment
id: github_deployment
with:
token: ${{ github.token }}
target_url: https://ukcp.vatsim.uk
environment: production
ref: ${{ github.event.client_payload.ref }}
#
# BUILD DEPENDENCIES SETUP
#
- name: Setup Yarn
uses: actions/setup-node@v1
with:
node-version: '16'
- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: v2
# Add GitHub Auth to Composer
- name: Add Composer GitHub Token
run: composer config -g github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}
# Restore Caches
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Restore Composer Cache
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Restore Vendor From Cache
uses: actions/cache@v1
with:
path: vendor
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }}
- name: Install Composer Dependencies
run: composer install --prefer-dist --no-interaction --optimize-autoloader --no-suggest
#
# YARN BUILD
# Install node_modules and run webpack
#
# Restore Caches
- name: Get Yarn Cache Directory
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore Yarn Cache
uses: actions/cache@v1
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
# Install node_modules
- name: Install Assets
run: yarn
# Run Webpack
- name: Compile Assets
run: yarn run prod
# Not required for deployment
- name: Remove node_modules
run: 'rm -rf node_modules'
- name: Reduce Composer Dependencies To Production
run: composer install --no-interaction --no-dev --optimize-autoloader
#
# DEPLOYMENT
# Prepare remote environment and deploy application
#
- name: Deploy application
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
source: "."
target: ${{ env.RELEASE_DIRECTORY }}
#
# REMOTE POST-DEPLOYMENT ACTIONS
# Conduct server-side post-deployment tasks and make application version live.
#
- name: (Remote) Setup .env & install composer dependencies
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER}}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
script: |
# Ensure we're working from the current release
cd $RELEASE_DIRECTORY
# Symlink .env from root directory
ln -s $APPLICATION_ROOT/.env .env
# Install application dependencies
$PHP_PATH artisan package:discover
$PHP_PATH artisan filament:upgrade
$PHP_PATH artisan optimize
$PHP_PATH artisan event:cache
envs: RELEASE_DIRECTORY,APPLICATION_ROOT,TAG,PHP_PATH
- name: (Remote) Update symbolic links
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER}}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
script: |
if [ ! -d "$SHARED_DIRECTORY/storage" ]; then
mkdir -p $SHARED_DIRECTORY/storage
mv $RELEASE_DIRECTORY/storage/* $SHARED_DIRECTORY/storage/
chmod -R 775 $SHARED_DIRECTORY/storage
fi
rm -rf $RELEASE_DIRECTORY/storage
ln -s $SHARED_DIRECTORY/storage $RELEASE_DIRECTORY/storage
# Update the current link to point to this release
ln -sfn $RELEASE_DIRECTORY $APPLICATION_ROOT/current
envs: RELEASE_DIRECTORY,SHARED_DIRECTORY,APPLICATION_ROOT
- name: Trigger Forge Deployment
uses: jbrooksuk/laravel-forge-action@v1.0.2
with:
trigger_url: ${{ secrets.FORGE_DEPLOY_WEBHOOK }}
#
# SENTRY
# Create a Sentry release
#
- name: Create Sentry release
uses: getsentry/action-release@v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: vatsim-uk
SENTRY_PROJECT: ukcp-api
with:
environment: production
version: ${{ github.sha }}
#
# HOUSEKEEPING
# Perform post-deployment housekeeping actions (release history)
#
- name: Housekeeping
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER}}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
script: |
# Ensure we're only keeping the desired number of versions in history
# Releases are extracted by an array ordered by directory
# creation date.
releases=($(ls -tU $APPLICATION_ROOT/releases))
number_of_releases=${#releases[@]}
if [ "$number_of_releases" -gt "$VERSIONS_TO_KEEP " ]; then
for i in $(seq 0 `expr $number_of_releases - $VERSIONS_TO_KEEP - 1`);
do
echo "Removing: ${releases[$i]}"
# rm -rf $APPLICATION_ROOT/releases/${releases[$i]}
done
fi
envs: APPLICATION_ROOT,VERSIONS_TO_KEEP
#
# GITHUB DEPLOYMENT JOB END
#
- name: Update Deployment Status (Failed)
if: failure()
uses: chrnorm/deployment-status@releases/v1
with:
token: ${{ github.token }}
target_url: https://ukcp.vatsim.uk
state: "failure"
deployment_id: ${{ steps.github_deployment.outputs.deployment_id }}
- name: Update Deployment Status (Success)
if: success()
uses: chrnorm/deployment-status@releases/v1
with:
token: ${{ github.token }}
target_url: https://ukcp.vatsim.uk
state: "success"
deployment_id: ${{ steps.github_deployment.outputs.deployment_id }}
#
# DISCORD NOTIFICATIONS JOB END
#
- name: Discord Notification (Failed)
if: failure()
uses: rjstone/discord-webhook-notify@v1
with:
severity: error
description: ${{ format('Deployment **FAILED** of **{0}**', github.repository) }}
details: >
${{ format(':fire: Deployment **FAILED** for commit `{0}` by :technologist: *{1}* to **Production** ({2})', env.COMMIT, github.actor, env.APP_URL) }}
footer: ${{ format('https://github.com/{0}/actions/runs/{1}', github.repository, github.run_id) }}
webhookUrl: ${{ secrets.ACTIONS_DISCORD_WEBHOOK }}
- name: Discord Notification (Success)
if: success()
uses: rjstone/discord-webhook-notify@v1
with:
severity: info
description: ${{ format('Deployment **SUCCEEDED** of **{0}**', github.repository) }}
details: >
${{ format(':white_check_mark: Deployment **SUCCEEDED** for commit `{0}` by :technologist: *{1}* to **Production** ({2})', env.COMMIT, github.actor, env.APP_URL) }}
footer: ${{ format('https://github.com/{0}/actions/runs/{1}', github.repository, github.run_id) }}
webhookUrl: ${{ secrets.ACTIONS_DISCORD_WEBHOOK }}