name: Cloudflare Workers (Preview) Deploy on: workflow_run: workflows: - Cloudflare Workers (Preview) Prepare types: - completed jobs: ask-for-approval: name: Ask for approval to deploy - ${{ matrix.app_name }} runs-on: ubuntu-latest permissions: pull-requests: write contents: read strategy: matrix: include: - app_name: stage-web steps: - name: Download artifact - metadata uses: dawidd6/action-download-artifact@v19 with: workflow_conclusion: success run_id: ${{ github.event.workflow_run.id }} name: preview-meta path: preview-meta allow_forks: true - name: Parse metadata id: meta run: | node --input-type=module <<'NODE' import { appendFile, readFile } from 'node:fs/promises' const metadata = JSON.parse(await readFile('preview-meta/preview-meta.json', 'utf8')) if ( !/^\d+$/.test(metadata.prNumber) || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(metadata.repositoryFullName) || !/^[a-f0-9]{40}$/i.test(metadata.headSha) ) { throw new Error('Preview metadata has an invalid shape') } await appendFile(process.env.GITHUB_OUTPUT, [ `PR_NUM=${metadata.prNumber}`, `REPO_FULL_NAME=${metadata.repositoryFullName}`, `HEAD_SHA=${metadata.headSha}`, '', ].join('\n')) NODE - name: Find Comment if: ${{ always() }} uses: peter-evans/find-comment@v4 id: fc with: issue-number: ${{ steps.meta.outputs.PR_NUM }} comment-author: 'github-actions[bot]' body-includes: to Cloudflare Workers (Preview) for *${{ matrix.app_name }}* - name: Comment on require approval uses: peter-evans/create-or-update-comment@v5 with: comment-id: ${{ steps.fc.outputs.comment-id }} issue-number: ${{ steps.meta.outputs.PR_NUM }} edit-mode: replace body: | ## ⏳ Approval required for deploying to Cloudflare Workers (Preview) for *${{ matrix.app_name }}*. | Name | Link | |:------------------------|:---------------------------------------------------------------------------------------| | 🔭 Waiting for approval | For maintainers, approve [here](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) Hey, maintainers, kindly take some time to review and approve this deployment when you are available. Thank you! 🙏 on-success: if: ${{ github.event.workflow_run.conclusion == 'success' }} name: Deploy - ${{ matrix.app_name }} runs-on: ubuntu-latest environment: Preview permissions: contents: read pull-requests: write strategy: matrix: include: - app_name: stage-web wrangler_config_path: ./apps/stage-web/wrangler.toml build_directory: ./apps/stage-web/dist build_command: | pnpm -F @proj-airi/stage-web run build pnpm -F @proj-airi/docs run build:base mv ./docs/.vitepress/dist ./apps/stage-web/dist/docs cp ./apps/stage-web/dist/docs/sitemap.xml ./apps/stage-web/dist/sitemap.xml pnpm -F @proj-airi/stage-ui run story:build mv ./packages/stage-ui/.histoire/dist ./apps/stage-web/dist/ui steps: - name: Download artifact - metadata uses: dawidd6/action-download-artifact@v19 with: workflow_conclusion: success run_id: ${{ github.event.workflow_run.id }} name: preview-meta path: preview-meta allow_forks: true - name: Parse metadata id: meta run: | node --input-type=module <<'NODE' import { appendFile, readFile } from 'node:fs/promises' const metadata = JSON.parse(await readFile('preview-meta/preview-meta.json', 'utf8')) if ( !/^\d+$/.test(metadata.prNumber) || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(metadata.repositoryFullName) || !/^[a-f0-9]{40}$/i.test(metadata.headSha) ) { throw new Error('Preview metadata has an invalid shape') } await appendFile(process.env.GITHUB_OUTPUT, [ `PR_NUM=${metadata.prNumber}`, `REPO_FULL_NAME=${metadata.repositoryFullName}`, `HEAD_SHA=${metadata.headSha}`, '', ].join('\n')) NODE - name: Checkout repository uses: actions/checkout@v6 with: repository: ${{ steps.meta.outputs.REPO_FULL_NAME }} ref: ${{ steps.meta.outputs.HEAD_SHA }} fetch-depth: 1 persist-credentials: false # https://gh.io/securely-using-pull_request_target allow-unsafe-pr-checkout: true - name: Setup pnpm uses: pnpm/action-setup@v4 - name: Setup Node.js uses: actions/setup-node@v6 with: node-version: lts/* cache: pnpm # NOTICE: # # Here installing wrangler to global is required, or otherwise: # ERR_PNPM_ADDING_TO_ROOT Running this command will add the dependency to the workspace root... # error occurs. # # Since https://github.com/cloudflare/wrangler-action/pull/339#issuecomment-2667622947 rejected the -g support # by saying un-reasonable 'I'm not sure if it's common ... to install packages to the global scope, ... might be introducing some unintended side effects.' # # Clearly I think installing with ` install` brings more unintended side effects... # # As suggested by https://github.com/cloudflare/wrangler-action/issues/181#issuecomment-2127990708, we should pre-install # with our package manager and then use it in the action. - run: pnpm i -g wrangler@4 - name: Install dependencies run: pnpm install --frozen-lockfile - name: Build packages run: pnpm run build:packages - name: Build ${{ matrix.app_name }} id: build run: ${{ matrix.build_command }} continue-on-error: true env: S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }} S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }} S3_ENDPOINT: ${{ secrets.S3_ENDPOINT }} S3_REGION: ${{ secrets.S3_REGION }} WARP_DRIVE_PUBLIC_BASE: ${{ secrets.WARP_DRIVE_PUBLIC_BASE }} STAGE_WEB_WARP_DRIVE_PREFIX: proj-airi/stage-web/pr-${{ steps.meta.outputs.PR_NUM }}/ STAGE_UI_WARP_DRIVE_PREFIX: proj-airi/stage-ui/pr-${{ steps.meta.outputs.PR_NUM }}/ - name: Find Comment if: ${{ always() }} uses: peter-evans/find-comment@v4 id: fc with: issue-number: ${{ steps.meta.outputs.PR_NUM }} comment-author: 'github-actions[bot]' body-includes: to Cloudflare Workers (Preview) for *${{ matrix.app_name }}* - name: Comment on build failure if: ${{ steps.build.outcome != 'success' }} uses: peter-evans/create-or-update-comment@v5 with: comment-id: ${{ steps.fc.outputs.comment-id }} issue-number: ${{ steps.meta.outputs.PR_NUM }} edit-mode: replace body: | ## ❌ Deploy to Cloudflare Workers (Preview) for *${{ matrix.app_name }}* failed. | Name | Link | |:---------------------|:---------------------------------------------------------------------------------------| | 🔍 Latest deploy log | https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | - name: Fail if build failed if: ${{ steps.build.outcome != 'success' }} run: | echo "Build step failed; marking workflow as failed." exit 1 - name: Wrangler Upload if: ${{ steps.build.outcome == 'success' }} id: wrangler-versions-upload uses: cloudflare/wrangler-action@v3.14.1 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} command: versions upload -c ${{ matrix.wrangler_config_path }} --preview-alias pr-${{ steps.meta.outputs.PR_NUM }} --message "GitHub Actions uploaded preview for Pull Request ${{ steps.meta.outputs.PR_NUM }}" --tag v0.0.1-pr.${{ steps.meta.outputs.PR_NUM }} gitHubToken: ${{ secrets.GITHUB_TOKEN }} - name: Prepare preview URLs if: ${{ steps.build.outcome == 'success' }} id: preview-urls run: | deployment_url="${{ steps.wrangler-versions-upload.outputs.deployment-url }}" if [ -z "$deployment_url" ]; then echo "Deployment URL from wrangler upload is empty." >&2 exit 1 fi host_without_scheme="${deployment_url#https://}" host_without_scheme="${host_without_scheme#http://}" preview_alias_url="https://pr-${{ steps.meta.outputs.PR_NUM }}-${host_without_scheme#*-}" echo "deploy_preview_url=$deployment_url" >> "$GITHUB_OUTPUT" echo "pull_request_preview_url=$preview_alias_url" >> "$GITHUB_OUTPUT" - name: Create or update comment if: ${{ steps.build.outcome == 'success' }} uses: peter-evans/create-or-update-comment@v5 with: comment-id: ${{ steps.fc.outputs.comment-id }} issue-number: ${{ steps.meta.outputs.PR_NUM }} edit-mode: replace body: | ## ✅ Deploy to Cloudflare Workers (Preview) for *${{ matrix.app_name }}* ready! | Name | Link | |:------------------------|:--------------------------------------------------------------------------------------------| | 🔍 Latest deploy log | https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} | | 😎 Deploy Preview | ${{ steps.preview-urls.outputs.deploy_preview_url }} | | 🚀 Pull Request Preview | ${{ steps.preview-urls.outputs.pull_request_preview_url }} |