banner
9527DHX

9527's xLog

无声无色的 X 空间 同步博客内容 | 内含限定博文 | 高可用性
github
twitter_id

Automatically Deploy Hexo Blog Using Github Actions and Netlify/Vercel

The following code is modified from the Hexo official documentation: Deploy Hexo on GitHub Pages. This tutorial needs to be used in conjunction with the above or other tutorials.
Before using, please create .github/workflows/xxx.yml in the repository, with a name of your choice.

This workflow file mainly implements the following process:
Users modify articles in Hexo locally, synchronize changes to GitHub, trigger GitHub Actions, generate new static files according to .github/workflows/xxx.yml, and update the files to the gh-pages branch (customizable) within the same repository. Netlify/Vercel can then pull the gh-pages branch through pre-configuration and directly deploy the website.

Compared to other methods, this approach moves the build process to GitHub Actions.
The last step uses the peaceiris/actions-gh-pages workflow preset, which has more features. For details, please refer to: GitHub Pages action - GitHub Marketplace

name: Build and Deploy # Customizable workflow name

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          submodules: recursive
          
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Cache NPM dependencies
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.OS }}-npm-cache
          restore-keys: |
            ${{ runner.OS }}-npm-cache

      - name: Install Dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./public # Customizable Hexo output directory

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public # Customizable Hexo output directory
          publish_branch: gh-pages # Customizable output branch
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.