-
Notifications
You must be signed in to change notification settings - Fork 68
212 lines (191 loc) · 7.32 KB
/
build_and_test.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
name: "Build, Test, Deploy"
on:
pull_request: #{ types: [ready_for_review] }
push: { branches: [master] }
#release: { types: [published] }
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
env:
BUILD_PATH: /tmp/archive.tgz
NVM_DIR: /opt/nvm
BASH_ENV: /opt/nvm/nvm.sh
NODE_ENV: development
jobs:
bat:
runs-on: ubuntu-latest
steps:
- name: Clone repo
uses: actions/checkout@v2
- name: Setup nvm
shell: bash
run: |
mkdir -pv $NVM_DIR
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- name: Install cms dependencies
run: |
cd cms
nvm install 16.13.0
npm install
- name: Start cms
run: cd cms; npm run develop &> /tmp/cms.log &
env:
INITIALIZE_DATA: "true"
- name: Check cms running
uses: actions/github-script@v5
env:
PORT: 1337
LOG_FILE: /tmp/cms.log
with:
script: |
const http = require('http');
const {readFileSync} = require('fs');
core.info('waiting 1 minute for strapi data initialization to complete ..');
await new Promise(zzz => setTimeout(zzz, 1 * 60 * 1000));
http
.request(`http://localhost:${process.env.PORT}/_health`, { method: 'HEAD' }, r => {
if (r.statusCode === 204 && r.headers.strapi === 'You are so French!')
{
core.info('cms successfully started')
return
}
core.error(readFileSync(process.env.LOG_FILE, 'utf-8'))
core.setFailed(`cms failed to start; returned status code: ${r.statusCode}; health status: ${r.headers.strapi}`);
})
.on('error', core.setFailed)
.end();
# - name: Check cms running
# run: |
# echo "Waiting 1 minute for strapi data initialization to complete.."
# sleep 1m # wait a moment grandpa
# _health=$(curl -s http://localhost:$PORT/_health -I | awk -F: '$1 == "strapi" { print $NF ~ /^[[:space:]]/ ? substr($NF, 2) : $NF }') || true
# status_code=$(curl -so/dev/null -w "%{http_code}" http://localhost:$PORT/_health -I) || true
# if [ $? -eq 7 ] || [ ${status_code:--999} -eq 000 ]; then
# echo "::error file=cms,title=cms_start_failure::" \
# "cms failed to start, returned status code '${status_code:-NO_STATUS_CODE}'"
# exit 7
# fi
# if [ "$_health" != "You are so French!" ]; then
# echo "::error file=cms,title=cms_unknown_error::" \
# "cms started, but something else is wrong; returned status code '${status_code:-NO_STATUS_CODE}'"
# exit 1
# fi
# echo "CMS successfully started"
# env: { PORT: 1337 }
- name: Install app dependencies
run: |
cd app
nvm i 16.13.0
npm i
- name: Start app
run: |
cd app
npm run dev &>/tmp/app.log &
echo $! > /tmp/app.pid
- name: Check app running
uses: actions/github-script@v5
env: { PORT: 3000, LOG_FILE: /tmp/app.log }
with:
script: |
const http = require('http');
const {readFileSync} = require('fs');
core.info('waiting 1 minute for app to start ..');
await new Promise(zzz => setTimeout(zzz, 60 * 1000));
http
.request(`http://localhost:${process.env.PORT}`, { method: 'GET' }, r => {
if (r.statusCode === 200)
{
core.info('app successfully started')
return
}
core.error(readFileSync(process.env.LOG_FILE, 'utf8'))
core.setFailed(`app failed to start; returned status code: ${r.statusCode}; body: ${r.read()}`);
})
.on('error', core.setFailed)
.end();
# - name: Check app running
# run: |
# echo "Waiting 10 seconds for app to start..."
# sleep 10s # wait a moment grandma
# status_code="$(curl -o /dev/null -s -w "%{http_code}\n" http://localhost:$PORT)" || true
# if { [ $? -eq 7 ] || [ ${status_code:--999} -eq 000 ]; }; then
# echo "::error file=app,title=app_start_failure::" \
# "app failed to start, returned status code '${status_code:-NO_STATUS_CODE}'"
# exit 7
# fi
# if [ "${status_code:--999}" -ne 200 ]; then
# echo "::error file=app,title=app_unknown_error::" \
# "app started, but something else is wrong; returned status code '${status_code:-NO_STATUS_CODE}'"
# exit 1
# fi
# echo "app successfully started"
# env: { PORT: 3000 }
- name: Build prod app
run: |
pkill -9 -F /tmp/app.pid
cd app; npm run build
env: { NEXT_PUBLIC_STRAPI_API_URL: "http://localhost:1337" }
- name: Generate archive
if: ${{ github.event_name == 'push' }}
run: |
cd app
echo $GITHUB_SHA > /tmp/.github_sha
echo $GITHUB_REF > /tmp/.github_ref
tar zcvf ${{ env.BUILD_PATH }} \
.next \
ecosystem.config.js \
next.config.js \
package-lock.json \
package.json \
/tmp/.github_sha \
/tmp/.github_ref
- name: Upload archive
if: ${{ github.event_name == 'push' }}
uses: actions/upload-artifact@v3
with:
name: build
path: ${{ env.BUILD_PATH }}
deploy:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' }} # don't wanna run on prs
needs: [bat]
steps:
- name: Download build archive
uses: actions/download-artifact@v3
with:
name: build
path: /tmp/
# - name: Push archive
# uses: easingthemes/ssh-deploy@main
# env:
# SOURCE: ${{ env.BUILD_PATH }}
# TARGET: ${{ secrets.TARGET }}
# REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
# REMOTE_USER: ${{ secrets.REMOTE_USER }}
# REMOTE_PORT: ${{ secrets.REMOTE_PORT }}
# SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
#
# - name: Start remote service
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.REMOTE_HOST }}
# port: ${{ secrets.REMOTE_PORT }}
# username: ${{ secrets.REMOTE_USER }}
# key: ${{ secrets.SSH_PRIVATE_KEY }}
# script: |
# target=${{ secrets.TARGET }}
# build_path=${{ secrets.BUILD_PATH }}
# cd $target
# tar zxf $build_path
# rm -f $build_path
# npm install --production
# pm2 reload ecosystem.config.js
- name: Push archive and start service
uses: debdutdeb/rc4community.deploy.action@main
with:
remote_host: ${{ secrets.REMOTE_HOST }}
remote_port: ${{ secrets.REMOTE_PORT }}
remote_user: ${{ secrets.REMOTE_USER }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
source: ${{ env.BUILD_PATH }}
target: ${{ secrets.TARGET }}