-
Notifications
You must be signed in to change notification settings - Fork 52
205 lines (186 loc) · 7.14 KB
/
Copy pathdeploy.yml
File metadata and controls
205 lines (186 loc) · 7.14 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
name: Deploy
# Deployment pipeline (#226).
#
# push to main → build + push image, deploy to staging
# release published → build + push image, deploy to production
# tag v* → same as a published release
# repository_dispatch(deploy) → production, triggered by release.yml
#
# Every deployment goes through the GitHub Deployments API so status shows up
# on the commit and, for a merged PR, in that PR's checks.
on:
push:
branches: [main]
tags: ["v*"]
release:
types: [published]
repository_dispatch:
types: [deploy]
workflow_dispatch:
inputs:
environment:
description: "Target environment"
required: true
default: staging
type: choice
options:
- staging
- production
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: false
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
permissions:
contents: read
packages: write
deployments: write
jobs:
# ── Decide where this run is going ─────────────────────────────────────────
target:
name: Resolve target environment
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.resolve.outputs.environment }}
steps:
- name: Resolve environment
id: resolve
run: |
case "${{ github.event_name }}" in
release|repository_dispatch) target=production ;;
workflow_dispatch) target="${{ github.event.inputs.environment }}" ;;
push)
if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
target=production
else
target=staging
fi
;;
*) target=staging ;;
esac
echo "environment=$target" >> "$GITHUB_OUTPUT"
echo "Deploying to $target"
# ── Build the image and push it to GHCR ────────────────────────────────────
build-and-push:
name: Build and push image
needs: target
runs-on: ubuntu-latest
outputs:
image: ${{ steps.meta.outputs.tags }}
digest: ${{ steps.build.outputs.digest }}
steps:
- uses: actions/checkout@v5
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Derive image tags
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,format=long
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push
id: build
uses: docker/build-push-action@v6
with:
context: .
target: production
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Summarise
run: |
{
echo "## Image published"
echo ""
echo "| Field | Value |"
echo "|-------|-------|"
echo "| Environment | ${{ needs.target.outputs.environment }} |"
echo "| Digest | \`${{ steps.build.outputs.digest }}\` |"
echo "| Tags | \`${{ steps.meta.outputs.tags }}\` |"
} >> "$GITHUB_STEP_SUMMARY"
# ── Roll the new image out ─────────────────────────────────────────────────
deploy:
name: Deploy to ${{ needs.target.outputs.environment }}
needs: [target, build-and-push]
runs-on: ubuntu-latest
environment:
name: ${{ needs.target.outputs.environment }}
url: ${{ steps.release.outputs.url }}
steps:
- uses: actions/checkout@v5
- name: Open GitHub deployment
id: start
uses: actions/github-script@v7
with:
script: |
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
environment: '${{ needs.target.outputs.environment }}',
auto_merge: false,
required_contexts: [],
description: 'Automated deploy from ${{ github.workflow }}',
});
core.setOutput('id', deployment.data.id);
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.data.id,
state: 'in_progress',
log_url: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
});
- name: Release image to ${{ needs.target.outputs.environment }}
id: release
env:
ENVIRONMENT: ${{ needs.target.outputs.environment }}
IMAGE_DIGEST: ${{ needs.build-and-push.outputs.digest }}
DEPLOY_HOOK_URL: ${{ secrets.DEPLOY_HOOK_URL }}
run: |
set -euo pipefail
IMAGE="${REGISTRY}/${IMAGE_NAME}@${IMAGE_DIGEST}"
echo "Releasing ${IMAGE} to ${ENVIRONMENT}"
if [ -z "${DEPLOY_HOOK_URL:-}" ]; then
echo "::warning::DEPLOY_HOOK_URL is not configured for ${ENVIRONMENT};"\
"the image is published but no host was told to pull it."
echo "url=" >> "$GITHUB_OUTPUT"
exit 0
fi
# The hook is whatever the hosting platform exposes (Render, Fly,
# Railway, a self-hosted webhook). It receives the exact digest so the
# environment runs the image this workflow just built.
curl --fail --silent --show-error --location \
--max-time 120 \
--request POST "${DEPLOY_HOOK_URL}" \
--header "Content-Type: application/json" \
--data "{\"image\":\"${IMAGE}\",\"environment\":\"${ENVIRONMENT}\"}"
echo "url=${DEPLOY_HOOK_URL%%\?*}" >> "$GITHUB_OUTPUT"
- name: Report deployment status
if: always()
uses: actions/github-script@v7
with:
script: |
const success = '${{ job.status }}' === 'success';
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.start.outputs.id }},
state: success ? 'success' : 'failure',
environment_url: '${{ steps.release.outputs.url }}' || undefined,
log_url: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
description: success ? 'Deployment succeeded' : 'Deployment failed',
});