forked from stardew-valley-dedicated-server/server
-
Notifications
You must be signed in to change notification settings - Fork 0
257 lines (229 loc) · 9.09 KB
/
deploy-server.yml
File metadata and controls
257 lines (229 loc) · 9.09 KB
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
name: Deploy Server
on:
workflow_run:
workflows: ["Build Preview"]
types:
- completed
branches:
- master
release:
types:
- published
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy'
required: true
type: choice
options:
- public-test
# - production
skip_graceful_shutdown:
description: 'Skip graceful shutdown (emergency deploy)'
required: false
default: false
type: boolean
concurrency:
group: deploy-${{ github.event.inputs.environment || 'auto' }}
cancel-in-progress: false
permissions: {}
jobs:
prepare:
name: Prepare deployment
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
has_targets: ${{ steps.set-matrix.outputs.has_targets }}
steps:
- name: Build deployment matrix
id: set-matrix
env:
TRIGGER: ${{ github.event_name }}
SELECTED: ${{ github.event.inputs.environment }}
WORKFLOW_SUCCESS: ${{ github.event.workflow_run.conclusion }}
run: |
# Define deployment targets as JSON
TARGETS='[
{"environment": "public-test", "image_tag": "preview", "on_preview": true, "on_release": false}
]'
# {"environment": "production", "image_tag": "latest", "on_preview": false, "on_release": true}
MATRIX=$(echo "$TARGETS" | jq -c '[.[] | select(
(env.TRIGGER == "workflow_dispatch" and env.SELECTED == .environment) or
(env.TRIGGER == "workflow_run" and env.WORKFLOW_SUCCESS == "success" and .on_preview) or
(env.TRIGGER == "release" and .on_release)
) | {environment, image_tag}]')
echo "matrix={\"include\":$MATRIX}" >> $GITHUB_OUTPUT
echo "has_targets=$([ "$MATRIX" != "[]" ] && echo true || echo false)" >> $GITHUB_OUTPUT
deploy:
name: Deploy ${{ matrix.environment }}
needs: prepare
if: needs.prepare.outputs.has_targets == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.prepare.outputs.matrix) }}
environment: ${{ matrix.environment }}
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
sparse-checkout: |
docker-compose.yml
sparse-checkout-cone-mode: false
- name: Graceful shutdown preparation
if: github.event.inputs.skip_graceful_shutdown != 'true'
run: |
echo "::notice::Graceful shutdown hook placeholder for ${{ matrix.environment }}"
- name: Create .env file
env:
DEPLOY_GAME_PORT: ${{ secrets.DEPLOY_GAME_PORT }}
DEPLOY_VNC_PORT: ${{ secrets.DEPLOY_VNC_PORT }}
DEPLOY_STEAM_AUTH_PORT: ${{ secrets.DEPLOY_STEAM_AUTH_PORT }}
run: |
echo "::group::Generate .env file for ${{ matrix.environment }}"
# Validate required secrets
missing=""
[[ -z "$DEPLOY_GAME_PORT" ]] && missing="$missing DEPLOY_GAME_PORT"
[[ -z "$DEPLOY_VNC_PORT" ]] && missing="$missing DEPLOY_VNC_PORT"
[[ -z "$DEPLOY_STEAM_AUTH_PORT" ]] && missing="$missing DEPLOY_STEAM_AUTH_PORT"
if [[ -n "$missing" ]]; then
echo "::error::Missing secrets:$missing"
echo "Configure these in Settings → Environments → ${{ matrix.environment }}"
exit 1
fi
cat > .env << EOF
# Auto-generated by GitHub Actions
# Environment: ${{ matrix.environment }}
IMAGE_VERSION=${{ matrix.image_tag }}
STEAM_USERNAME=${{ secrets.DEPLOY_STEAM_USERNAME }}
STEAM_PASSWORD=${{ secrets.DEPLOY_STEAM_PASSWORD }}
STEAM_REFRESH_TOKEN=${{ secrets.DEPLOY_STEAM_REFRESH_TOKEN }}
VNC_PASSWORD=${{ secrets.DEPLOY_VNC_PASSWORD }}
GAME_PORT=$DEPLOY_GAME_PORT
VNC_PORT=$DEPLOY_VNC_PORT
STEAM_AUTH_PORT=$DEPLOY_STEAM_AUTH_PORT
DISABLE_RENDERING=true
# API authentication
API_KEY=${{ secrets.DEPLOY_API_KEY }}
# Discord bot (optional)
DISCORD_BOT_TOKEN=${{ secrets.DEPLOY_DISCORD_BOT_TOKEN }}
DISCORD_CHAT_CHANNEL_ID=${{ secrets.DEPLOY_DISCORD_CHAT_CHANNEL_ID }}
EOF
sed -i 's/^ //' .env
echo "::endgroup::"
- name: Create deploy directory
uses: appleboy/ssh-action@0ff4204d59e8e51228ff73bce53f80d53301dee2 # v1.2.5
with:
host: ${{ secrets.DEPLOY_SSH_HOST }}
username: ${{ secrets.DEPLOY_SSH_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_SSH_PORT || 22 }}
script: mkdir -p ~/srv/${{ matrix.environment }}
- name: Copy files to server
uses: appleboy/scp-action@ff85246acaad7bdce478db94a363cd2bf7c90345 # v1.0.0
with:
host: ${{ secrets.DEPLOY_SSH_HOST }}
username: ${{ secrets.DEPLOY_SSH_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_SSH_PORT || 22 }}
source: "docker-compose.yml,.env"
target: ~/srv/${{ matrix.environment }}
- name: Pull and restart containers
uses: appleboy/ssh-action@0ff4204d59e8e51228ff73bce53f80d53301dee2 # v1.2.5
with:
host: ${{ secrets.DEPLOY_SSH_HOST }}
username: ${{ secrets.DEPLOY_SSH_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_SSH_PORT || 22 }}
script: |
set -e
cd ~/srv/${{ matrix.environment }}
echo "::group::Pull images"
docker compose pull
echo "::endgroup::"
echo "::group::Stop containers"
docker compose down --timeout 30 || true
echo "::endgroup::"
echo "::group::Setup steam-auth and download game"
docker compose run --rm steam-auth setup
echo "::endgroup::"
echo "::group::Start containers"
docker compose up -d
echo "::endgroup::"
echo "::group::Cleanup"
docker image prune -f
echo "::endgroup::"
- name: Verify deployment
uses: appleboy/ssh-action@0ff4204d59e8e51228ff73bce53f80d53301dee2 # v1.2.5
with:
host: ${{ secrets.DEPLOY_SSH_HOST }}
username: ${{ secrets.DEPLOY_SSH_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
port: ${{ secrets.DEPLOY_SSH_PORT || 22 }}
script: |
set -e
cd ~/srv/${{ matrix.environment }}
echo "::group::Wait for startup"
sleep 30
echo "::endgroup::"
echo "::group::Health check"
if docker compose ps | grep -qE "unhealthy|Exit"; then
echo "::error::Container health check failed"
docker compose logs --tail=50
exit 1
fi
docker compose ps
echo "::endgroup::"
- name: Build summary
run: |
echo "### Deployed: ${{ matrix.environment }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Setting | Value |" >> $GITHUB_STEP_SUMMARY
echo "|---------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| Environment | \`${{ matrix.environment }}\` |" >> $GITHUB_STEP_SUMMARY
echo "| Image | \`${{ matrix.image_tag }}\` |" >> $GITHUB_STEP_SUMMARY
- name: Discord notification
if: success()
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
if [ -n "$DISCORD_WEBHOOK" ]; then
jq -n \
--arg env "${{ matrix.environment }}" \
--arg tag "${{ matrix.image_tag }}" \
--arg commit "${GITHUB_SHA:0:7}" \
'{
embeds: [{
title: "Server Deployed",
color: 3066993,
fields: [
{name: "Environment", value: $env, inline: true},
{name: "Image", value: ("`" + $tag + "`"), inline: true},
{name: "Commit", value: ("`" + $commit + "`"), inline: true}
]
}]
}' | curl -H "Content-Type: application/json" -d @- "$DISCORD_WEBHOOK"
fi
- name: Discord notification (failure)
if: failure()
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
if [ -n "$DISCORD_WEBHOOK" ]; then
jq -n \
--arg env "${{ matrix.environment }}" \
--arg commit "${GITHUB_SHA:0:7}" \
'{
embeds: [{
title: "Deployment Failed",
color: 15158332,
fields: [
{name: "Environment", value: $env, inline: true},
{name: "Commit", value: ("`" + $commit + "`"), inline: true}
]
}]
}' | curl -H "Content-Type: application/json" -d @- "$DISCORD_WEBHOOK"
fi