Thiết lập GitHub Actions tự động triển khai cho blog Hexo

Mình vừa chuyển sang dùng Hexo và lúc đầu dùng Spck Editor để viết bài, mỗi lần cập nhật phải push thủ công khá mất thời gian. Tình cờ trong nhóm bạn dev biết đến GitHub Actions, cho phép tự động build blog Hexo và deploy lên GitHub Pages mỗi khi có commit mới, và trình soạn thảo trên GitHub cũng cực kỳ tiện lợi. Vậy là mình dành ra 1 tiếng rưỡi để setup – hoàn toàn trên thiết bị Android, không có máy tính!

Tạo blog Hexo nếu chưa có

Nếu bạn chưa có blog Hexo, thực hiện qua ZeroTermux:

pkg install nodejs

Kiểm tra:

node -v
npm -v

Cài Hexo CLI và khởi tạo blog:

npm install hexo-cli -g
hexo init blog

Sau đó di chuyển thư mục blog ra bộ nhớ ngoài (ví dụ qua MT Manager).

Tạo kho chứa mã nguồn trên GitHub

Tạo repository mới (ví dụ: MyHexo), đặt chế độ Private. Trên thiết bị di động, dùng Spck Editor, tạo thư mục /storage/emulated/0/Android/data/io.spck/files/MyHexo/, copy toàn bộ nội dung thư mục blog Hexo (trừ node_modules.deploy_git) sang đó.

Trong Spck:

  • Mở dự án
  • Khởi tạo Git repository (nhấn biểu tượng branch)
  • Thêm remote: git remote add origin https://github.com/username/MyHexo.git

Tạo Personal Access Token

Truy cập https://github.com/settings/tokens, tạo token với quyền repo. Sao chép token ngay vì chỉ hiện một lần.

Quay lại Spck, thêm token vào cấu hình Git, rồi commit và push toàn bộ mã nguồn lên GitHub.

Cấu hình GitHub Actions

Vào repository MyHexoSettingsSecrets and variablesActionsNew repository secret, thêm 3 biến môi trường:

Secret nameGiá trị
GITHUBMAILEmail tài khoản GitHub
GITHUBTOKENToken vừa tạo
GITHUBUSERNAMETên người dùng GitHub

Vào tab ActionsNew workflowset up a workflow yourself. Dán nội dung sau:

name: Hexo Auto Deploy

on:
  push:
    branches:
      - master

jobs:
  build-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          ref: master

      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: "16.x"

      - name: Install Hexo CLI
        run: |
          export TZ='Asia/Shanghai'
          npm install hexo-cli -g

      - name: Cache node_modules
        uses: actions/cache@v1
        id: cache
        with:
          path: node_modules
          key: ${{ runner.OS }}-${{ hashFiles('**/package-lock.json') }}

      - name: Install dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: npm install

      - name: Generate static files
        run: |
          hexo clean
          hexo generate

      - name: Deploy to GitHub Pages
        run: |
          cd ./public
          git init
          git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
          git config --global user.email '${{ secrets.GITHUBEMAIL }}'
          git add .
          git commit -m "Auto deploy: ${{ github.event.head_commit.message }}"
          git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io.git" master:master

Lưu ý: Nếu nhánh local của bạn là main thay vì master, đổi cả branchesref thành main.

Nhấn Commit changes....

Cuối cùng, vào repository username.github.io (nơi Pages sẽ deploy) kiểm tra cấu hình Pages để đảm bảo source trùng với nhánh bạn đã cấu hình trong workflow (ví dụ: master).

Thẻ: GitHub Actions Hexo CI/CD Node.js GitHub Pages

Đăng vào ngày 30 tháng 5 lúc 22:47