-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
234 lines (206 loc) · 6.7 KB
/
.gitlab-ci.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
image: registry.gitlab.com/nikolayradoev/docker-images/chrome-mongo-node:20243
cache:
key: '${CI_COMMIT_REF_SLUG}-client'
paths:
- client/node_modules/
- server/node_modules/
policy: pull
.only-client: &only-client
only:
refs:
- master
- merge_requests
changes:
- client/**/*
- common/**/*
cache:
key: '${CI_COMMIT_REF_SLUG}-client'
paths:
- client/node_modules/
policy: pull
.only-server: &only-server
only:
refs:
- master
- merge_requests
changes:
- server/**/*
- common/**/*
cache:
key: '${CI_COMMIT_REF_SLUG}-server'
paths:
- server/node_modules/
policy: pull
stages:
- install
- lint
- test
- deploy
install:client:
stage: install
<<: *only-client
script:
- cd client
- npm ci --cache .npm --prefer-offline
cache:
key: '${CI_COMMIT_REF_SLUG}-client'
paths:
- client/node_modules/
- client/.npm/
policy: pull-push
install:server:
stage: install
<<: *only-server
script:
- cd server
- npm ci --cache .npm --prefer-offline
cache:
key: '${CI_COMMIT_REF_SLUG}-server'
paths:
- server/node_modules/
- server/.npm/
policy: pull-push
lint:client:
stage: lint
needs: ['install:client']
allow_failure: true
<<: *only-client
script:
- cd client
- npm run lint
lint:server:
stage: lint
needs: ['install:server']
allow_failure: true
<<: *only-server
script:
- cd server
- npm run lint
test:client:
stage: test
needs: ['install:client']
<<: *only-client
script:
- Xvfb :99 -ac -screen 0 1920x1080x24 &
- cd client
- npm run coverage -- --browsers=ChromeHeadlessNoSandbox --watch=false
dependencies:
- install:client
artifacts:
paths:
- client/coverage/
test:server:
stage: test
needs: ['install:server']
<<: *only-server
script:
- cd server
- npm run coverage
dependencies:
- install:server
artifacts:
paths:
- server/coverage/
pages:
stage: deploy
rules:
- if: '$CI_COMMIT_TAG =~ /deploy/'
when: manual
script:
- cd client
- npm ci --cache .npm --prefer-offline
- npm run deploy -- --base-href $BASE_HREF
- mkdir ../public
- mv dist/client/* ../public/
- cd ..
- bash client/configure_404.sh
artifacts:
paths:
- public
variables:
EC2_USER: ec2-user
ORIGIN: 'https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}'
deploy:server:
stage: deploy
rules:
- if: '$CI_COMMIT_TAG =~ /deploy/'
when: manual
script:
- 'which ssh-agent || (apt-get update -qq && apt-get install -qq openssh-client )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$EC2_PEM_FILE_CONTENT")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- |
timeout 600 ssh -t -oStrictHostKeyChecking=no -o ServerAliveInterval=15 -o ServerAliveCountMax=5 "${EC2_USER}@${EC2_HOST}" "
set -e
echo 'Update repository cache'
sudo yum update -y
echo 'Setting up swap memory'
if test -f '/swapfile'
then
echo 'swap memory is already configured, skipping...'
else
sudo dd if=/dev/zero of=/swapfile bs=128M count=16
sudo chmod 600 /swapfile
sudo mkswap -f /swapfile
sudo swapon /swapfile
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
fi
echo 'Setting up git'
if which git &> /dev/null
then
echo 'git is already installed, skipping...'
else
sudo yum install -y git
fi
echo 'Setting up node'
if which node &> /dev/null
then
echo 'node is already installed, skipping...'
else
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.nvm/nvm.sh
nvm install --lts
nvm alias default node
fi
echo 'Setting up forever'
if which forever &> /dev/null
then
echo 'forever is already installed, skipping...'
else
npm install forever -g
fi
echo 'Setting up amazon-cloudwatch-agent'
if yum list installed amazon-cloudwatch-agent &> /dev/null
then
echo 'amazon-cloudwatch-agent is already installed, skipping...'
else
sudo yum install -y amazon-cloudwatch-agent
sudo amazon-cloudwatch-agent-ctl -a start -m ec2
fi
set -xv
# Force kill any node app or any app running on the server port
forever stopall && sudo fuser -k '${SERVER_PORT}/tcp' && sudo killall node
# Clone the repository
git clone ${ORIGIN} repo || echo Git repository already exist
cd repo
# Update the remote origin
# This action is important because the CI_JOB_TOKEN become invalid after each pipeline
git remote remove origin
git remote add origin ${ORIGIN}
# Checkout to the targeted commit
git fetch --all
git checkout ${CI_COMMIT_SHA}
# Build the project
cd server
npm ci
npm run build
# Create /var/log/messages file if not exists
[[ -f /var/log/messages ]] || sudo touch /var/log/messages
sudo chmod 777 /var/log/messages
# Refresh amazon-cloudwatch-agent config
sudo amazon-cloudwatch-agent-ctl -a fetch-config -s -m ec2 -c file:../amazon-cloudwatch-agent-config.json
# Launch the server in background and append output to /var/log/messages for CloudWatch
PORT=${SERVER_PORT} forever start -a -l /var/log/messages out/server/app/index.js
"