This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
445 lines (386 loc) · 15.8 KB
/
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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
---
name: CI
run-name: ansible-easy-vpn Testing
on:
push:
pull_request_review:
workflow_dispatch:
inputs:
only_ubuntu_22:
description: "Only run on Ubuntu 22.04"
required: false
type: boolean
default: false
only_ubuntu_20:
description: "Only run on Ubuntu 20.04"
required: false
type: boolean
default: false
only_debian_11:
description: "Only run on Debian 11"
required: false
type: boolean
default: false
only_debian_12:
description: "Only run on Debian 12"
required: false
type: boolean
default: false
manual_mode:
description: "Don't destroy the server after the setup is complete"
required: false
type: boolean
default: false
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout the current branch
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.10"
- name: Install ansible-lint
run: pip install ansible ansible-lint
- name: Comment out the ask_vault_password bit
run: "sed -i'.bak' -e 's/ask_vault_pass/#ask_vault_pass/' ansible.cfg"
- name: Ansible-lint
run: ansible-lint
matrix_prep:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
letsencrypt_staging: ${{ steps.set-matrix.outputs.letsencrypt_staging }}
steps:
- name: Checkout the code
uses: actions/checkout@v2
- id: set-matrix
run: |
if [[ ${ONLY_UBUNTU_22} == 'true' ]]; then
# Only deploy on Ubuntu 22.04, don't use Letsencrypt Staging
matrix=$(jq 'map(. | select((.os=="ubuntu-22.04")) )' .github/workflows/matrix_includes.json)
elif [[ ${ONLY_UBUNTU_20} == 'true' ]]; then
# Only deploy on Ubuntu 20.04, don't use Letsencrypt Staging
matrix=$(jq 'map(. | select((.os=="ubuntu-20.04")) )' .github/workflows/matrix_includes.json)
elif [[ ${ONLY_DEBIAN_11} == 'true' ]]; then
# Only deploy on Debian 11, don't use Letsencrypt Staging
matrix=$(jq 'map(. | select((.os=="debian-11")) )' .github/workflows/matrix_includes.json)
elif [[ ${ONLY_DEBIAN_12} == 'true' ]]; then
# Only deploy on Debian 11, don't use Letsencrypt Staging
matrix=$(jq 'map(. | select((.os=="debian-12")) )' .github/workflows/matrix_includes.json)
else
# Deploy on all supported OSes, use Letsencrypt Staging to avoid rate-limiting
matrix=$(jq 'map(.)' .github/workflows/matrix_includes.json)
fi
echo "matrix={\"include\":$(echo $matrix)}" >> $GITHUB_OUTPUT
echo "letsencrypt_staging='yes'" >> $GITHUB_OUTPUT
env:
ONLY_UBUNTU_22: ${{ inputs.only_ubuntu_22 }}
ONLY_UBUNTU_20: ${{ inputs.only_ubuntu_20 }}
ONLY_DEBIAN_11: ${{ inputs.only_debian_11 }}
ONLY_DEBIAN_12: ${{ inputs.only_debian_12 }}
build:
runs-on: ubuntu-latest
needs: matrix_prep
environment: cicd
strategy:
fail-fast: false
matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
steps:
- name: Initialize the ssh-agent
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Checkout the branch locally
uses: actions/checkout@v3
- name: Update apt cache on the runner and install dependencies
run: sudo apt update && sudo apt install sshpass expect wamerican
- name: Generate a random username and password
run: |
echo "EASYVPN_USERNAME=$(shuf -n 1 /usr/share/dict/words | tr '[:upper:]' '[:lower:]' | tr -dc '[:alnum:]' )" >> $GITHUB_ENV &&
echo "EASYVPN_PASSWORD=$(openssl rand -base64 20)" >> $GITHUB_ENV
- name: Set the username and password outputs
id: random_username
run: |
case $INDEX in
"1")
echo "EASYVPN_USERNAME_1=$EASYVPN_USERNAME" >> $GITHUB_OUTPUT
echo "EASYVPN_PASSWORD_1=$EASYVPN_PASSWORD" >> $GITHUB_OUTPUT
;;
"2")
echo "EASYVPN_USERNAME_2=$EASYVPN_USERNAME" >> $GITHUB_OUTPUT
echo "EASYVPN_PASSWORD_2=$EASYVPN_PASSWORD" >> $GITHUB_OUTPUT
;;
"3")
echo "EASYVPN_USERNAME_3=$EASYVPN_USERNAME" >> $GITHUB_OUTPUT
echo "EASYVPN_PASSWORD_3=$EASYVPN_PASSWORD" >> $GITHUB_OUTPUT
;;
"4")
echo "EASYVPN_USERNAME_4=$EASYVPN_USERNAME" >> $GITHUB_OUTPUT
echo "EASYVPN_PASSWORD_4=$EASYVPN_PASSWORD" >> $GITHUB_OUTPUT
;;
*)
exit 1
;;
esac
env:
INDEX: ${{ matrix.index }}
- name: Spawn a Hetzner node
uses: TimDaub/hetzner-cloud-deploy-server-action@v2
with:
server-name: ansible-easy-vpn-${{ env.EASYVPN_USERNAME }}
server-image: ${{ matrix.os }}
server-type: "cx11"
ssh-key-name: "github-actions"
hcloud-token: ${{ secrets.HCLOUD_TOKEN }}
delete-server: false
- name: Add the host key to known hosts
run: mkdir -p ~/.ssh/ && ssh-keyscan -H $SERVER_IPV4 >> ~/.ssh/known_hosts
- name: Update apt cache
run: ssh root@$SERVER_IPV4 apt update
- name: Install git and expect (Debian-based)
run: ssh root@$SERVER_IPV4 apt install -y git expect wamerican
- uses: infraway/[email protected]
with:
type: "A"
name: "${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
content: "${{ env.SERVER_IPV4 }}"
proxied: false
token: ${{ secrets.CLOUDFLARE_TOKEN }}
zone: ${{ secrets.CLOUDFLARE_ZONE }}
- uses: infraway/[email protected]
with:
type: "CNAME"
name: "auth.${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
content: "${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
proxied: false
token: ${{ secrets.CLOUDFLARE_TOKEN }}
zone: ${{ secrets.CLOUDFLARE_ZONE }}
- uses: infraway/[email protected]
with:
type: "CNAME"
name: "wg.${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
content: "${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
proxied: false
token: ${{ secrets.CLOUDFLARE_TOKEN }}
zone: ${{ secrets.CLOUDFLARE_ZONE }}
- uses: infraway/[email protected]
with:
type: "CNAME"
name: "adguard.${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
content: "${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
proxied: false
token: ${{ secrets.CLOUDFLARE_TOKEN }}
zone: ${{ secrets.CLOUDFLARE_ZONE }}
- name: Clone the branch to the Hetzner instance
run: ssh root@$SERVER_IPV4 git clone https://github.com/notthebee/ansible-easy-vpn -b ${{ github.head_ref || github.ref_name }}
env:
GIT_SSL_NO_VERIFY: "true"
- name: Execute the automated bootstrap script
run: >-
ssh -t -t root@$SERVER_IPV4
"export LETSENCRYPT_STAGING=$LETSENCRYPT_STAGING &&
ansible-easy-vpn/testing/expect/defaults.exp
--username $EASYVPN_USERNAME
--password $EASYVPN_PASSWORD
--smtp_server $SMTP_SERVER
--smtp_login $SMTP_LOGIN
--smtp_password $SMTP_PASSWORD
--domain $DOMAIN"
env:
LETSENCRYPT_STAGING: ${{ needs.matrix_prep.outputs.letsencrypt_staging }}
SMTP_SERVER: ${{ secrets.SMTP_SERVER }}
SMTP_LOGIN: ${{ secrets.SMTP_LOGIN }}
SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }}
DOMAIN: ${{ env.EASYVPN_USERNAME }}.${{ secrets.CLOUDFLARE_DOMAIN }}
- name: Copy the private key from the Hetzner host
run: sshpass -p$EASYVPN_PASSWORD scp root@$SERVER_IPV4:/tmp/id_ssh_ed25519 ~/.ssh/id_vpn
- name: Initialize the ssh-agent
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Regsiter the private key with the ssh-agent
run: expect -c "spawn ssh-add $HOME/.ssh/id_vpn; expect -re \"Enter passphrase.*\"; send -- \"$EASYVPN_PASSWORD\r\"; expect -re \"Identity added.*\""
- name: Reboot the server
run: |
ssh -t -t $EASYVPN_USERNAME@$SERVER_IPV4 "sudo reboot" || true
- name: Wait for the server to reboot
run: >-
sleep 60s
- name: Copy the private key to the Github root
run: cp $HOME/.ssh/id_vpn id_vpn
- name: Log into the server and execute the script again for idempotency
run: >-
ssh -t -t $EASYVPN_USERNAME@$SERVER_IPV4 "export LETSENCRYPT_STAGING=$LETSENCRYPT_STAGING; ansible-easy-vpn/testing/expect/idempotency.exp --password $EASYVPN_PASSWORD"
env:
LETSENCRYPT_STAGING: ${{ needs.matrix_prep.outputs.letsencrypt_staging }}
- name: Add the personal SSH key for debugging
run: >-
curl https://github.com/notthebee.keys > notthebee.pub &&
ssh-copy-id -f -i notthebee.pub $EASYVPN_USERNAME@$SERVER_IPV4
- name: Archive the private SSH key (Matrix 1)
if: ${{ matrix.index == '1' }}
uses: actions/upload-artifact@v3
with:
name: "private-ssh-key-1"
path: "id_vpn"
- name: Archive the private SSH key (Matrix 2)
if: ${{ matrix.index == '2' }}
uses: actions/upload-artifact@v3
with:
name: "private-ssh-key-2"
path: "id_vpn"
- name: Archive the private SSH key (Matrix 3)
if: ${{ matrix.index == '3' }}
uses: actions/upload-artifact@v3
with:
name: "private-ssh-key-3"
path: "id_vpn"
- name: Archive the private SSH key (Matrix 4)
if: ${{ matrix.index == '4' }}
uses: actions/upload-artifact@v3
with:
name: "private-ssh-key-4"
path: "id_vpn"
outputs:
EASYVPN_USERNAME_1: "${{ steps.random_username.outputs.EASYVPN_USERNAME_1 }}"
EASYVPN_USERNAME_2: "${{ steps.random_username.outputs.EASYVPN_USERNAME_2 }}"
EASYVPN_USERNAME_3: "${{ steps.random_username.outputs.EASYVPN_USERNAME_3 }}"
EASYVPN_USERNAME_4: "${{ steps.random_username.outputs.EASYVPN_USERNAME_4 }}"
EASYVPN_PASSWORD_1: "${{ steps.random_username.outputs.EASYVPN_PASSWORD_1 }}"
EASYVPN_PASSWORD_2: "${{ steps.random_username.outputs.EASYVPN_PASSWORD_2 }}"
EASYVPN_PASSWORD_3: "${{ steps.random_username.outputs.EASYVPN_PASSWORD_3 }}"
EASYVPN_PASSWORD_4: "${{ steps.random_username.outputs.EASYVPN_PASSWORD_4 }}"
debug:
runs-on: ubuntu-latest
environment: cicd
strategy:
fail-fast: false
matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
needs:
- matrix_prep
- build
steps:
- name: Wait for the server to reboot
if: inputs.manual_mode
run: >-
sleep infinity
fetch_config:
runs-on: ubuntu-latest
environment: cicd
strategy:
fail-fast: false
matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
needs:
- matrix_prep
- debug
- build
steps:
- name: Check out this repo
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.10"
- name: Install expect
run: >-
sudo apt install expect
- name: Create the .ssh folder
run: >-
mkdir /home/runner/.ssh
- name: Get the private SSH key artifact (Matrix 1)
if: matrix.index == '1'
uses: actions/download-artifact@v3
with:
name: "private-ssh-key-1"
path: /home/runner/.ssh
- name: Get the private SSH key artifact (Matrix 2)
if: matrix.index == '2'
uses: actions/download-artifact@v3
with:
name: "private-ssh-key-2"
path: /home/runner/.ssh
- name: Get the private SSH key artifact (Matrix 3)
if: matrix.index == '3'
uses: actions/download-artifact@v3
with:
name: "private-ssh-key-3"
path: /home/runner/.ssh
- name: Get the private SSH key artifact (Matrix 4)
if: matrix.index == '4'
uses: actions/download-artifact@v3
with:
name: "private-ssh-key-4"
path: /home/runner/.ssh
- name: Set the correct permissions for the SSH key
run: |
chmod 700 $HOME/.ssh
chmod 600 $HOME/.ssh/*
- name: Initialize the ssh-agent
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Regsiter the private key with the ssh-agent
run: expect -c "spawn ssh-add $HOME/.ssh/id_vpn; expect -re \"Enter passphrase.*\"; send -- \"$EASYVPN_PASSWORD\r\"; expect -re \"Identity added.*\""
env:
EASYVPN_PASSWORD: "${{ needs.build.outputs[format('EASYVPN_PASSWORD_{0}', matrix.index)] }}"
- name: Remove Chrome
run: sudo apt purge google-chrome-stable
- name: Remove default Chromium
run: sudo apt purge chromium-browser
- name: Install snapd
run: sudo apt install snapd
- name: Install chromium
run: sudo snap install chromium
- name: Install all necessary packages
run: pip install webdriver-manager selenium pyotp pexpect
- name: Create the config dir
run: mkdir /home/runner/wireguard
- name: Run the testing script
run: python testing/selenium/acceptance.py --username $EASYVPN_USERNAME --password $EASYVPN_PASSWORD --base_url $DOMAIN --ssh_agent $SSH_AUTH_SOCK
env:
EASYVPN_USERNAME: "${{ needs.build.outputs[format('EASYVPN_USERNAME_{0}', matrix.index)] }}"
EASYVPN_PASSWORD: "${{ needs.build.outputs[format('EASYVPN_PASSWORD_{0}', matrix.index)] }}"
DOMAIN: "${{ needs.build.outputs[format('EASYVPN_USERNAME_{0}', matrix.index)] }}.${{ secrets.CLOUDFLARE_DOMAIN }}"
- name: Upload Selenium testing screenshots
if: always()
uses: actions/upload-artifact@v4
with:
name: "Screenshots"
path: "screenshots/"
destroy:
runs-on: ubuntu-latest
environment: cicd
strategy:
fail-fast: false
matrix: ${{fromJson(needs.matrix_prep.outputs.matrix)}}
if: always()
needs:
- matrix_prep
- build
- debug
- fetch_config
steps:
- name: Destroy the Hetzner instances
run: >-
export SERVER_ID=$(curl -H
"Authorization: Bearer $HCLOUD_TOKEN" https://api.hetzner.cloud/v1/servers |
jq --arg SERVER_NAME "$SERVER_NAME" '.servers[] | select(.name==$SERVER_NAME) | .id') &&
curl -X DELETE -H "Authorization: Bearer $HCLOUD_TOKEN"
https://api.hetzner.cloud/v1/servers/$SERVER_ID
env:
HCLOUD_TOKEN: ${{ secrets.HCLOUD_TOKEN }}
SERVER_NAME: ansible-easy-vpn-${{ needs.build.outputs[format('EASYVPN_USERNAME_{0}', matrix.index)] }}
- name: Delete all Cloudflare domains
run: |
curl --silent "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?per_page=50000" \
--header "Authorization: Bearer $CLOUDFLARE_TOKEN" \
| jq --raw-output '.result[].id' | while read id
do
curl --silent --request DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$id" \
--header "Authorization: Bearer $CLOUDFLARE_TOKEN"
done
env:
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE }}