Huy's Notes
Automate your static blog deployment with Github Page

Automate your static blog deployment with Github Page

Previously, I have a Gatsby blog managed in a private repository named huytd/blog, and everytime I write something, I had to run the gatsby build command and push everything in the public folder (Gatsby's default output folder) into a public Github Pages repository named huytd/huytd.github.io.

I want to setup a Github Action to automate this workflow, so all I need to do is just write and push. Or when I use some online markdown editor like vim.md, I can just forget about pushing stuff too.

The idea of the workflow would be:

  1. Checkout the private huytd/blog repository into tho root folder
  2. Checkout the public huytd/huytd.github.io repository into /public folder
  3. Run gatsby build in the root folder
  4. Commit and push the changes in public folder to the public repository

Here's the full workflow:

name: Gatsby Publish

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
      - uses: actions/[email protected]
        with:
          repository: 'huytd/huytd.github.io'
          token: ${{ secrets.ACCESS_TOKEN }}
          path: 'public'
      - name: Setting up
        run: |
         npm install
         npm run build
      - name: Push to Github Pages
        run: |
         cd public
         git config user.name github-actions
         git config user.email [email protected]
         git add .
         git commit -m "generated"
         git push

Referred in


If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.