|
| 1 | +name: Deploy to Cloudflare Containers |
| 2 | + |
| 3 | +# ───────────────────────────────────────────────────────────────────────── |
| 4 | +# Build + push Docker images for cloud (cloud.objectos.app) and/or |
| 5 | +# objectos (single-project runtime per env), then `wrangler deploy` the |
| 6 | +# matching Worker so the new image is pinned. |
| 7 | +# |
| 8 | +# Triggers: |
| 9 | +# 1. Manual: Actions → "Deploy to Cloudflare Containers" → Run |
| 10 | +# (choose `target` = cloud | objectos | both, and optionally |
| 11 | +# the commit SHA to deploy; defaults to the workflow ref). |
| 12 | +# 2. Tag: push a tag matching `deploy-cloud-*`, `deploy-objectos-*`, |
| 13 | +# or `deploy-all-*`. The tag suffix is informational; the |
| 14 | +# image tag = the resolved commit SHA (short, 8 chars) so a |
| 15 | +# redeploy of the same SHA is idempotent. |
| 16 | +# |
| 17 | +# Image naming: |
| 18 | +# registry.cloudflare.com/<ACCOUNT_ID>/objectstack-cloud:<sha8> |
| 19 | +# registry.cloudflare.com/<ACCOUNT_ID>/objectos:<sha8> |
| 20 | +# |
| 21 | +# The corresponding `apps/<target>/wrangler.toml` is rewritten in-place to |
| 22 | +# pin `image = "<registry>:<sha8>"` and committed back to `main` so the |
| 23 | +# repo always tracks what is actually deployed. |
| 24 | +# |
| 25 | +# Required secrets (Repository → Settings → Secrets and variables → Actions): |
| 26 | +# CLOUDFLARE_API_TOKEN — token with Containers:Edit + Workers:Edit perms |
| 27 | +# CLOUDFLARE_ACCOUNT_ID — 2846eb40a60f4738e292b90dcd8cce10 |
| 28 | +# ───────────────────────────────────────────────────────────────────────── |
| 29 | + |
| 30 | +on: |
| 31 | + workflow_dispatch: |
| 32 | + inputs: |
| 33 | + target: |
| 34 | + description: 'Which app(s) to deploy' |
| 35 | + required: true |
| 36 | + default: 'both' |
| 37 | + type: choice |
| 38 | + options: |
| 39 | + - cloud |
| 40 | + - objectos |
| 41 | + - both |
| 42 | + ref: |
| 43 | + description: 'Commit SHA or branch to deploy (default: workflow ref)' |
| 44 | + required: false |
| 45 | + default: '' |
| 46 | + push: |
| 47 | + tags: |
| 48 | + - 'deploy-cloud-*' |
| 49 | + - 'deploy-objectos-*' |
| 50 | + - 'deploy-all-*' |
| 51 | + |
| 52 | +concurrency: |
| 53 | + # Serialize deploys per-target so two pushes don't race on `wrangler deploy`. |
| 54 | + group: deploy-${{ github.event.inputs.target || github.ref_name }} |
| 55 | + cancel-in-progress: false |
| 56 | + |
| 57 | +jobs: |
| 58 | + # ── Resolve which targets to build based on the trigger ── |
| 59 | + plan: |
| 60 | + runs-on: ubuntu-latest |
| 61 | + outputs: |
| 62 | + deploy_cloud: ${{ steps.plan.outputs.deploy_cloud }} |
| 63 | + deploy_objectos: ${{ steps.plan.outputs.deploy_objectos }} |
| 64 | + sha: ${{ steps.plan.outputs.sha }} |
| 65 | + sha8: ${{ steps.plan.outputs.sha8 }} |
| 66 | + steps: |
| 67 | + - name: Checkout (resolve ref) |
| 68 | + uses: actions/checkout@v5 |
| 69 | + with: |
| 70 | + ref: ${{ github.event.inputs.ref || github.ref }} |
| 71 | + fetch-depth: 1 |
| 72 | + |
| 73 | + - id: plan |
| 74 | + name: Plan targets + resolve SHA |
| 75 | + run: | |
| 76 | + set -euo pipefail |
| 77 | + SHA="$(git rev-parse HEAD)" |
| 78 | + SHA8="$(git rev-parse --short=8 HEAD)" |
| 79 | + echo "sha=$SHA" >> "$GITHUB_OUTPUT" |
| 80 | + echo "sha8=$SHA8" >> "$GITHUB_OUTPUT" |
| 81 | +
|
| 82 | + DEPLOY_CLOUD=false |
| 83 | + DEPLOY_OBJECTOS=false |
| 84 | + case "${{ github.event_name }}" in |
| 85 | + workflow_dispatch) |
| 86 | + T="${{ github.event.inputs.target }}" |
| 87 | + [[ "$T" == "cloud" || "$T" == "both" ]] && DEPLOY_CLOUD=true |
| 88 | + [[ "$T" == "objectos" || "$T" == "both" ]] && DEPLOY_OBJECTOS=true |
| 89 | + ;; |
| 90 | + push) |
| 91 | + REF="${{ github.ref_name }}" |
| 92 | + [[ "$REF" == deploy-cloud-* || "$REF" == deploy-all-* ]] && DEPLOY_CLOUD=true |
| 93 | + [[ "$REF" == deploy-objectos-* || "$REF" == deploy-all-* ]] && DEPLOY_OBJECTOS=true |
| 94 | + ;; |
| 95 | + esac |
| 96 | +
|
| 97 | + echo "deploy_cloud=$DEPLOY_CLOUD" >> "$GITHUB_OUTPUT" |
| 98 | + echo "deploy_objectos=$DEPLOY_OBJECTOS" >> "$GITHUB_OUTPUT" |
| 99 | + echo "─────────────────────────────────" |
| 100 | + echo "Trigger: ${{ github.event_name }}" |
| 101 | + echo "SHA: $SHA" |
| 102 | + echo "Image tag: $SHA8" |
| 103 | + echo "Cloud: $DEPLOY_CLOUD" |
| 104 | + echo "ObjectOS: $DEPLOY_OBJECTOS" |
| 105 | +
|
| 106 | + # ── Build + push cloud image; pin tag in wrangler.toml; wrangler deploy ── |
| 107 | + cloud: |
| 108 | + needs: plan |
| 109 | + if: needs.plan.outputs.deploy_cloud == 'true' |
| 110 | + runs-on: ubuntu-latest |
| 111 | + permissions: |
| 112 | + contents: write # to commit the wrangler.toml tag bump back to main |
| 113 | + env: |
| 114 | + ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 115 | + IMAGE_NAME: objectstack-cloud |
| 116 | + SHA8: ${{ needs.plan.outputs.sha8 }} |
| 117 | + steps: |
| 118 | + - uses: actions/checkout@v5 |
| 119 | + with: |
| 120 | + ref: ${{ needs.plan.outputs.sha }} |
| 121 | + # Need full history so we can push back the tag-bump commit. |
| 122 | + fetch-depth: 0 |
| 123 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + |
| 125 | + - name: Set up Docker Buildx |
| 126 | + uses: docker/setup-buildx-action@v3 |
| 127 | + |
| 128 | + - name: Log in to Cloudflare container registry |
| 129 | + uses: docker/login-action@v3 |
| 130 | + with: |
| 131 | + registry: registry.cloudflare.com |
| 132 | + username: ${{ env.ACCOUNT_ID }} |
| 133 | + password: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 134 | + |
| 135 | + - name: Build + push cloud image |
| 136 | + uses: docker/build-push-action@v6 |
| 137 | + with: |
| 138 | + context: . |
| 139 | + file: apps/cloud/Dockerfile |
| 140 | + platforms: linux/amd64 |
| 141 | + push: true |
| 142 | + tags: registry.cloudflare.com/${{ env.ACCOUNT_ID }}/${{ env.IMAGE_NAME }}:${{ env.SHA8 }} |
| 143 | + # GitHub Actions cache — drops cloud rebuild from ~20m to ~3-6m |
| 144 | + # after the first run. |
| 145 | + cache-from: type=gha,scope=cloud |
| 146 | + cache-to: type=gha,scope=cloud,mode=max |
| 147 | + |
| 148 | + - name: Pin image tag in wrangler.toml |
| 149 | + run: | |
| 150 | + set -euo pipefail |
| 151 | + NEW="registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}" |
| 152 | + sed -i -E "s#^image = .*${IMAGE_NAME}:.*#image = \"${NEW}\"#" apps/cloud/wrangler.toml |
| 153 | + echo "Pinned to: $NEW" |
| 154 | + grep '^image' apps/cloud/wrangler.toml |
| 155 | +
|
| 156 | + - name: Wrangler deploy (cloud) |
| 157 | + uses: cloudflare/wrangler-action@v3 |
| 158 | + with: |
| 159 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 160 | + accountId: ${{ env.ACCOUNT_ID }} |
| 161 | + workingDirectory: apps/cloud |
| 162 | + command: deploy --config wrangler.toml |
| 163 | + |
| 164 | + - name: Commit pinned tag back to main |
| 165 | + if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/') |
| 166 | + run: | |
| 167 | + set -euo pipefail |
| 168 | + if git diff --quiet apps/cloud/wrangler.toml; then |
| 169 | + echo "No tag change to commit." |
| 170 | + exit 0 |
| 171 | + fi |
| 172 | + git config user.name "github-actions[bot]" |
| 173 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 174 | + git checkout -B deploy/cloud-${SHA8} |
| 175 | + git add apps/cloud/wrangler.toml |
| 176 | + git commit -m "chore(deploy): pin cloud image tag to ${SHA8} |
| 177 | +
|
| 178 | + Auto-bumped by .github/workflows/deploy.yml after successful Cloudflare |
| 179 | + Containers deploy of registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}. |
| 180 | +
|
| 181 | + Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>" |
| 182 | + # Fast-forward push to main if possible; otherwise leave the branch |
| 183 | + # for a human to PR. We never force-push main. |
| 184 | + git fetch origin main |
| 185 | + if git merge-base --is-ancestor origin/main HEAD; then |
| 186 | + git push origin HEAD:main |
| 187 | + else |
| 188 | + git push origin HEAD |
| 189 | + echo "::warning::Could not fast-forward main; pushed branch deploy/cloud-${SHA8} instead. Open a PR." |
| 190 | + fi |
| 191 | +
|
| 192 | + # ── Build + push objectos image; pin tag; wrangler deploy ── |
| 193 | + objectos: |
| 194 | + needs: plan |
| 195 | + if: needs.plan.outputs.deploy_objectos == 'true' |
| 196 | + runs-on: ubuntu-latest |
| 197 | + permissions: |
| 198 | + contents: write |
| 199 | + env: |
| 200 | + ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 201 | + IMAGE_NAME: objectos |
| 202 | + SHA8: ${{ needs.plan.outputs.sha8 }} |
| 203 | + steps: |
| 204 | + - uses: actions/checkout@v5 |
| 205 | + with: |
| 206 | + ref: ${{ needs.plan.outputs.sha }} |
| 207 | + fetch-depth: 0 |
| 208 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 209 | + |
| 210 | + - name: Set up Docker Buildx |
| 211 | + uses: docker/setup-buildx-action@v3 |
| 212 | + |
| 213 | + - name: Log in to Cloudflare container registry |
| 214 | + uses: docker/login-action@v3 |
| 215 | + with: |
| 216 | + registry: registry.cloudflare.com |
| 217 | + username: ${{ env.ACCOUNT_ID }} |
| 218 | + password: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 219 | + |
| 220 | + - name: Build + push objectos image |
| 221 | + uses: docker/build-push-action@v6 |
| 222 | + with: |
| 223 | + context: . |
| 224 | + file: apps/objectos/Dockerfile |
| 225 | + platforms: linux/amd64 |
| 226 | + push: true |
| 227 | + tags: registry.cloudflare.com/${{ env.ACCOUNT_ID }}/${{ env.IMAGE_NAME }}:${{ env.SHA8 }} |
| 228 | + cache-from: type=gha,scope=objectos |
| 229 | + cache-to: type=gha,scope=objectos,mode=max |
| 230 | + |
| 231 | + - name: Pin image tag in wrangler.toml |
| 232 | + run: | |
| 233 | + set -euo pipefail |
| 234 | + NEW="registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}" |
| 235 | + sed -i -E "s#^image = .*${IMAGE_NAME}:.*#image = \"${NEW}\"#" apps/objectos/wrangler.toml |
| 236 | + echo "Pinned to: $NEW" |
| 237 | + grep '^image' apps/objectos/wrangler.toml |
| 238 | +
|
| 239 | + - name: Wrangler deploy (objectos) |
| 240 | + uses: cloudflare/wrangler-action@v3 |
| 241 | + with: |
| 242 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 243 | + accountId: ${{ env.ACCOUNT_ID }} |
| 244 | + workingDirectory: apps/objectos |
| 245 | + command: deploy --config wrangler.toml |
| 246 | + |
| 247 | + - name: Commit pinned tag back to main |
| 248 | + if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/') |
| 249 | + run: | |
| 250 | + set -euo pipefail |
| 251 | + if git diff --quiet apps/objectos/wrangler.toml; then |
| 252 | + echo "No tag change to commit." |
| 253 | + exit 0 |
| 254 | + fi |
| 255 | + git config user.name "github-actions[bot]" |
| 256 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 257 | + git checkout -B deploy/objectos-${SHA8} |
| 258 | + git add apps/objectos/wrangler.toml |
| 259 | + git commit -m "chore(deploy): pin objectos image tag to ${SHA8} |
| 260 | +
|
| 261 | + Auto-bumped by .github/workflows/deploy.yml after successful Cloudflare |
| 262 | + Containers deploy of registry.cloudflare.com/${ACCOUNT_ID}/${IMAGE_NAME}:${SHA8}. |
| 263 | +
|
| 264 | + Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>" |
| 265 | + git fetch origin main |
| 266 | + if git merge-base --is-ancestor origin/main HEAD; then |
| 267 | + git push origin HEAD:main |
| 268 | + else |
| 269 | + git push origin HEAD |
| 270 | + echo "::warning::Could not fast-forward main; pushed branch deploy/objectos-${SHA8} instead. Open a PR." |
| 271 | + fi |
0 commit comments