Aller au contenu principal

Blog personnel avec Docusaurus, Github Pages + Actions

· 2 minutes de lecture
Florent B.

L'objectif est de publier un blog sur Github Pages en utilisant Docusaurus et un déploiement automatique. Afin de garder privée les Issues et Pull Request, j'utilise deux dépôts distincts :

  • personal-blog: Dépôt privé contenant les sources, les actions, les issues et PR
  • flobsx.github.io: Le Dépôt public automatiquement reconstruit lors des update de personal-blog

Prérequis

Installer :

  • Node JS
  • Git
  • GH

Créer le projet Docusaurus

npx create-docusaurus@latest personal-blog classic
cd personal-blog
npm i

git init
git add .
git commit -m "first commit"
git barnch -M main
git remote add origin https://github.com/flobsx/personal-blog.git
git push -u origin main

code .

Modifier la configuration

Ouvrir le fichier docusaurus.config.js

const config = {
title: 'flobsx dev blog',
tagline: 'Vers l\'infini et puis voilà 🚀',
favicon: 'img/favicon.ico',

// Set the production url of your site here
url: 'https://flobsx.github.io',
// Set the /<baseUrl>/ pathname under which your site is served
// For GitHub pages deployment, it is often '/<projectName>/'
baseUrl: '/',

// GitHub pages deployment config.
// If you aren't using GitHub pages, you don't need these.
organizationName: 'flobsx', // Usually your GitHub org/user name.
projectName: 'flobsx.github.io', // Usually your repo name.
deploymentBranch: 'gh-pages',
trailingSlash: true,
[...]

Déploiement automatique sur dépôt distant

Préparer les clés de déploiement :

ssh-keygen -t ed25519 -C "flobsx@gmail.com"
gh auth refresh -h github.com -s admin:public_key
gh repo deploy-key add ~/.ssh/id_ed25519.pub --allow-write --title "gh-action-deploy" --repo flobsx/flobsx.github.io
gh secret set GH_PAGES_DEPLOY -R flobsx/personal-blog < ~/.ssh/id_ed25519

Créer le workflow :

mkdir .github
mkdir .github/workflows
code .github/workflows/deploy.yml
name: Deploy to GitHub Pages

on:
pull_request:
branches: [main]
push:
branches: [main]

permissions:
contents: write

jobs:
test-deploy:
if: github.event_name != 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Test build website
run: yarn build
deploy:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
- uses: webfactory/ssh-agent@v0.5.0
with:
ssh-private-key: ${{ secrets.GH_PAGES_DEPLOY }}
- name: Deploy to GitHub Pages
env:
USE_SSH: true
run: |
git config --global user.email "flobsx@gmail.com"
git config --global user.name "flobsx"
yarn install --frozen-lockfile
yarn deploy

Finalisation

yarn install
GIT_USER=flobsx yarn deploy

git add .
git commit -m "ready to deploy"
git push -u origin main