-
Notifications
You must be signed in to change notification settings - Fork 13
203 lines (196 loc) · 7.52 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
name: Deploy
on:
push:
branches:
- main
workflow_dispatch:
jobs:
create-deployment-artifacts:
name: Create Deployment Artifacts
runs-on: ubuntu-latest
outputs:
DEPLOYMENT_MATRIX: ${{steps.export-deployment-matrix.outputs.DEPLOYMENT_MATRIX }}
steps:
- uses: actions/checkout@v3
- name: Configure PHP 8.1
uses: shivammathur/setup-php@master
with:
php-version: 8.1
extensions: mbstring, ctype, fileinfo, openssl, PDO, bcmath, json, tokenizer, xml
- name: Composer Install
run: |
composer install --no-dev --no-interaction --prefer-dist
- name: Create Deployment Artifacts
run: tar -czf ${{github.sha}}.tar.gz --exclude=*.git --exclude=node_modules *
- name: Store Distribution Artifacts
uses: actions/upload-artifact@v3
with:
name: app-build
path: ${{github.sha}}.tar.gz
retention-days: 1
- name: Export Deployment Matrix
id: export-deployment-matrix
run: |
delimiter="$(openssl rand -hex 8)"
JSON="$(cat ./deployment.json)"
echo "DEPLOYMENT_MATRIX<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "$JSON" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
prepare-release-on-servers:
name: "${{ matrix.server.name }}: Prepare release"
runs-on: ubuntu-latest
needs: create-deployment-artifacts
strategy:
matrix:
server: ${{ fromJson(needs.create-deployment-artifacts.outputs.DEPLOYMENT_MATRIX ) }}
steps:
- uses: actions/download-artifact@v3
with:
name: app-build
- name: Upload
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
port: ${{ secrets.PORT }}
source: ${{ github.sha }}.tar.gz
target: ${{ matrix.server.path }}/artifacts
- name: Extract Archives and create directories
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
port: ${{ secrets.PORT }}
envs: GITHUB_SHA
script: |
mkdir -p "${{ matrix.server.path }}/releases/${GITHUB_SHA}"
tar xzf ${{ matrix.server.path }}/artifacts/${GITHUB_SHA}.tar.gz -C "${{ matrix.server.path }}/releases/${GITHUB_SHA}"
sudo mv ${{matrix.server.path}}/releases/${GITHUB_SHA}/storage/ProductionCertificate.cer ${{matrix.server.path}}/releases/${GITHUB_SHA}
sudo mv ${{matrix.server.path}}/releases/${GITHUB_SHA}/storage/SandboxCertificate.cer ${{matrix.server.path}}/releases/${GITHUB_SHA}
rm -rf ${{matrix.server.path}}/releases/${GITHUB_SHA}/storage
mkdir -p ${{matrix.server.path}}/storage/{app,public,framework,logs}
mkdir -p ${{matrix.server.path}}/storage/framework/{cache,sessions,testing,views}
mkdir -p ${{matrix.server.path}}/storage/framework/cache/data
sudo chmod -R 0777 ${{ matrix.server.path }}/storage
run-before-hooks:
name: "${{ matrix.server.name }}: Before hook"
runs-on: ubuntu-latest
needs: [create-deployment-artifacts, prepare-release-on-servers]
strategy:
matrix:
server: ${{ fromJson(needs.create-deployment-artifacts.outputs.DEPLOYMENT_MATRIX ) }}
steps:
- name: Run before hooks
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
RELEASE_PATH: ${{ matrix.server.path }}/releases/${{ github.sha }}
ACTIVE_RELEASE_PATH: ${{ matrix.server.path }}/current
STORAGE_PATH: ${{ matrix.server.path }}/storage
BASE_PATH: ${{ matrix.server.path }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
port: ${{ secrets.PORT }}
envs: GITHUB_SHA,RELEASE_PATH,ACTIVE_RELEASE_PATH,STORAGE_PATH,BASE_PATH
script: |
${{ matrix.server.beforeHooks }}
activate-release:
name: "${{ matrix.server.name }}: Activate release"
runs-on: ubuntu-latest
needs:
[
create-deployment-artifacts,
prepare-release-on-servers,
run-before-hooks,
]
strategy:
matrix:
server: ${{ fromJson(needs.create-deployment-artifacts.outputs.DEPLOYMENT_MATRIX ) }}
steps:
- name: Activate release
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
RELEASE_PATH: ${{ matrix.server.path }}/releases/${{ github.sha }}
ACTIVE_RELEASE_PATH: ${{ matrix.server.path }}/current
STORAGE_PATH: ${{ matrix.server.path }}/storage
BASE_PATH: ${{ matrix.server.path }}
LARAVEL_ENV: ${{ secrets.LARAVEL_ENV }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
port: ${{ secrets.PORT }}
envs: GITHUB_SHA,RELEASE_PATH,ACTIVE_RELEASE_PATH,STORAGE_PATH,BASE_PATH,ENV_PATH,LARAVEL_ENV
script: |
rm -rf ${BASE_PATH}/.env
printf "%s" "$LARAVEL_ENV" > "${BASE_PATH}/.env"
sudo ln -s -f ${BASE_PATH}/.env $RELEASE_PATH
sudo ln -s -f $STORAGE_PATH $RELEASE_PATH
sudo ln -s -n -f $RELEASE_PATH $ACTIVE_RELEASE_PATH
sudo service php8.1-fpm reload
run-after-hooks:
name: "${{ matrix.server.name }}: After hook"
runs-on: ubuntu-latest
needs:
[
create-deployment-artifacts,
prepare-release-on-servers,
run-before-hooks,
activate-release,
]
strategy:
matrix:
server: ${{ fromJson(needs.create-deployment-artifacts.outputs.DEPLOYMENT_MATRIX ) }}
steps:
- name: Run after hooks
uses: appleboy/ssh-action@master
env:
GITHUB_SHA: ${{ github.sha }}
RELEASE_PATH: ${{ matrix.server.path }}/releases/${{ github.sha }}
ACTIVE_RELEASE_PATH: ${{ matrix.server.path }}/current
STORAGE_PATH: ${{ matrix.server.path }}/storage
BASE_PATH: ${{ matrix.server.path }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
port: ${{ secrets.PORT }}
envs: GITHUB_SHA,RELEASE_PATH,ACTIVE_RELEASE_PATH,STORAGE_PATH,BASE_PATH
script: |
${{ matrix.server.afterHooks }}
clean-up:
name: "${{ matrix.server.name }}: Clean up"
runs-on: ubuntu-latest
needs:
[
create-deployment-artifacts,
prepare-release-on-servers,
run-before-hooks,
activate-release,
run-after-hooks,
]
strategy:
matrix:
server: ${{ fromJson(needs.create-deployment-artifacts.outputs.DEPLOYMENT_MATRIX ) }}
steps:
- name: Clean Up
uses: appleboy/ssh-action@master
env:
RELEASES_PATH: ${{ matrix.server.path }}/releases
ARTIFACTS_PATH: ${{ matrix.server.path }}/artifacts
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
port: ${{ secrets.PORT }}
envs: RELEASES_PATH,ARTIFACTS_PATH
script: |
cd $RELEASES_PATH && sudo ls -t -1 | tail -n +2 | xargs rm -rf
cd $ARTIFACTS_PATH && sudo ls -t -1 | tail -n +2 | xargs rm -rf