Welcome to Ray's Blog

Stay Hungry Stay Foolish - Steve Jobs

0%

Deploy Your Hexo Blog with GitHub Actions πŸš€

GitHub Actions is a fantastic tool for automating your development workflow. In this post, I’ll guide you through setting up a GitHub Actions workflow to build and deploy your Hexo blog to GitHub Pages automatically!

Overview

This GitHub Actions workflow is designed to:

βœ… Build your Hexo blog whenever you push changes to the main branch.
βœ… Cache dependencies to speed up the build process.
βœ… Generate static files using Hexo.
βœ… Deploy the files to GitHub Pages seamlessly.

Let’s dive into the details of the workflow.


References

πŸ”Ή Hexo Blog Official Documentation: https://hexo.io/docs/
πŸ”Ή GitHub Actions Official Tutorial: https://docs.github.com/en/actions


GitHub Actions Workflow Breakdown

1. Triggering the Workflow

1
2
3
4
on:
push:
branches:
- main # Adjust this if your default branch is different

This configuration triggers the workflow whenever you push changes to the main branch. If your default branch is different, update it accordingly.


2. Checking Out the Repository

1
2
3
4
5
- name: Checkout Repository with Submodules
uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 1

This step ensures the repository is checked out along with its submodules. Setting fetch-depth: 1 speeds up the process by limiting the Git history.


3. Caching Node Modules

1
2
3
4
5
6
7
- name: Cache Node Modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

Caching node_modules helps speed up subsequent builds. The cache key is based on the operating system and package-lock.json, so dependencies are only reinstalled when necessary.


4. Installing Dependencies

1
2
- name: Install Dependencies
run: npm ci

Using npm ci ensures a clean and consistent dependency installation, making it ideal for CI/CD environments.


5. Generating Static Files with Hexo

1
2
3
4
5
- name: Generate Static Files with Hexo
run: |
npm install hexo-cli -g
hexo clean
hexo generate

This step installs the Hexo CLI globally, cleans previous builds, and generates the latest static files for deployment.


6. Deploying to GitHub Pages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- name: Deploy to GitHub Pages (Push Static Files)
run: |
# Configure Git
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"

# Clone the GitHub Pages repository
rm -rf github-pages
git clone --depth 1 https://github.com/Appigle/Appigle.github.io.git github-pages || mkdir github-pages
cd github-pages

# Ensure the branch exists
git symbolic-ref HEAD refs/heads/main || git checkout -b main

# Remove old files (except .git)
find . -mindepth 1 -maxdepth 1 ! -name ".git" -exec rm -rf {} \;

# Copy new generated files from public/
cp -r ../public/* .

# Commit and push using Personal Access Token (PAT)
git add .
git commit -m "Deploy Hexo site - $(date '+%Y-%m-%d %H:%M:%S')" || echo "No changes to commit"
git push "https://${{ secrets.DEPLOY_PAT }}@github.com/Appigle/Appigle.github.io.git" main --force

πŸ”Ή What Happens in This Step?

βœ… Git Configuration – Sets up the Git user for committing changes.
βœ… Repository Cloning – Clones your GitHub Pages repository (or creates the folder if it doesn’t exist).
βœ… Branch Handling – Ensures you’re working on the correct branch.
βœ… Cleanup – Removes old files (except .git) to prevent stale content.
βœ… Deployment – Copies the new static files from the public/ folder, commits the changes, and pushes them to your GitHub Pages repository using a Personal Access Token (PAT) stored in your GitHub Secrets.


πŸŽ‰ Wrapping Up

With this GitHub Actions workflow, your Hexo blog is now set up for continuous deployment. Every time you push changes, your site will be automatically updated without any manual intervention.

πŸš€ Happy blogging! If you have any questions or need further improvements, feel free to connect me: github.com/Appigle. πŸ˜ƒ