diff --git a/.github/workflows/cloudflare-worker-preview.yml b/.github/workflows/cloudflare-worker-preview.yml deleted file mode 100644 index 4f1b941b..00000000 --- a/.github/workflows/cloudflare-worker-preview.yml +++ /dev/null @@ -1,143 +0,0 @@ -name: Cloudflare Worker Preview Deploy - -on: - pull_request: - -concurrency: - group: cloudflare-worker-preview-${{ github.event.pull_request.number }} - cancel-in-progress: true - -jobs: - deploy: - if: github.event.pull_request.head.repo.full_name == github.repository - runs-on: ubuntu-latest - permissions: - contents: read - pull-requests: write - steps: - - name: Checkout repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - with: - persist-credentials: false - - - name: Prepare preview metadata - id: metadata - shell: bash - run: | - preview_message="$(git log -1 --pretty=%s)" - preview_message="$(printf '%s' "$preview_message" | head -c 100)" - - { - echo 'preview_message<> "$GITHUB_OUTPUT" - - - name: Setup node - uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0 - with: - node-version-file: '.node-version' - package-manager-cache: false - - - name: Install dependencies - run: npm ci - - - name: Build app - run: npm run build - - - name: Upload Worker preview - id: deploy - uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65 # v3.14.1 - with: - apiToken: ${{ secrets.TF_CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.TF_VAR_ACCOUNT_ID }} - command: > - versions upload - -c dist/wrangler.json - --preview-alias pr-${{ github.event.pull_request.number }} - --message "${{ steps.metadata.outputs.preview_message }}" - - - name: Resolve preview URL - id: preview - env: - DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }} - COMMAND_OUTPUT: ${{ steps.deploy.outputs.command-output }} - PR_NUMBER: ${{ github.event.pull_request.number }} - shell: bash - run: | - alias="pr-${PR_NUMBER}" - preview_url="" - alias_url_pattern="^https?://${alias}-[^[:space:]]+$" - - if printf '%s\n' "$DEPLOYMENT_URL" | grep -Eq "$alias_url_pattern"; then - preview_url="$DEPLOYMENT_URL" - else - preview_url="$(printf '%s\n' "$COMMAND_OUTPUT" | grep -Eo "https?://${alias}-[^[:space:]\"'<>)]+" | head -n 1 || true)" - fi - - if ! printf '%s\n' "$preview_url" | grep -Eq "$alias_url_pattern"; then - echo "Failed to resolve aliased Worker preview URL." >&2 - exit 1 - fi - - { - echo "preview_alias=${alias}" - echo "preview_url=${preview_url}" - } >> "$GITHUB_OUTPUT" - - - name: Write deployment summary - env: - PREVIEW_ALIAS: ${{ steps.preview.outputs.preview_alias }} - PREVIEW_URL: ${{ steps.preview.outputs.preview_url }} - shell: bash - run: | - { - echo "## Cloudflare Worker Preview" - echo - echo "- Alias: \`${PREVIEW_ALIAS}\`" - echo "- Preview URL: ${PREVIEW_URL}" - } >> "$GITHUB_STEP_SUMMARY" - - - name: Publish preview URL - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 - env: - MARKER: '' - PREVIEW_URL: ${{ steps.preview.outputs.preview_url }} - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const marker = process.env.MARKER; - const previewUrl = process.env.PREVIEW_URL; - if (!previewUrl) { - core.setFailed("Missing preview URL from Cloudflare deploy step."); - return; - } - - const body = `${marker}\nWorker preview: ${previewUrl}`; - const { owner, repo } = context.repo; - const issue_number = context.issue.number; - const comments = await github.paginate(github.rest.issues.listComments, { - owner, - repo, - issue_number, - }); - const existing = comments.find( - (comment) => comment.user.type === "Bot" && comment.body.includes(marker), - ); - - if (existing) { - await github.rest.issues.updateComment({ - owner, - repo, - comment_id: existing.id, - body, - }); - return; - } - - await github.rest.issues.createComment({ - owner, - repo, - issue_number, - body, - }); diff --git a/infra/README.md b/infra/README.md index 0617d377..ad003c0b 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,9 +1,7 @@ # Infrastructure -`infra/web` manages the Cloudflare Worker service, immutable Worker version uploads, -the live production deployment, and the Worker custom domain. Local setup uses -`terraform.tfvars.example` for shared inputs and `gitlab.http.tfbackend.example` for -the GitLab state backend config. +`infra/web` manages the Cloudflare Worker, immutable Worker versions, the live +production deployment, and the production custom domain. Prerequisites: @@ -84,28 +82,23 @@ If you already created local state before switching to GitLab state, use `tofu -chdir=infra/web init -reconfigure -migrate-state -backend-config="../gitlab.http.tfbackend"` once instead. -Preview deploys: +Preview builds: -- PR previews use Wrangler `versions upload` against the generated - `dist/wrangler.json`. -- That Wrangler-based preview path is temporary until the Cloudflare provider adds support for preview version/alias management. -- `npm run build` generates both `dist/` and `dist/wrangler.json`; Wrangler uploads a - preview Worker version and binds a stable alias such as `pr-123`. -- GitHub Actions also sets the preview version message from the current git commit - subject, truncated to 100 bytes to fit Cloudflare's limit. -- Preview URLs live on `workers.dev` and do not touch the live custom domain. +- `infra/web/main.tf` enables preview URL capability with `subdomain.previews_enabled = true`. +- Previews are handled by Cloudflare Workers Builds, not GitHub Actions. +- Connect the repo once in Cloudflare Workers Builds. +- Set the Cloudflare Builds deploy command to `npx wrangler versions upload`. +- This disables automatic deployments while still allowing Cloudflare to build PRs/branches and save them as preview versions. +- That keeps Cloudflare from promoting `dev` commits to production. Production stays on the OpenTofu/GitHub Actions path in this repo. + +```bash +npx wrangler versions upload +``` Production deploys: -- PRs that change `infra/web` get an OpenTofu plan comment on the PR from - `.github/workflows/cloudflare-worker-prod.yml`. -- `.github/workflows/cloudflare-worker-prod.yml` builds the app and runs `tofu apply` on - pushes to `dev`, or via manual workflow dispatch. -- `tofu apply` uploads `dist/` as a new immutable Worker version with - `cloudflare_worker_version`, then `cloudflare_workers_deployment` moves live traffic - to that exact version. -- GitHub Actions sets the production deployment message from the current git commit - subject, truncated to 100 bytes to fit Cloudflare's limit. -- `app.sable.moe` is the Worker custom domain for production. -- `cloudflare_workers_custom_domain` attaches the Worker to `app.sable.moe`, and - Cloudflare creates the backing DNS record automatically. +- `.github/workflows/cloudflare-worker-prod.yml` comments PR plans for `infra/web` changes. +- That PR plan job only runs for same-repo PRs, not fork PRs, because it needs repo secrets. +- The same workflow applies production on pushes to `dev` or manual dispatch. +- `tofu apply` uploads `dist/` through `cloudflare_worker_version` and promotes it with `cloudflare_workers_deployment`. +- Production lives on `app.sable.moe`.