|
| 1 | +name: Preview release |
| 2 | + |
| 3 | +permissions: |
| 4 | + pull-requests: write |
| 5 | + |
| 6 | +on: |
| 7 | + # Manual trigger with inputs for control |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + trigger_supabase_js: |
| 11 | + description: 'Trigger supabase-js tests' |
| 12 | + type: boolean |
| 13 | + default: true |
| 14 | + target_branch: |
| 15 | + description: 'Target branch for supabase-js tests' |
| 16 | + type: string |
| 17 | + default: 'master' |
| 18 | + |
| 19 | + # Push to master - only when source code changes |
| 20 | + push: |
| 21 | + branches: |
| 22 | + - master |
| 23 | + paths: |
| 24 | + - 'src/**' |
| 25 | + - 'package.json' |
| 26 | + - 'package-lock.json' |
| 27 | + - 'tsconfig.json' |
| 28 | + |
| 29 | + # PR triggers - only when labeled |
| 30 | + # Using pull_request_target to access secrets when PRs come from forks |
| 31 | + pull_request_target: |
| 32 | + types: [labeled, synchronize] |
| 33 | + |
| 34 | +jobs: |
| 35 | + preview: |
| 36 | + # Run only for PRs with 'trigger: preview' label or pushes to master |
| 37 | + if: > |
| 38 | + github.repository == 'supabase/postgrest-js' && |
| 39 | + ( |
| 40 | + github.event_name == 'workflow_dispatch' || |
| 41 | + github.event_name == 'push' || |
| 42 | + (github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'trigger: preview')) |
| 43 | + ) |
| 44 | + runs-on: ubuntu-latest |
| 45 | + outputs: |
| 46 | + preview-url: ${{ steps.preview.outputs.url }} |
| 47 | + package-name: ${{ steps.preview.outputs.package }} |
| 48 | + steps: |
| 49 | + - name: Checkout code |
| 50 | + uses: actions/checkout@v4 |
| 51 | + with: |
| 52 | + # For pull_request_target, we need to explicitly checkout the PR's head |
| 53 | + ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }} |
| 54 | + |
| 55 | + - name: Setup Node.js |
| 56 | + uses: actions/setup-node@v4 |
| 57 | + with: |
| 58 | + node-version: '20' |
| 59 | + cache: 'npm' |
| 60 | + |
| 61 | + - name: Install dependencies |
| 62 | + run: npm ci |
| 63 | + |
| 64 | + - name: Build |
| 65 | + run: npm run build |
| 66 | + |
| 67 | + - name: Publish preview |
| 68 | + id: preview |
| 69 | + run: | |
| 70 | + OUTPUT=$(npx pkg-pr-new@latest publish --compact 2>&1) |
| 71 | + PREVIEW_URL=$(echo "$OUTPUT" | grep -o 'https://pkg\.pr\.new/@supabase/[^[:space:]]*' | head -1) |
| 72 | + REPO_NAME=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2) |
| 73 | +
|
| 74 | + if [ -z "$PREVIEW_URL" ]; then |
| 75 | + echo "Error: Failed to extract preview URL from pkg-pr-new output" |
| 76 | + echo "Output was: $OUTPUT" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +
|
| 80 | + echo "Preview Release URL: $PREVIEW_URL" |
| 81 | + echo "url=$PREVIEW_URL" >> $GITHUB_OUTPUT |
| 82 | + echo "package=$REPO_NAME" >> $GITHUB_OUTPUT |
| 83 | +
|
| 84 | + trigger-supabase-js-tests: |
| 85 | + needs: preview |
| 86 | + # Only run if preview URL exists and either: |
| 87 | + # - Not workflow_dispatch, OR |
| 88 | + # - workflow_dispatch with trigger_supabase_js = true |
| 89 | + if: > |
| 90 | + needs.preview.outputs.preview-url != '' && |
| 91 | + ( |
| 92 | + github.event_name != 'workflow_dispatch' || |
| 93 | + github.event.inputs.trigger_supabase_js == 'true' |
| 94 | + ) |
| 95 | + runs-on: ubuntu-latest |
| 96 | + steps: |
| 97 | + - name: Generate GitHub App token |
| 98 | + id: generate-token |
| 99 | + uses: actions/create-github-app-token@v1 |
| 100 | + with: |
| 101 | + app-id: ${{ vars.CROSS_REPO_APP_ID }} |
| 102 | + private-key: ${{ secrets.CROSS_REPO_APP_PRIVATE_KEY }} |
| 103 | + owner: supabase |
| 104 | + repositories: postgrest-js,supabase-js |
| 105 | + |
| 106 | + - name: Trigger supabase-js CI tests |
| 107 | + uses: actions/github-script@v7 |
| 108 | + with: |
| 109 | + github-token: ${{ steps.generate-token.outputs.token }} |
| 110 | + script: | |
| 111 | + const prNumber = context.issue.number || 'push'; |
| 112 | + const triggeringRepo = context.repo.repo; |
| 113 | + // Use input target_branch if workflow_dispatch, otherwise default to master |
| 114 | + const targetBranch = context.eventName === 'workflow_dispatch' && context.payload.inputs?.target_branch |
| 115 | + ? context.payload.inputs.target_branch |
| 116 | + : 'master'; |
| 117 | +
|
| 118 | + try { |
| 119 | + const response = await github.rest.actions.createWorkflowDispatch({ |
| 120 | + owner: 'supabase', |
| 121 | + repo: 'supabase-js', |
| 122 | + workflow_id: 'external-test.yml', |
| 123 | + ref: targetBranch, |
| 124 | + inputs: { |
| 125 | + triggering_repo: triggeringRepo, |
| 126 | + triggering_pr: prNumber.toString(), |
| 127 | + preview_url: '${{ needs.preview.outputs.preview-url }}', |
| 128 | + package_name: '${{ needs.preview.outputs.package-name }}', |
| 129 | + triggering_sha: context.eventName === 'pull_request_target' ? context.payload.pull_request.head.sha : context.sha |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + console.log('Successfully triggered supabase-js tests'); |
| 134 | + console.log('Response:', response.status); |
| 135 | + } catch (error) { |
| 136 | + console.error('Failed to trigger supabase-js tests:', error); |
| 137 | + throw error; |
| 138 | + } |
| 139 | +
|
| 140 | + - name: Find existing preview comment |
| 141 | + if: github.event_name == 'pull_request_target' |
| 142 | + uses: peter-evans/find-comment@v3 |
| 143 | + id: find-comment |
| 144 | + with: |
| 145 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 146 | + issue-number: ${{ github.event.pull_request.number }} |
| 147 | + comment-author: 'github-actions[bot]' |
| 148 | + body-includes: '<!-- postgrest-js-preview-status -->' |
| 149 | + |
| 150 | + - name: Create or update preview comment |
| 151 | + if: github.event_name == 'pull_request_target' |
| 152 | + uses: peter-evans/create-or-update-comment@v4 |
| 153 | + with: |
| 154 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 155 | + comment-id: ${{ steps.find-comment.outputs.comment-id }} |
| 156 | + issue-number: ${{ github.event.pull_request.number }} |
| 157 | + body: | |
| 158 | + <!-- postgrest-js-preview-status --> |
| 159 | + 🚀 **Preview release created!** |
| 160 | + |
| 161 | + supabase-js CI tests have been automatically triggered on feature branch to verify compatibility. |
| 162 | + |
| 163 | + **Preview package:** `${{ needs.preview.outputs.preview-url }}` |
| 164 | + |
| 165 | + Results will be posted here once testing is complete. |
| 166 | + |
| 167 | + edit-mode: replace |
0 commit comments