Skip to content

Commit 250bb9f

Browse files
authored
ci(cli): publish pkg.pr.new previews on pull requests (#5427)
## Summary Simplifies preview publishing to match the pkg.pr.new pull_request model. The preview workflow now runs directly on non-draft PRs to develop, builds the legacy release-shaped CLI artifacts, publishes all platform wrapper packages plus apps/cli to pkg.pr.new with comments disabled, and logs/verifies the PR-scoped npx command. This removes the workflow_run dependency on Test, the same-repo-only fork guard, and the custom github-actions PR comment. Fork PRs can now get previews through GitHub's normal fork workflow approval model without granting issue/comment write permissions to the preview workflow.
1 parent cd3e082 commit 250bb9f

1 file changed

Lines changed: 16 additions & 148 deletions

File tree

Lines changed: 16 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,44 @@
11
name: Publish Preview CLI Packages
22

33
on:
4-
workflow_run:
5-
workflows:
6-
- Test
4+
pull_request:
75
types:
8-
- completed
6+
- opened
7+
- synchronize
8+
- reopened
9+
- ready_for_review
10+
branches:
11+
- develop
912

1013
permissions:
1114
actions: read
1215
contents: read
13-
pull-requests: read
1416

1517
concurrency:
16-
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_sha || github.run_id }}
18+
group: ${{ github.workflow }}-${{ github.head_ref }}
1719
cancel-in-progress: true
1820

1921
jobs:
20-
resolve:
21-
if: >-
22-
github.event.workflow_run.event == 'pull_request' &&
23-
github.event.workflow_run.conclusion == 'success'
24-
name: Resolve preview build context
25-
runs-on: ubuntu-latest
26-
env:
27-
GH_TOKEN: ${{ github.token }}
28-
REPOSITORY: ${{ github.repository }}
29-
outputs:
30-
should_build: ${{ steps.context.outputs.should_build }}
31-
pr_number: ${{ steps.context.outputs.pr_number }}
32-
pr_head_sha: ${{ steps.context.outputs.pr_head_sha }}
33-
preview_version: ${{ steps.context.outputs.preview_version }}
34-
steps:
35-
- name: Resolve PR context
36-
id: context
37-
run: |
38-
set -euo pipefail
39-
40-
should_build=false
41-
pr_number="$(jq -r '.workflow_run.pull_requests[0].number // ""' "$GITHUB_EVENT_PATH")"
42-
pr_head_sha="$(jq -r '.workflow_run.head_sha // ""' "$GITHUB_EVENT_PATH")"
43-
pr_head_branch="$(jq -r '.workflow_run.head_branch // ""' "$GITHUB_EVENT_PATH")"
44-
45-
if [[ -z "${pr_head_sha}" ]]; then
46-
echo "Workflow run has no head SHA; skipping."
47-
echo "should_build=false" >> "$GITHUB_OUTPUT"
48-
exit 0
49-
fi
50-
51-
if [[ -z "${pr_number}" && -n "${pr_head_branch}" ]]; then
52-
pr_number="$(
53-
gh pr list \
54-
--repo "${REPOSITORY}" \
55-
--head "${pr_head_branch}" \
56-
--state open \
57-
--json number,headRefOid \
58-
--jq 'map(select(.headRefOid == "'"${pr_head_sha}"'")) | .[0].number // ""'
59-
)"
60-
fi
61-
62-
if [[ -z "${pr_number}" ]]; then
63-
echo "Test run is not associated with an open pull request; skipping."
64-
echo "should_build=false" >> "$GITHUB_OUTPUT"
65-
exit 0
66-
fi
67-
68-
pr_json="$(gh api "repos/${REPOSITORY}/pulls/${pr_number}")"
69-
current_head_sha="$(jq -r '.head.sha' <<< "${pr_json}")"
70-
state="$(jq -r '.state' <<< "${pr_json}")"
71-
draft="$(jq -r '.draft' <<< "${pr_json}")"
72-
head_repo="$(jq -r '.head.repo.full_name' <<< "${pr_json}")"
73-
base_repo="$(jq -r '.base.repo.full_name' <<< "${pr_json}")"
74-
75-
if [[ "${state}" != "open" ]]; then
76-
echo "PR #${pr_number} is ${state}; skipping."
77-
echo "should_build=false" >> "$GITHUB_OUTPUT"
78-
exit 0
79-
fi
80-
81-
if [[ "${draft}" == "true" ]]; then
82-
echo "PR #${pr_number} is draft; skipping."
83-
echo "should_build=false" >> "$GITHUB_OUTPUT"
84-
exit 0
85-
fi
86-
87-
if [[ "${head_repo}" != "${base_repo}" ]]; then
88-
echo "PR #${pr_number} comes from fork ${head_repo}; skipping."
89-
echo "should_build=false" >> "$GITHUB_OUTPUT"
90-
exit 0
91-
fi
92-
93-
if [[ "${pr_head_sha}" != "${current_head_sha}" ]]; then
94-
echo "Test SHA ${pr_head_sha} is stale; current PR head is ${current_head_sha}. Skipping."
95-
echo "should_build=false" >> "$GITHUB_OUTPUT"
96-
exit 0
97-
fi
98-
99-
preview_version="0.0.0-pr.${pr_number}"
100-
should_build=true
101-
102-
{
103-
echo "should_build=${should_build}"
104-
echo "pr_number=${pr_number}"
105-
echo "pr_head_sha=${pr_head_sha}"
106-
echo "preview_version=${preview_version}"
107-
} >> "$GITHUB_OUTPUT"
108-
10922
build:
110-
needs: resolve
111-
if: needs.resolve.outputs.should_build == 'true'
23+
if: github.event.pull_request.draft == false
11224
name: Build preview CLI packages
11325
uses: ./.github/workflows/build-cli-artifacts.yml
11426
with:
115-
version: ${{ needs.resolve.outputs.preview_version }}
27+
version: 0.0.0-pr.${{ github.event.pull_request.number }}
11628
shell: legacy
117-
ref: ${{ needs.resolve.outputs.pr_head_sha }}
11829

