-
Notifications
You must be signed in to change notification settings - Fork 0
239 lines (205 loc) · 8.62 KB
/
deploy.yml
File metadata and controls
239 lines (205 loc) · 8.62 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
name: Deploy
run-name: >-
${{
github.event_name == 'workflow_run' && format('Deploy dev: {0}', github.event.workflow_run.head_commit.message) ||
github.event_name == 'release' && format('Deploy prod: {0}', github.event.release.tag_name) ||
format('Deploy: {0}', github.sha)
}}
concurrency:
group: >-
deploy-${{
github.event_name == 'release' && format('prod-{0}', github.event.release.tag_name) ||
github.event_name == 'workflow_run' && format('ci-{0}', github.event.workflow_run.head_branch) ||
github.ref
}}
cancel-in-progress: true
on:
workflow_run:
workflows: [CI]
types: [completed]
branches: [main]
release:
types: [published]
workflow_dispatch:
inputs:
no_cache:
description: Build without Docker cache
required: false
default: false
type: boolean
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
permissions:
contents: read
packages: write
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
if: >-
github.event_name == 'workflow_dispatch' ||
github.event_name == 'release' ||
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
- name: Set deployment vars
id: vars
run: |
if [[ "${{ github.event_name }}" == "release" ]]; then
echo "image_tag=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
echo "env_file=.env.prod.enc" >> $GITHUB_OUTPUT
echo "stack_name=underlay-prod" >> $GITHUB_OUTPUT
else
echo "image_tag=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "env_file=.env.dev.enc" >> $GITHUB_OUTPUT
echo "stack_name=underlay-dev" >> $GITHUB_OUTPUT
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
target: production
push: true
provenance: false
sbom: false
no-cache: ${{ inputs.no_cache || false }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.image_tag }}
${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Start SSH agent
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Install sops
run: |
curl -LO https://github.com/getsops/sops/releases/download/v3.9.4/sops-v3.9.4.linux.amd64
sudo mv sops-v3.9.4.linux.amd64 /usr/local/bin/sops
sudo chmod +x /usr/local/bin/sops
- name: Extract DEPLOY_HOST from env file
id: host
env:
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_SECRET_KEY }}
run: |
set -euo pipefail
DEPLOY_HOST=$(sops -d --input-type dotenv --output-type dotenv "${{ steps.vars.outputs.env_file }}" | grep '^DEPLOY_HOST=' | cut -d= -f2)
if [[ -z "$DEPLOY_HOST" ]]; then
echo "::error::DEPLOY_HOST not found in ${{ steps.vars.outputs.env_file }}"
exit 1
fi
echo "deploy_host=${DEPLOY_HOST}" >> $GITHUB_OUTPUT
- name: Add known hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -H "${{ steps.host.outputs.deploy_host }}" >> ~/.ssh/known_hosts 2>/dev/null
- name: Deploy over SSH
env:
SSH_USER: ${{ secrets.SSH_USER }}
DEPLOY_HOST: ${{ steps.host.outputs.deploy_host }}
REPO: ${{ github.repository }}
BRANCH: ${{ github.ref_name }}
GHCR_USER: ${{ secrets.GHCR_USER }}
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
IMAGE_TAG: ${{ steps.vars.outputs.image_tag }}
ENV_FILE: ${{ steps.vars.outputs.env_file }}
STACK_NAME: ${{ steps.vars.outputs.stack_name }}
run: |
ssh "${SSH_USER}@${DEPLOY_HOST}" \
"env GHCR_USER='${GHCR_USER}' GHCR_TOKEN='${GHCR_TOKEN}' IMAGE_TAG='${IMAGE_TAG}' ENV_FILE='${ENV_FILE}' STACK_NAME='${STACK_NAME}' bash -s -- '${REPO}' '${BRANCH}'" <<'EOS'
set -euo pipefail
REPO="${1:?missing repo}"
BRANCH="${2:-main}"
: "${IMAGE_TAG:?missing IMAGE_TAG}"
: "${GHCR_USER:?missing GHCR_USER}"
: "${GHCR_TOKEN:?missing GHCR_TOKEN}"
: "${STACK_NAME:?missing STACK_NAME}"
REPO_NAME="${REPO##*/}"
APP_DIR="/srv/${STACK_NAME}"
REPO_SSH="git@github.com:${REPO}.git"
ssh-keyscan -H github.com >> ~/.ssh/known_hosts 2>/dev/null
chmod 600 ~/.ssh/known_hosts
if [[ ! -d "${APP_DIR}/.git" ]]; then
sudo mkdir -p "${APP_DIR}"
sudo chown -R "$USER:$USER" "${APP_DIR}"
git clone --branch "${BRANCH}" "${REPO_SSH}" "${APP_DIR}"
fi
cd "${APP_DIR}"
git fetch --prune --tags origin
git checkout --detach "${IMAGE_TAG}"
: "${ENV_FILE:?missing ENV_FILE}"
umask 077
sops -d --input-type dotenv --output-type dotenv "$ENV_FILE" > .env
# Init swarm if not already active
if ! sudo docker info --format '{{.Swarm.LocalNodeState}}' | grep -qx active; then
sudo docker swarm init
fi
echo "$GHCR_TOKEN" | sudo docker login ghcr.io -u "$GHCR_USER" --password-stdin
sudo docker pull "ghcr.io/${REPO}:${IMAGE_TAG}"
# Deploy/update stack — export .env vars for compose interpolation
# (docker stack deploy does NOT read .env files automatically)
# IMAGE and IMAGE_TAG are set last to prevent .env from overriding them
sudo env \
$(grep -v '^#' .env | grep -v '^$' | xargs) \
IMAGE="ghcr.io/${REPO}" IMAGE_TAG="${IMAGE_TAG}" \
docker stack deploy -c docker-compose.yml \
--with-registry-auth --resolve-image always --prune "${STACK_NAME}"
# Verify the deployed image matches what was built
DEPLOYED_IMAGE=$(sudo docker service inspect "${STACK_NAME}_app" --format '{{.Spec.TaskTemplate.ContainerSpec.Image}}')
echo "Deployed image: ${DEPLOYED_IMAGE}"
if [[ "$DEPLOYED_IMAGE" != *"${IMAGE_TAG}"* ]]; then
echo "::warning::Deployed image tag doesn't match expected ${IMAGE_TAG}"
fi
sudo docker stack services "${STACK_NAME}"
# Wait for rollout
wait_rollout() {
local svc="$1" timeout="${2:-300}"
local end=$((SECONDS + timeout))
while (( SECONDS < end )); do
local state
state="$(sudo docker service inspect "$svc" --format '{{if .UpdateStatus}}{{.UpdateStatus.State}}{{end}}' 2>/dev/null || echo "")"
echo " $svc: update_state=$state"
if [[ "$state" == "rollback_started" || "$state" == "rollback_completed" ]]; then
echo " ERROR: $svc rolled back!"
sudo docker service ps "$svc" --no-trunc --format '{{.Name}} {{.CurrentState}} {{.Error}}' | head -10
return 1
fi
if [[ "$state" == "completed" ]] || [[ -z "$state" ]]; then
# Verify all running tasks are healthy (not just started)
local unhealthy
unhealthy="$(sudo docker service ps "$svc" --filter desired-state=running --format '{{.CurrentState}}' 2>/dev/null | grep -cv '^Running' || true)"
if [[ "$unhealthy" == "0" ]]; then
echo " $svc rollout complete"
return 0
fi
fi
sleep 10
done
echo "Rollout timeout for $svc"
sudo docker service ps "$svc" --no-trunc --format '{{.Name}} {{.CurrentState}} {{.Error}}' | head -10
return 1
}
wait_rollout "${STACK_NAME}_app" 300
wait_rollout "${STACK_NAME}_cron" 120
# Cleanup old images
sudo docker image prune -a --filter "until=72h" -f
echo "Deployed ${STACK_NAME} @ ${IMAGE_TAG} to $(hostname)"
EOS