-
Notifications
You must be signed in to change notification settings - Fork 2
298 lines (267 loc) · 11.7 KB
/
deploy.yml
File metadata and controls
298 lines (267 loc) · 11.7 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
name: Deploy to Cloudflare Containers
# ─────────────────────────────────────────────────────────────────────────
# Build + push Docker images for cloud (cloud.objectos.app) and/or
# objectos (single-project runtime per env), then `wrangler deploy` the
# matching Worker so the new image is pinned.
#
# Triggers:
# 1. Manual: Actions → "Deploy to Cloudflare Containers" → Run
# (choose `target` = cloud | objectos | both, and optionally
# the commit SHA to deploy; defaults to the workflow ref).
# 2. Tag: push a tag matching `deploy-cloud-*`, `deploy-objectos-*`,
# or `deploy-all-*`. The tag suffix is informational; the
# image tag = the resolved commit SHA (short, 8 chars) so a
# redeploy of the same SHA is idempotent.
#
# Image naming:
# registry.cloudflare.com/<ACCOUNT_ID>/objectstack-cloud:<sha8>
# registry.cloudflare.com/<ACCOUNT_ID>/objectos:<sha8>
#
# The corresponding `apps/<target>/wrangler.toml` is rewritten in-place to
# pin `image = "<registry>:<sha8>"` and committed back to `main` so the
# repo always tracks what is actually deployed.
#
# Required secrets (Repository → Settings → Secrets and variables → Actions):
# CLOUDFLARE_API_TOKEN — token with Containers:Edit + Workers:Edit perms
# CLOUDFLARE_ACCOUNT_ID — 2846eb40a60f4738e292b90dcd8cce10
# ─────────────────────────────────────────────────────────────────────────
on:
workflow_dispatch:
inputs:
target:
description: 'Which app(s) to deploy'
required: true
default: 'both'
type: choice
options:
- cloud
- objectos
- both
ref:
description: 'Commit SHA or branch to deploy (default: workflow ref)'
required: false
default: ''
push:
tags:
- 'deploy-cloud-*'
- 'deploy-objectos-*'
- 'deploy-all-*'
concurrency:
# Serialize deploys per-target so two pushes don't race on `wrangler deploy`.
group: deploy-${{ github.event.inputs.target || github.ref_name }}
cancel-in-progress: false
jobs:
# ── Resolve which targets to build based on the trigger ──
plan:
runs-on: ubuntu-latest
outputs:
deploy_cloud: ${{ steps.plan.outputs.deploy_cloud }}
deploy_objectos: ${{ steps.plan.outputs.deploy_objectos }}
sha: ${{ steps.plan.outputs.sha }}
sha8: ${{ steps.plan.outputs.sha8 }}
steps:
- name: Checkout (resolve ref)
uses: actions/checkout@v5
with:
ref: ${{ github.event.inputs.ref || github.ref }}
fetch-depth: 1
- id: plan
name: Plan targets + resolve SHA
run: |
set -euo pipefail
SHA="$(git rev-parse HEAD)"
SHA8="$(git rev-parse --short=8 HEAD)"
echo "sha=$SHA" >> "$GITHUB_OUTPUT"
echo "sha8=$SHA8" >> "$GITHUB_OUTPUT"
DEPLOY_CLOUD=false
DEPLOY_OBJECTOS=false
case "${{ github.event_name }}" in
workflow_dispatch)
T="${{ github.event.inputs.target }}"
[[ "$T" == "cloud" || "$T" == "both" ]] && DEPLOY_CLOUD=true
[[ "$T" == "objectos" || "$T" == "both" ]] && DEPLOY_OBJECTOS=true
;;
push)
REF="${{ github.ref_name }}"
[[ "$REF" == deploy-cloud-* || "$REF" == deploy-all-* ]] && DEPLOY_CLOUD=true
[[ "$REF" == deploy-objectos-* || "$REF" == deploy-all-* ]] && DEPLOY_OBJECTOS=true
;;
esac
echo "deploy_cloud=$DEPLOY_CLOUD" >> "$GITHUB_OUTPUT"
echo "deploy_objectos=$DEPLOY_OBJECTOS" >> "$GITHUB_OUTPUT"
echo "─────────────────────────────────"
echo "Trigger: ${{ github.event_name }}"
echo "SHA: $SHA"
echo "Image tag: $SHA8"
echo "Cloud: $DEPLOY_CLOUD"
echo "ObjectOS: $DEPLOY_OBJECTOS"
# ── Build + push cloud image; pin tag in wrangler.toml; wrangler deploy ──
cloud:
needs: plan
if: needs.plan.outputs.deploy_cloud == 'true'
runs-on: ubuntu-latest
permissions:
contents: write # to commit the wrangler.toml tag bump back to main
env:
ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
IMAGE_NAME: objectstack-cloud
SHA8: ${{ needs.plan.outputs.sha8 }}
steps:
- uses: actions/checkout@v5
with:
ref: ${{ needs.plan.outputs.sha }}
# Need full history so we can push back the tag-bump commit.
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up pnpm
uses: pnpm/action-setup@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- name: Install workspace deps (for wrangler bundle)
run: pnpm install --frozen-lockfile --prefer-offline
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build cloud image (local)
uses: docker/build-push-action@v6
with:
context: .
file: apps/cloud/Dockerfile
platforms: linux/amd64
# Load to the local daemon — `wrangler containers push` reads from
# the local daemon and re-pushes with Cloudflare-issued temporary
# registry credentials (the registry rejects plain docker login).
load: true
tags: registry.cloudflare.com/${{ env.ACCOUNT_ID }}/${{ env.IMAGE_NAME }}:${{ env.SHA8 }}
# GitHub Actions cache — drops cloud rebuild from ~20m to ~3-6m
# after the first run.
cache-from: type=gha,scope=cloud
cache-to: type=gha,scope=cloud,mode=max
- name: Push image via wrangler (handles CF registry auth)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ env.ACCOUNT_ID }}
run: |
npx --yes wrangler@4 containers push \
"registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}"
- name: Pin image tag in wrangler.toml
run: |
set -euo pipefail
NEW="registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}"
sed -i -E "s#^image = .*${IMAGE_NAME}:.*#image = \"${NEW}\"#" apps/cloud/wrangler.toml
echo "Pinned to: $NEW"
grep '^image' apps/cloud/wrangler.toml
- name: Wrangler deploy (cloud)
working-directory: apps/cloud
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ env.ACCOUNT_ID }}
run: npx --yes wrangler@4 deploy --config wrangler.toml
- name: Commit pinned tag back to main
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
run: |
set -euo pipefail
if git diff --quiet apps/cloud/wrangler.toml; then
echo "No tag change to commit."
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -B deploy/cloud-${SHA8}
git add apps/cloud/wrangler.toml
git commit -m "chore(deploy): pin cloud image tag to ${SHA8}
Auto-bumped by .github/workflows/deploy.yml after successful Cloudflare
Containers deploy of registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>"
# Fast-forward push to main if possible; otherwise leave the branch
# for a human to PR. We never force-push main.
git fetch origin main
if git merge-base --is-ancestor origin/main HEAD; then
git push origin HEAD:main
else
git push origin HEAD
echo "::warning::Could not fast-forward main; pushed branch deploy/cloud-${SHA8} instead. Open a PR."
fi
# ── Build + push objectos image; pin tag; wrangler deploy ──
objectos:
needs: plan
if: needs.plan.outputs.deploy_objectos == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
env:
ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
IMAGE_NAME: objectos
SHA8: ${{ needs.plan.outputs.sha8 }}
steps:
- uses: actions/checkout@v5
with:
ref: ${{ needs.plan.outputs.sha }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up pnpm
uses: pnpm/action-setup@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- name: Install workspace deps (for wrangler bundle)
run: pnpm install --frozen-lockfile --prefer-offline
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build objectos image (local)
uses: docker/build-push-action@v6
with:
context: .
file: apps/objectos/Dockerfile
platforms: linux/amd64
load: true
tags: registry.cloudflare.com/${{ env.ACCOUNT_ID }}/${{ env.IMAGE_NAME }}:${{ env.SHA8 }}
cache-from: type=gha,scope=objectos
cache-to: type=gha,scope=objectos,mode=max
- name: Push image via wrangler (handles CF registry auth)
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ env.ACCOUNT_ID }}
run: |
npx --yes wrangler@4 containers push \
"registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}"
- name: Pin image tag in wrangler.toml
run: |
set -euo pipefail
NEW="registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}"
sed -i -E "s#^image = .*${IMAGE_NAME}:.*#image = \"${NEW}\"#" apps/objectos/wrangler.toml
echo "Pinned to: $NEW"
grep '^image' apps/objectos/wrangler.toml
- name: Wrangler deploy (objectos)
working-directory: apps/objectos
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ env.ACCOUNT_ID }}
run: npx --yes wrangler@4 deploy --config wrangler.toml
- name: Commit pinned tag back to main
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
run: |
set -euo pipefail
if git diff --quiet apps/objectos/wrangler.toml; then
echo "No tag change to commit."
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -B deploy/objectos-${SHA8}
git add apps/objectos/wrangler.toml
git commit -m "chore(deploy): pin objectos image tag to ${SHA8}
Auto-bumped by .github/workflows/deploy.yml after successful Cloudflare
Containers deploy of registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>"
git fetch origin main
if git merge-base --is-ancestor origin/main HEAD; then
git push origin HEAD:main
else
git push origin HEAD
echo "::warning::Could not fast-forward main; pushed branch deploy/objectos-${SHA8} instead. Open a PR."
fi