Skip to content

Feature/improve ci cd #2

Feature/improve ci cd

Feature/improve ci cd #2

Workflow file for this run

name: Docusaurus CI, Previews & Cleanup
on:
push:
branches:
- main
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 repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Cache npm dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Install dependencies
run: npm ci
- 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: Checkout repository
uses: actions/checkout@v4
- 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 "branch=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 "branch=gh-pages" >> $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: ${{ steps.vars.outputs.branch }}
- 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: Delete preview branch
uses: dawidd6/action-delete-branch@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branches: pr-${{ github.event.pull_request.number }}
soft_fail: true