From 5f5afff8fa1567944d9a78bad13478438147b2b5 Mon Sep 17 00:00:00 2001 From: "yanqi.zong" Date: Sun, 25 May 2025 19:45:15 -0700 Subject: [PATCH 1/2] feat: update release workflow --- .github/workflows/prerelease.yml | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml index 9d8a05fa..e86c592a 100644 --- a/.github/workflows/prerelease.yml +++ b/.github/workflows/prerelease.yml @@ -14,7 +14,6 @@ permissions: jobs: deploy: - if: contains(github.event.pull_request.labels.*.name, 'prerelease') runs-on: ubuntu-latest strategy: matrix: @@ -27,13 +26,41 @@ jobs: secret_project_id: VERCEL_PROJECT_ZH_HANT_ID name: Deploy ${{ matrix.locale }} steps: + # Calculate if this matrix job should run based on PR labels + # + # This job will deploy if: + # - The event is NOT a pull_request (e.g., workflow_dispatch) + # - OR the PR has the label 'prerelease' + # - OR the PR has the label 'prerelease:' matching the current matrix locale + - name: Calculate should_deploy + id: should_deploy + shell: bash + run: | + LABELS_JSON='${{ toJson(github.event.pull_request.labels.*.name) }}' + echo "LABELS_JSON: $LABELS_JSON" + SHOULD_DEPLOY=false + if [[ "${{ github.event_name }}" != "pull_request" ]]; then + SHOULD_DEPLOY=true + elif echo "$LABELS_JSON" | grep -E '"prerelease"' > /dev/null; then + SHOULD_DEPLOY=true + elif echo "$LABELS_JSON" | grep -E '"prerelease:'"${{ matrix.locale }}"'"' > /dev/null; then + SHOULD_DEPLOY=true + fi + echo "should_deploy=$SHOULD_DEPLOY" >> $GITHUB_OUTPUT - name: Checkout code + if: steps.should_deploy.outputs.should_deploy == 'true' uses: actions/checkout@v3 with: fetch-depth: 1 + ref: ${{ github.head_ref }} + - name: Log checked out commit + if: steps.should_deploy.outputs.should_deploy == 'true' + run: git log -1 - name: Setup Tools + if: steps.should_deploy.outputs.should_deploy == 'true' uses: ./.github/actions/setup - name: Deploy to Vercel (${{ matrix.locale }}) + if: steps.should_deploy.outputs.should_deploy == 'true' id: deploy uses: ./.github/actions/vercel-deploy with: @@ -42,6 +69,7 @@ jobs: vercel_org_id: ${{ secrets.VERCEL_ORG_ID }} vercel_token: ${{ secrets.VERCEL_TOKEN }} - name: Comment PR with Vercel Preview Links (${{ matrix.locale }}) + if: steps.should_deploy.outputs.should_deploy == 'true' uses: ./.github/actions/comment-vercel-preview with: inspect_url: ${{ steps.deploy.outputs.inspect_url }} From 34cff4d5e5e33bb13f67ea0e75c668790e3fdba1 Mon Sep 17 00:00:00 2001 From: "yanqi.zong" Date: Mon, 26 May 2025 09:51:32 -0700 Subject: [PATCH 2/2] feat: comment pr with all vercel preview links --- .../actions/comment-vercel-preview/action.yml | 53 +++++++++++------- .github/workflows/prerelease.yml | 54 +++++++++++++++++-- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/.github/actions/comment-vercel-preview/action.yml b/.github/actions/comment-vercel-preview/action.yml index 937372d8..023dc0c7 100644 --- a/.github/actions/comment-vercel-preview/action.yml +++ b/.github/actions/comment-vercel-preview/action.yml @@ -1,33 +1,50 @@ name: 'Comment Vercel Preview' description: 'Comment Vercel deploy preview links on a PR.' inputs: - inspect_url: - description: 'Inspect URL from Vercel deploy output.' - required: true - preview_url: - description: 'Preview/Production URL from Vercel deploy output.' - required: true - label: - description: 'Label for the deployment (e.g., EN, ZH-HANS).' + deploy_results_path: + description: 'Path to the JSON file containing all deploy results.' required: true runs: using: 'composite' steps: - - name: Comment PR with Vercel Preview Links + - name: Comment PR with all Vercel Preview Links uses: actions/github-script@v7 with: script: | - const inspect = process.env.INSPECT_URL; - const preview = process.env.PREVIEW_URL; - let body = `**Vercel Deploy Preview (${process.env.LABEL})**\n\n`; - body += `- [Inspect](${inspect}) | [Preview](${preview})`; - github.rest.issues.createComment({ + const fs = require('fs'); + const path = process.env.DEPLOY_RESULTS_PATH; + const marker = ''; + const results = JSON.parse(fs.readFileSync(path, 'utf8')); + let body = `${marker}\n`; + body += `## Vercel Deploy Previews\n`; + console.log('Deploy results:', results); + // Handle array of deploy results + for (const job of results) { + if (job && job.label) { + body += `- **${job.label}**: [Inspect](${job.inspect_url}) | [Preview](${job.preview_url})\n`; + } + } + // Find existing comment + const { data: comments } = await github.rest.issues.listComments({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body }); + const existing = comments.find(c => c.body && c.body.includes(marker)); + if (existing) { + await github.rest.issues.updateComment({ + comment_id: existing.id, + owner: context.repo.owner, + repo: context.repo.repo, + body + }); + } else { + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body + }); + } env: - INSPECT_URL: ${{ inputs.inspect_url }} - PREVIEW_URL: ${{ inputs.preview_url }} - LABEL: ${{ inputs.label }} + DEPLOY_RESULTS_PATH: ${{ inputs.deploy_results_path }} diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml index e86c592a..f4a8d0ac 100644 --- a/.github/workflows/prerelease.yml +++ b/.github/workflows/prerelease.yml @@ -68,10 +68,56 @@ jobs: vercel_project_id: ${{ secrets[matrix.secret_project_id] }} vercel_org_id: ${{ secrets.VERCEL_ORG_ID }} vercel_token: ${{ secrets.VERCEL_TOKEN }} - - name: Comment PR with Vercel Preview Links (${{ matrix.locale }}) + - name: Set deploy outputs if: steps.should_deploy.outputs.should_deploy == 'true' + id: set_outputs + run: | + echo "preview_url=${{ steps.deploy.outputs.prod_url }}" >> $GITHUB_OUTPUT + echo "inspect_url=${{ steps.deploy.outputs.inspect_url }}" >> $GITHUB_OUTPUT + echo "label=${{ matrix.locale }}" >> $GITHUB_OUTPUT + - name: Write deploy result to file + if: steps.should_deploy.outputs.should_deploy == 'true' + run: | + jq -n \ + --arg label "${{ steps.set_outputs.outputs.label }}" \ + --arg preview_url "${{ steps.set_outputs.outputs.preview_url }}" \ + --arg inspect_url "${{ steps.set_outputs.outputs.inspect_url }}" \ + '{label: $label, preview_url: $preview_url, inspect_url: $inspect_url}' > deploy-result.json + - name: Upload deploy result artifact + if: steps.should_deploy.outputs.should_deploy == 'true' + uses: actions/upload-artifact@v4 + with: + name: deploy-result-${{ matrix.locale }} + path: deploy-result.json + + comment-vercel-previews: + needs: deploy + name: Comment Vercel Previews + runs-on: ubuntu-latest + if: always() + steps: + - name: Checkout repo + uses: actions/checkout@v3 + - name: Download all deploy result artifacts + uses: actions/download-artifact@v4 + with: + pattern: deploy-result-* + path: deploy-results + - name: Combine deploy results + id: aggregate + run: | + jq -s '.' deploy-results/deploy-result-*/deploy-result.json > deploy-results.json + - name: Check if deploy results exist + id: check_results + run: | + if [ -f deploy-results.json ] && [ $(jq length deploy-results.json) -gt 0 ]; then + echo "has_results=true" >> $GITHUB_OUTPUT + else + echo "has_results=false" >> $GITHUB_OUTPUT + fi + - name: Comment PR with all Vercel Preview Links + if: | + success() && hashFiles('deploy-results.json') != '' && steps.check_results.outputs.has_results == 'true' uses: ./.github/actions/comment-vercel-preview with: - inspect_url: ${{ steps.deploy.outputs.inspect_url }} - preview_url: ${{ steps.deploy.outputs.prod_url }} - label: ${{ matrix.locale }} + deploy_results_path: deploy-results.json