Skip to content

Commit 4c53aaa

Browse files
committed
Refactor build preview workflow: streamline baseUrl injection and cleanup process
1 parent a9cca8f commit 4c53aaa

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

.github/workflows/build_preview.yml

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ permissions:
1313
id-token: write
1414
pull-requests: write
1515

16-
1716
jobs:
1817
build:
1918
if: github.event.action != 'closed'
@@ -32,6 +31,7 @@ jobs:
3231

3332
- name: Install dependencies
3433
run: npm ci
34+
3535
- name: Inject correct baseUrl into docusaurus.config.js
3636
id: inject
3737
run: |
@@ -45,6 +45,7 @@ jobs:
4545
echo "baseUrl=$BASE_URL" >> $GITHUB_OUTPUT
4646
4747
sed -i "s|baseUrl: '.*'|baseUrl: '$BASE_URL'|" docusaurus.config.js
48+
4849
- name: Comment injected baseUrl on PR
4950
if: github.event_name == 'pull_request'
5051
uses: marocchino/sticky-pull-request-comment@v2
@@ -69,8 +70,6 @@ jobs:
6970
runs-on: ubuntu-latest
7071

7172
steps:
72-
73-
7473
- name: Download build artifact
7574
uses: actions/download-artifact@v4
7675
with:
@@ -81,10 +80,10 @@ jobs:
8180
id: vars
8281
run: |
8382
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
84-
echo "branch=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
83+
echo "destination_dir=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
8584
echo "url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.number }}/" >> $GITHUB_OUTPUT
8685
else
87-
echo "branch=gh-pages" >> $GITHUB_OUTPUT
86+
echo "destination_dir=." >> $GITHUB_OUTPUT
8887
echo "url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/" >> $GITHUB_OUTPUT
8988
fi
9089
@@ -93,7 +92,9 @@ jobs:
9392
with:
9493
github_token: ${{ secrets.GITHUB_TOKEN }}
9594
publish_dir: ./build
96-
publish_branch: ${{ steps.vars.outputs.branch }}
95+
publish_branch: gh-pages
96+
destination_dir: ${{ steps.vars.outputs.destination_dir }}
97+
keep_files: true # Keep other PR previews intact
9798

9899
- name: Comment preview link on PR
99100
if: github.event_name == 'pull_request'
@@ -109,10 +110,20 @@ jobs:
109110
runs-on: ubuntu-latest
110111

111112
steps:
112-
- name: Delete preview branch
113-
uses: dawidd6/action-delete-branch@v3
113+
- name: Checkout gh-pages
114+
uses: actions/checkout@v4
114115
with:
115-
github_token: ${{ secrets.GITHUB_TOKEN }}
116-
branches: pr-${{ github.event.pull_request.number }}
117-
soft_fail: true
118-
116+
ref: gh-pages
117+
118+
- name: Remove PR preview directory
119+
run: |
120+
git config user.name "github-actions[bot]"
121+
git config user.email "github-actions[bot]@users.noreply.github.com"
122+
123+
if [ -d "pr-${{ github.event.pull_request.number }}" ]; then
124+
git rm -rf pr-${{ github.event.pull_request.number }}
125+
git commit -m "🧹 Remove preview for PR #${{ github.event.pull_request.number }}"
126+
git push
127+
else
128+
echo "Directory pr-${{ github.event.pull_request.number }} not found, skipping cleanup"
129+
fi

0 commit comments

Comments
 (0)