11930
publish:
120-
needs: [resolve, build]
121-
if: needs.resolve.outputs.should_build == 'true' && needs.build.result == 'success'
31+
needs: build
32+
if: github.event.pull_request.draft == false && needs.build.result == 'success'
12233
name: Publish preview package
12334
runs-on: ubuntu-latest
124-
permissions:
125-
actions: read
126-
contents: read
127-
issues: write
128-
pull-requests: read
12935
env:
130-
GH_TOKEN: ${{ github.token }}
131-
PREVIEW_VERSION: ${{ needs.resolve.outputs.preview_version }}
132-
PR_HEAD_SHA: ${{ needs.resolve.outputs.pr_head_sha }}
133-
PR_NUMBER: ${{ needs.resolve.outputs.pr_number }}
134-
REPOSITORY: ${{ github.repository }}
36+
PREVIEW_VERSION: 0.0.0-pr.${{ github.event.pull_request.number }}
37+
PR_NUMBER: ${{ github.event.pull_request.number }}
13538
steps:
13639
- name: Checkout
13740
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
13841
with:
139-
ref: ${{ needs.resolve.outputs.pr_head_sha }}
14042
persist-credentials: false
14143

14244
- name: Setup
@@ -145,7 +47,7 @@ jobs:
14547
- name: Download preview build artifacts
14648
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
14749
with:
148-
name: cli-build-legacy-${{ needs.resolve.outputs.preview_version }}
50+
name: cli-build-legacy-${{ env.PREVIEW_VERSION }}
14951

15052
- name: Prepare package files
15153
run: |
@@ -176,39 +78,5 @@ jobs:
17678
run: |
17779
set -euo pipefail
17880
preview_url="https://pkg.pr.new/supabase@${PR_NUMBER}"
81+
echo "Preview command: npx ${preview_url}"
17982
npx --yes "${preview_url}" --version
180-
181-
- name: Update PR comment
182-
run: |
183-
set -euo pipefail
184-
preview_url="https://pkg.pr.new/supabase@${PR_NUMBER}"
185-
short_sha="${PR_HEAD_SHA:0:7}"
186-
marker="<!-- pkg-pr-new-preview -->"
187-
cat > comment.md <<EOF
188-
${marker}
189-
## pkg.pr.new preview
190-
191-
Published version \`${PREVIEW_VERSION}\` from commit [\`${short_sha}\`](https://github.com/${REPOSITORY}/commit/${PR_HEAD_SHA}) after tests passed.
192-
193-
\`\`\`sh
194-
npx ${preview_url}
195-
\`\`\`
196-
197-
\`\`\`sh
198-
npx ${preview_url} --version
199-
\`\`\`
200-
EOF
201-
202-
jq -n --rawfile body comment.md '{ body: $body }' > comment.json
203-
comment_id="$(
204-
gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \
205-
--paginate \
206-
--jq '.[] | select(.body | contains("'"${marker}"'")) | .id' \
207-
| head -n1
208-
)"
209-
210-
if [[ -n "${comment_id}" ]]; then
211-
gh api --method PATCH "repos/${REPOSITORY}/issues/comments/${comment_id}" --input comment.json >/dev/null
212-
else
213-
gh api --method POST "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" --input comment.json >/dev/null
214-
fi

0 commit comments

Comments
 (0)