-
Notifications
You must be signed in to change notification settings - Fork 4
199 lines (172 loc) · 7.3 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
name: 'Deploy'
env:
DIGITALOCEAN_DROPLET_SIZE: s-1vcpu-1gb
DIGITALOCEAN_SSH_KEY_NAME: ci-teamspeak
DIGITALOCEAN_REGION: ams3
DIGITALOCEAN_VOLUME_SIZE: 5GiB
on:
workflow_dispatch:
inputs:
droplet-name:
description: 'Droplet name teamspeak'
required: true
default: "teamspeak"
droplet-volume:
description: 'Droplet volume database'
required: true
default: "database"
jobs:
generate-keys:
name: Generate keys
runs-on: ubuntu-latest
steps:
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: SSH KEY
id: ssh
run: |
ID=$(doctl compute ssh-key list --format ID,Name | grep "${{ env.DIGITALOCEAN_SSH_KEY_NAME }}" | cut -d ' ' -f1)
if [ -z "$ID" ]
then
echo "::set-output name=id::$(doctl compute ssh-key create --format ID,Name --public-key "${{ secrets.PUBLIC_KEY }}" "${{ env.DIGITALOCEAN_SSH_KEY_NAME }}" | grep "${{ env.DIGITALOCEAN_SSH_KEY_NAME }}" | cut -d ' ' -f1)"
else
echo "::set-output name=id::$(echo "$ID")"
fi
outputs:
id: ${{ steps.ssh.outputs.id }}
hydrate-droplet:
name: Hydrate droplet
runs-on: ubuntu-latest
needs:
- generate-keys
steps:
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Check droplet
id: check-droplet
run: echo "::set-output name=value::$(doctl compute droplet list --region "${{ env.DIGITALOCEAN_REGION }}" | grep ${{ github.event.inputs.droplet-name }} | wc -l)"
- name: Abort if droplet name already exists
if: ${{ steps.check-droplet.outputs.value != 0 }}
run: exit 1
- name: Create droplet
id: create-droplet
if: ${{ steps.check-droplet.outputs.value == 0 }}
run: |
doctl compute droplet create \
--image "docker-20-04" \
--size "${{ env.DIGITALOCEAN_DROPLET_SIZE }}" \
--enable-monitoring \
--wait \
--ssh-keys "${{ needs.generate-keys.outputs.id }}" \
--output "json" \
--region "${{ env.DIGITALOCEAN_REGION }}" "${{ github.event.inputs.droplet-name }}"
hydrate-volume:
name: Hydrate volume
runs-on: ubuntu-latest
steps:
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Check volume
id: check-volume
run: echo "::set-output name=value::$(doctl compute volume list --region "${{ env.DIGITALOCEAN_REGION }}" | grep "${{ github.event.inputs.droplet-volume }}" | wc -l)"
- name: Abort if volume name already exists
if: ${{ steps.check-volume.outputs.value != 0 }}
run: echo "${{ steps.check-volume.outputs.value }}" & exit 1
- name: Create volume
if: ${{ steps.check-volume.outputs.value == 0 }}
run: |
doctl compute volume create \
--size "${{ env.DIGITALOCEAN_VOLUME_SIZE }}" \
--region "${{ env.DIGITALOCEAN_REGION }}" "${{ github.event.inputs.droplet-volume }}"
attach:
name: Attach volume
runs-on: ubuntu-latest
needs:
- hydrate-droplet
- hydrate-volume
- generate-keys
steps:
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Get volume id
id: get-volume
run: echo "::set-output name=id::$(doctl compute volume ls --format ID,Name | grep "${{ github.event.inputs.droplet-volume }}" | cut -d ' ' -f1)"
- name: Get droplet id
id: get-droplet
run: |
echo "::set-output name=id::$(doctl compute droplet get --template "{{.ID}}" ${{ github.event.inputs.droplet-name }})"
echo "::set-output name=ip::$(doctl compute droplet get --template "{{.PublicIPv4}}" ${{ github.event.inputs.droplet-name }})"
- name: Attach
if: ${{ success() }}
continue-on-error: true
run: doctl compute volume-action attach --wait ${{ steps.get-volume.outputs.id }} ${{ steps.get-droplet.outputs.id }}
outputs:
droplet-id: ${{ steps.get-droplet.outputs.id }}
droplet-ip: ${{ steps.get-droplet.outputs.ip }}
deploy:
name: Deploy
runs-on: ubuntu-latest
needs:
- attach
- generate-keys
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Create private and public ssh key file
run: |
mkdir ~/.ssh
echo "${{ secrets.PRIVATE_KEY }}" > ~/.ssh/id_rsa
echo "${{ secrets.PUBLIC_KEY }}" > ~/.ssh/id_rsa.pub
touch ~/.ssh/known_hosts
chmod 400 ~/.ssh/id_rsa
chmod 400 ~/.ssh/known_hosts
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Copy docker compose and start script
run: |
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./docker-compose.yml root@${{ needs.attach.outputs.droplet-ip }}:./docker-compose.yml
sed -i '1 a export VOLUME=/mnt/${{ github.event.inputs.droplet-volume }}' ./run.sh
sed -i '1 a export TS3SERVER_DB_PASSWORD=${{ secrets.TS3SERVER_DB_PASSWORD }}' ./run.sh
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./run.sh root@${{ needs.attach.outputs.droplet-ip }}:/etc/init.d/run-teamspeak
- name: Copy licence key
shell: bash
run: |
[ -z "${{ secrets.TS3SERVER_LICENSEKEY }}" ] && echo "No licence key" || echo "${{ secrets.TS3SERVER_LICENSEKEY }}" > licensekey.dat
[ -f "./licensekey.dat" ] && scp -r -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./licensekey.dat root@${{ needs.attach.outputs.droplet-ip }}:./licensekey.dat
- name: Mount volume
uses: garygrossgarten/github-action-ssh@release
with:
username: root
host: ${{ needs.attach.outputs.droplet-ip }}
privateKey: ${{ secrets.PRIVATE_KEY }}
command: |
sudo mkfs.ext4 /dev/disk/by-id/scsi-0DO_Volume_${{ github.event.inputs.droplet-volume }}
mkdir -p /mnt/${{ github.event.inputs.droplet-volume }}
mount -o discard,defaults,noatime /dev/disk/by-id/scsi-0DO_Volume_${{ github.event.inputs.droplet-volume }} /mnt/${{ github.event.inputs.droplet-volume }}
echo '/dev/disk/by-id/scsi-0DO_Volume_${{ github.event.inputs.droplet-volume }} /mnt/${{ github.event.inputs.droplet-volume }} ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
exit
- name: Docker
uses: garygrossgarten/github-action-ssh@release
with:
username: root
host: ${{ needs.attach.outputs.droplet-ip }}
privateKey: ${{ secrets.PRIVATE_KEY }}
command: |
export VOLUME=/mnt/${{ github.event.inputs.droplet-volume }}
export TS3SERVER_DB_PASSWORD=${{ secrets.TS3SERVER_DB_PASSWORD }}
docker-compose config
docker-compose up -d
sleep 60 && docker-compose logs
exit
- name: Your server IP
run: echo "${{ needs.attach.outputs.droplet-ip }}"