fix: align workflows to v* tags and support manual release deploys #345
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Cloudflare Worker Preview Deploy | |
| on: | |
| pull_request: | |
| paths: | |
| - 'src/**' | |
| - 'index.html' | |
| - 'package.json' | |
| - 'package-lock.json' | |
| - 'vite.config.ts' | |
| - 'tsconfig.json' | |
| - '.github/workflows/cloudflare-web-preview.yml' | |
| - '.github/actions/setup/**' | |
| push: | |
| branches: | |
| - dev | |
| paths: | |
| - 'src/**' | |
| - 'index.html' | |
| - 'package.json' | |
| - 'package-lock.json' | |
| - 'vite.config.ts' | |
| - 'tsconfig.json' | |
| - '.github/workflows/cloudflare-web-preview.yml' | |
| - '.github/actions/setup/**' | |
| concurrency: | |
| group: cloudflare-worker-preview-${{ github.event.pull_request.number || github.ref_name }} | |
| cancel-in-progress: true | |
| jobs: | |
| deploy: | |
| if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push' | |
| 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<<EOF' | |
| echo "$preview_message" | |
| echo EOF | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Setup app and build | |
| uses: ./.github/actions/setup | |
| with: | |
| build: 'true' | |
| - name: Set deploy alias | |
| id: alias | |
| shell: bash | |
| run: | | |
| if [ "${{ github.event_name }}" = "pull_request" ]; then | |
| echo "alias=pr-${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT" | |
| else | |
| branch="${GITHUB_REF_NAME}" | |
| alias="$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g')" | |
| echo "alias=${alias}" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Upload Worker preview | |
| id: deploy | |
| uses: cloudflare/wrangler-action@da0e0dfe58b7a431659754fdf3f186c529afbe65 # v3.14.1 | |
| env: | |
| PREVIEW_MESSAGE: ${{ steps.metadata.outputs.preview_message }} | |
| with: | |
| apiToken: ${{ secrets.TF_CLOUDFLARE_API_TOKEN }} | |
| accountId: ${{ secrets.TF_VAR_ACCOUNT_ID }} | |
| command: > | |
| versions upload | |
| -c dist/wrangler.json | |
| --preview-alias ${{ steps.alias.outputs.alias }} | |
| --message "$PREVIEW_MESSAGE" | |
| - name: Resolve preview URL | |
| id: preview | |
| env: | |
| DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }} | |
| COMMAND_OUTPUT: ${{ steps.deploy.outputs.command-output }} | |
| ALIAS: ${{ steps.alias.outputs.alias }} | |
| shell: bash | |
| run: | | |
| alias_url_pattern="^https?://${ALIAS}-[^[:space:]]+$" | |
| preview_url="" | |
| 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_url=${preview_url}" >> "$GITHUB_OUTPUT" | |
| - name: Publish summary and PR comment | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| env: | |
| MARKER: '<!-- cloudflare-worker-preview -->' | |
| PREVIEW_URL: ${{ steps.preview.outputs.preview_url }} | |
| PREVIEW_ALIAS: ${{ steps.alias.outputs.alias }} | |
| SHORT_SHA: ${{ github.event.pull_request.head.sha || github.sha }} | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const marker = process.env.MARKER; | |
| const previewUrl = process.env.PREVIEW_URL; | |
| const previewAlias = process.env.PREVIEW_ALIAS; | |
| const shortSha = process.env.SHORT_SHA?.slice(0, 7); | |
| const now = new Date().toUTCString().replace(':00 GMT', ' UTC'); | |
| if (!previewUrl) { | |
| core.setFailed("Missing preview URL from Cloudflare deploy step."); | |
| return; | |
| } | |
| const tableRow = "| ✅ Deployment successful! | <a href='" + previewUrl + "'>" + previewUrl + "</a> | " + shortSha + " | `" + previewAlias + "` | " + now + " |"; | |
| const comment = [ | |
| marker, | |
| `## Deploying with <a href="https://workers.dev"><img alt="Cloudflare Workers" src="https://workers.cloudflare.com/logo.svg" width="16"></a> Cloudflare Workers`, | |
| ``, | |
| `| Status | Preview URL | Commit | Alias | Updated (UTC) |`, | |
| `| - | - | - | - | - |`, | |
| tableRow, | |
| ].join("\n"); | |
| await core.summary.addRaw(comment.replace(marker + "\n", "")).write(); | |
| if (context.eventName !== 'pull_request') return; | |
| 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( | |
| (c) => c.user.type === "Bot" && c.body.includes(marker), | |
| ); | |
| if (existing) { | |
| await github.rest.issues.deleteComment({ | |
| owner, repo, comment_id: existing.id, | |
| }); | |
| } | |
| await github.rest.issues.createComment({ owner, repo, issue_number, body: comment }); |