-
Notifications
You must be signed in to change notification settings - Fork 64
253 lines (211 loc) · 9.29 KB
/
deploy.yml
File metadata and controls
253 lines (211 loc) · 9.29 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
name: Deploy pubpub
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 dev: {0}', github.sha)
}}
concurrency:
group: >-
deploy-${{
github.event_name == 'release' && format('release-{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]
workflow_dispatch:
inputs:
skip_ci_check:
description: Deploy even if CI failed
required: false
default: false
type: boolean
no_cache:
description: Build without Docker cache
required: false
default: false
type: boolean
release:
types: [published]
jobs:
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')
permissions:
contents: read
packages: write
id-token: write
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 "host=${{ secrets.SSH_HOST_PROD }}" >> $GITHUB_OUTPUT
echo "swarm_addr=${{ secrets.SWARM_ADDR_PROD }}" >> $GITHUB_OUTPUT
echo "env_file=.env.enc" >> $GITHUB_OUTPUT
echo "cache_api_key=${{ secrets.CACHE_API_KEY }}" >> $GITHUB_OUTPUT
echo "cache_purge_all_url=${{ secrets.CACHE_PURGE_ALL_URL }}" >> $GITHUB_OUTPUT
echo "cache_purge_host_url=${{ secrets.CACHE_PURGE_HOST_URL }}" >> $GITHUB_OUTPUT
else
echo "image_tag=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "host=${{ secrets.SSH_HOST_DEV }}" >> $GITHUB_OUTPUT
echo "swarm_addr=${{ secrets.SWARM_ADDR_DEV }}" >> $GITHUB_OUTPUT
echo "env_file=.env.dev.enc" >> $GITHUB_OUTPUT
fi
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
# This secret.GITHUB_TOKEN is generated automatically per workflow
# run from the permissions block above
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
provenance: false
sbom: false
no-cache: ${{ inputs.no_cache || false }}
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |
ghcr.io/${{ github.repository }}:${{ 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: Add known hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -H "${{ steps.vars.outputs.host }}" >> ~/.ssh/known_hosts
- name: Deploy over SSH
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_HOST: ${{ steps.vars.outputs.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 }}
SWARM_ADDR: ${{ steps.vars.outputs.swarm_addr }}
ENV_FILE: ${{ steps.vars.outputs.env_file }}
CACHE_API_KEY: ${{ steps.vars.outputs.cache_api_key }}
CACHE_PURGE_ALL_URL: ${{ steps.vars.outputs.cache_purge_all_url }}
CACHE_PURGE_HOST_URL: ${{ steps.vars.outputs.cache_purge_host_url }}
run: |
ssh "${SSH_USER}@${SSH_HOST}" \
"env GHCR_USER='${GHCR_USER}' GHCR_TOKEN='${GHCR_TOKEN}' IMAGE_TAG='${IMAGE_TAG}' SWARM_ADDR='${SWARM_ADDR}' ENV_FILE='${ENV_FILE}' CACHE_API_KEY='${CACHE_API_KEY}' CACHE_PURGE_ALL_URL='${CACHE_PURGE_ALL_URL}' CACHE_PURGE_HOST_URL='${CACHE_PURGE_HOST_URL}' bash -s -- '${REPO}' '${BRANCH}'" <<'EOS'
set -euo pipefail
REPO="${1:?missing repo}"
BRANCH="${2:-main}"
: "${IMAGE_TAG:?missing IMAGE_TAG}"
DEPLOY_REF="$IMAGE_TAG"
: "${GHCR_USER:?missing GHCR_USER}"
: "${GHCR_TOKEN:?missing GHCR_TOKEN}"
REPO_NAME="${REPO##*/}"
APP_DIR="/srv/${REPO_NAME}"
REPO_SSH="git@github.com:${REPO}.git"
if [[ -z "$REPO_NAME" || -z "$APP_DIR" ]]; then
echo "Bad derived paths: REPO='$REPO' REPO_NAME='$REPO_NAME' APP_DIR='$APP_DIR'"
exit 1
fi
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
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 "${DEPLOY_REF}"
APP_COMMIT="$(git rev-parse HEAD)"
if [[ -z "$APP_COMMIT" ]]; then
echo "Unable to resolve APP_COMMIT from DEPLOY_REF='${DEPLOY_REF}'"
exit 1
fi
cd infra
umask 077
: "${ENV_FILE:?missing ENV_FILE}"
sops -d --input-type dotenv --output-type dotenv "$ENV_FILE" > .env
# one-time / idempotent
if ! sudo docker info --format '{{.Swarm.LocalNodeState}}' | grep -qx active; then
: "${SWARM_ADDR:?missing SWARM_ADDR}"
sudo docker swarm init --advertise-addr "$SWARM_ADDR"
fi
echo "$GHCR_TOKEN" | sudo docker login ghcr.io -u "$GHCR_USER" --password-stdin
echo "IMAGE_TAG in shell: [$IMAGE_TAG]"
echo "APP_COMMIT in shell: [$APP_COMMIT]"
# For some reason, not pulling explicitly makes the docker stack deploy throw an error that it can't find the package.
sudo docker pull ghcr.io/knowledgefutures/pubpub:"$IMAGE_TAG"
# deploy/update stack
sudo env IMAGE_TAG="$IMAGE_TAG" APP_COMMIT="$APP_COMMIT" docker stack deploy -c stack.yml --with-registry-auth --resolve-image always --prune pubpub
# show progress
sudo docker stack services pubpub
# Remove images not used by running containers, keeping the last 72h for rollback
sudo docker image prune -a --filter "until=72h" -f
# wait until rollout is complete and then clear cache
wait_rollout() {
echo "Beginning wait for rollout..."
svc="$1"
timeout="${2:-600}"
end=$((SECONDS+timeout))
while (( SECONDS < end )); do
desired="$(sudo docker service inspect "$svc" --format '{{.Spec.Mode.Replicated.Replicas}}' 2>/dev/null || echo "")"
running="$(sudo docker service ps "$svc" --filter desired-state=running --format '{{.CurrentState}}' 2>/dev/null | grep -c '^Running' || true)"
state="$(sudo docker service inspect "$svc" --format '{{if .UpdateStatus}}{{.UpdateStatus.State}}{{end}}' 2>/dev/null || echo "")"
echo " $svc: desired=$desired running=$running state=$state"
if [[ -n "$desired" && "$running" == "$desired" ]] && { [[ -z "$state" ]] || [[ "$state" == "completed" ]]; }; then
echo " $svc rollout complete"
return 0
fi
sleep 5
done
echo "Rollout timeout for $svc"
return 1
}
wait_rollout pubpub_app 600
wait_rollout pubpub_worker 300
wait_rollout pubpub_pubstash 300
wait_rollout pubpub_cron 300
if [[ -n "${CACHE_API_KEY:-}" ]]; then
if [[ -n "${CACHE_PURGE_ALL_URL:-}" ]]; then
curl -fsS -X POST \
-H "Fastly-Key: $CACHE_API_KEY" \
-H "Fastly-Soft-Purge: 1" \
"$CACHE_PURGE_ALL_URL"
echo "Cache: soft purged all"
fi
if [[ -n "${CACHE_PURGE_HOST_URL:-}" ]]; then
curl -fsS -X POST \
-H "Fastly-Key: $CACHE_API_KEY" \
"$CACHE_PURGE_HOST_URL"
echo "Cache: purged host"
fi
fi
EOS