-
Notifications
You must be signed in to change notification settings - Fork 57
260 lines (229 loc) · 8.7 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
name: Deploy
on:
repository_dispatch:
types: [manual-trigger, deploy-trigger]
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
concurrency:
group: core-deploy
cancel-in-progress: false
steps:
- name: Checkout Codes
uses: actions/checkout@v3
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/[email protected]
with:
APP_URL: https://www.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.1
VERSIONS_TO_KEEP: 5
DISCORD_TRAINING_ALERTS_CHANNEL_ID: ${{ secrets.DISCORD_TRAINING_ALERTS_CHANNEL_ID }}
#
# 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@v2
name: Create GitHub Deployment
id: github_deployment
with:
token: ${{ github.token }}
environment_url: https://www.vatsim.uk
environment: production
ref: ${{ github.event.client_payload.ref }}
#
# BUILD DEPENDENCIES SETUP
#
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
#
# NODE BUILD
# Install node_modules and run webpack
#
# Restore Caches
- name: Get Node Cache Directory
id: node-cache-dir-path
run: echo "::set-output name=dir::$(node cache dir)"
- name: Restore Node Cache
uses: actions/cache@v3
id: node-cache
with:
path: ${{ steps.node-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
# Install node_modules
- name: Install Core assets
run: npm ci
# Run Webpack
- name: Compile Core assets
run: npm run build
# Not required for deployment
- name: Remove node_modules
run: 'rm -rf node_modules'
#
# 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 /usr/local/bin/composer install --no-interaction --no-dev
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/[email protected]
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: core
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://www.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://www.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 }}