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 | on: |
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 | - name: Checkout Repository with Submodules |
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 | - name: Cache Node Modules |
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 | - name: Install Dependencies |
Using npm ci
ensures a clean and consistent dependency installation, making it ideal for CI/CD environments.
5. Generating Static Files with Hexo
1 | - name: Generate Static Files with Hexo |
This step installs the Hexo CLI globally, cleans previous builds, and generates the latest static files for deployment.
6. Deploying to GitHub Pages
1 | - name: Deploy to GitHub Pages (Push Static Files) |
πΉ 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. π