-
Notifications
You must be signed in to change notification settings - Fork 0
109 lines (100 loc) · 3.65 KB
/
Copy pathdeploy.yml
File metadata and controls
109 lines (100 loc) · 3.65 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
name: Shared Deploy
on:
workflow_call:
inputs:
runner:
description: >-
Runner the jobs run on, as a JSON array of labels. Defaults to the self-hosted runner of the
organisation in Helsinki; pass another array, such as '["ubuntu-latest"]', for a job that
really needs a GitHub-hosted VM.
required: false
type: string
default: '["self-hosted", "helsinki"]'
environment:
description: Target environment name (dev, staging, prod).
required: true
type: string
image_name:
description: Image name without tag. Defaults to ghcr.io/<repo>.
required: false
type: string
default: ''
image_tag:
description: Explicit tag to deploy (e.g. sha-<commit> for a rollback). Defaults to the environment mapping (prod -> main, otherwise <environment>).
required: false
type: string
default: ''
health_url:
description: URL polled after deploy to confirm the app is healthy.
required: true
type: string
secrets:
WATCHTOWER_URL:
required: true
WATCHTOWER_TOKEN:
required: true
permissions:
contents: read
jobs:
deploy:
name: Deploy ${{ inputs.environment }}
runs-on: ${{ fromJSON(inputs.runner) }}
timeout-minutes: 15
environment: ${{ inputs.environment }}
steps:
- name: Resolve image reference
id: image
env:
IMAGE_NAME: ${{ inputs.image_name }}
IMAGE_TAG: ${{ inputs.image_tag }}
REPOSITORY: ${{ github.repository }}
ENVIRONMENT: ${{ inputs.environment }}
run: |
name="${IMAGE_NAME:-ghcr.io/${REPOSITORY}}"
name="${name,,}"
tag="$IMAGE_TAG"
if [ -z "$tag" ]; then
case "$ENVIRONMENT" in
prod) tag="main" ;;
*) tag="$ENVIRONMENT" ;;
esac
fi
echo "ref=${name}:${tag}" >> "$GITHUB_OUTPUT"
echo "Triggering update for: ${name}:${tag}"
- name: Trigger Watchtower update
env:
WATCHTOWER_URL: ${{ secrets.WATCHTOWER_URL }}
WATCHTOWER_TOKEN: ${{ secrets.WATCHTOWER_TOKEN }}
IMAGE_REF: ${{ steps.image.outputs.ref }}
run: |
response="$(curl -fsS -X POST -G \
--retry 3 --retry-delay 5 --max-time 300 \
-H "Authorization: Bearer $WATCHTOWER_TOKEN" \
--data-urlencode "image=$IMAGE_REF" \
"$WATCHTOWER_URL/v1/update")"
echo "$response" | jq .
failed="$(echo "$response" | jq -r '.summary.failed // 0')"
updated="$(echo "$response" | jq -r '.summary.updated // 0')"
if [ "$failed" != "0" ]; then
echo "::error::Watchtower reported $failed failed update(s)."
exit 1
fi
if [ "$updated" = "0" ]; then
echo "::warning::No container was updated (image already current, or no container matches $IMAGE_REF — check the watchtower label and the image tag in the compose file)."
fi
- name: Wait for health check
env:
HEALTH_URL: ${{ inputs.health_url }}
run: |
sleep 5
for attempt in $(seq 1 30); do
status="$(curl -s -o /dev/null -w '%{http_code}' "$HEALTH_URL" || echo 000)"
if [ "$status" = "200" ]; then
echo "Healthy after $attempt attempt(s)."
exit 0
fi
echo "Attempt $attempt/30: $HEALTH_URL returned $status, retrying in 10s..."
sleep 10
done
echo "::error::$HEALTH_URL did not return 200 within 5 minutes."
exit 1