Skip to content

Commit 77430d0

Browse files
committed
feat: update pr preview workflow with correct base url and deployment logic
1 parent dbc940a commit 77430d0

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

.github/workflows/publish-docs.yml

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ name: Publish Docs
22

33
on:
44
push:
5-
branches: [master]
5+
branches:
6+
- master
67
pull_request:
7-
branches: [master]
8+
branches:
9+
- master
810
workflow_dispatch:
911
inputs:
1012
pr_number:
@@ -31,8 +33,25 @@ jobs:
3133
- name: Install dependencies
3234
run: npm ci
3335

36+
- name: Determine Base URL
37+
id: base-url
38+
run: |
39+
REPO_NAME="${{ github.event.repository.name }}"
40+
OWNER="${{ github.repository_owner }}"
41+
42+
# Check if it's an org page (owner.github.io)
43+
if [[ "${REPO_NAME,,}" == "${OWNER,,}.github.io" ]]; then
44+
BASE_PATH="/"
45+
else
46+
BASE_PATH="/$REPO_NAME/"
47+
fi
48+
49+
echo "base_path=$BASE_PATH" >> $GITHUB_OUTPUT
50+
3451
- name: Build
3552
run: npm run build
53+
env:
54+
BASE_URL: ${{ steps.base-url.outputs.base_path }}
3655

3756
- name: Upload build artifact
3857
uses: actions/upload-artifact@v4
@@ -46,36 +65,51 @@ jobs:
4665
steps:
4766
- name: Checkout
4867
uses: actions/checkout@v4
68+
with:
69+
ref: refs/pull/${{ inputs.pr_number }}/head
4970

50-
- name: Find latest run ID
51-
id: find-run
52-
env:
53-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71+
- name: Setup Node.js
72+
uses: actions/setup-node@v4
73+
with:
74+
node-version: 20
75+
76+
- name: Install dependencies
77+
run: npm ci
78+
79+
- name: Determine Base URL
80+
id: base-url
5481
run: |
55-
BRANCH=$(gh pr view ${{ inputs.pr_number }} --json headRefName --jq .headRefName)
56-
echo "Found branch: $BRANCH"
57-
RUN_ID=$(gh run list --workflow "Publish Docs" --branch "$BRANCH" --event pull_request --status success --limit 1 --json databaseId --jq '.[0].databaseId')
58-
echo "Found run ID: $RUN_ID"
59-
if [ -z "$RUN_ID" ]; then
60-
echo "::error::No successful 'Publish Docs' run found for PR #${{ inputs.pr_number }} (branch: $BRANCH)"
61-
exit 1
82+
REPO_NAME="${{ github.event.repository.name }}"
83+
OWNER="${{ github.repository_owner }}"
84+
PR_NUM="${{ inputs.pr_number }}"
85+
86+
# Check if it's an org page (owner.github.io)
87+
if [[ "${REPO_NAME,,}" == "${OWNER,,}.github.io" ]]; then
88+
BASE_PATH="/pr-$PR_NUM/"
89+
FULL_URL="https://$OWNER.github.io/pr-$PR_NUM/"
90+
else
91+
BASE_PATH="/$REPO_NAME/pr-$PR_NUM/"
92+
FULL_URL="https://$OWNER.github.io/$REPO_NAME/pr-$PR_NUM/"
6293
fi
63-
echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT
94+
95+
echo "base_path=$BASE_PATH" >> $GITHUB_OUTPUT
96+
echo "full_url=$FULL_URL" >> $GITHUB_OUTPUT
6497
65-
- name: Download build artifact
66-
uses: actions/download-artifact@v4
67-
with:
68-
name: site
69-
path: site
70-
run-id: ${{ steps.find-run.outputs.run_id }}
71-
github-token: ${{ secrets.GITHUB_TOKEN }}
98+
- name: Build
99+
run: npm run build
100+
env:
101+
BASE_URL: ${{ steps.base-url.outputs.base_path }}
72102

73103
- name: Deploy PR Preview
74104
uses: JamesIves/github-pages-deploy-action@v4
75105
with:
76-
branch: gh-pages-pr-previews
77-
folder: site
106+
token: ${{ secrets.GITHUB_TOKEN }}
107+
branch: gh-pages
108+
folder: build
78109
target-folder: pr-${{ inputs.pr_number }}
110+
clean: true
111+
git-config-name: github-actions[bot]
112+
git-config-email: github-actions[bot]@users.noreply.github.com
79113

80114
- name: Comment Preview URL
81115
uses: marocchino/sticky-pull-request-comment@v2
@@ -86,10 +120,10 @@ jobs:
86120
🚀 **Preview Ready!**
87121
Your docs preview for this PR is available here:
88122
89-
**https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ inputs.pr_number }}/**
123+
**${{ steps.base-url.outputs.full_url }}**
90124
91125
deploy-production:
92-
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
126+
if: ${{ github.event_name == 'push' }}
93127
runs-on: ubuntu-latest
94128
needs: build
95129
steps:

0 commit comments

Comments
 (0)