Skip to content

Add Ci Preview & Deploy #6

Add Ci Preview & Deploy

Add Ci Preview & Deploy #6

Workflow file for this run

name: Docusaurus CI, Previews & Cleanup
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened, closed]
permissions:
contents: write
pages: write
id-token: write
pull-requests: write
jobs:
build:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Inject correct baseUrl into docusaurus.config.js
id: inject
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
BASE_URL="/${{ github.event.repository.name }}/pr-${{ github.event.number }}/"
else
BASE_URL="/${{ github.event.repository.name }}/"
fi
echo "Using baseUrl=$BASE_URL"
echo "baseUrl=$BASE_URL" >> $GITHUB_OUTPUT
sed -i "s|baseUrl: '.*'|baseUrl: '$BASE_URL'|" docusaurus.config.js
- name: Comment injected baseUrl on PR
if: github.event_name == 'pull_request'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: docusaurus-baseurl
message: |
🛠 **Injected baseUrl**
➤ `${{ steps.inject.outputs.baseUrl }}`
- name: Build Docusaurus site
run: npm run build
- name: Upload site build as artifact
uses: actions/upload-artifact@v4
with:
name: docusaurus-build
path: build
deploy:
if: github.event.action != 'closed'
needs: build
runs-on: ubuntu-latest
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: docusaurus-build
path: build
- name: Determine deploy target
id: vars
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "destination_dir=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.number }}/" >> $GITHUB_OUTPUT
else
echo "destination_dir=." >> $GITHUB_OUTPUT
echo "url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/" >> $GITHUB_OUTPUT
fi
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
publish_branch: gh-pages
destination_dir: ${{ steps.vars.outputs.destination_dir }}
keep_files: true # Keep other PR previews intact
- name: Comment preview link on PR
if: github.event_name == 'pull_request'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: docusaurus-preview
message: |
🚀 **Docusaurus preview deployed!**
🔗 [View Preview](${{ steps.vars.outputs.url }})
cleanup:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Remove PR preview directory
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if [ -d "pr-${{ github.event.pull_request.number }}" ]; then
git rm -rf pr-${{ github.event.pull_request.number }}
git commit -m "🧹 Remove preview for PR #${{ github.event.pull_request.number }}"
git push
else
echo "Directory pr-${{ github.event.pull_request.number }} not found, skipping cleanup"
fi