# A workflow for deploying a Hexo blog based on GitHub Actions

> This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to [original Chinese version](<https://blog.wendain.ing/posts/hexo-workflow-based-on-github-actions/>). I am continuously working to improve the translation.

## TL;DR

A workflow for managing and deploying a Hexo blog directly using the raw git approach, so that the statically hosted repository not only hosts the web content, but also preserves all the information of the blog repository.

## Why do it this way

It started with something I saw while idly chatting in a group chat:

<img src="https://image.wendaining.top/image-20260428132235570.png" style="zoom:33%;" />

<img src="https://image.wendaining.top/QQ_1777353854562.png" style="zoom:33%;" />

<img src="https://image.wendaining.top/QQ_1777353812097.png" style="zoom:33%;" />

Actually, once you read it, you basically get it. The logic is pretty simple — anyone familiar with GitHub Actions and CI/CD workflows could easily come up with it. Unfortunately, I'm too much of a noob and only thought of it after reading.

Anyway, let's implement it.

## Implementation

In my blog's hosting repository, I created a new branch called Blog-Source, whose contents are exactly identical to my local blog folder.

In the local folder, I created a local git repository, and manage the blog through a git workflow, abandoning the original hexo workflow such as `hexo deploy` and the like.

I created a new GitHub Actions workflow, located at `./.github/workflows/`, with the following content:

```yml
name: Deploy to GitHub Pages

on:
  push:
    branches:
      - Blog-Source
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '22'
          cache: 'npm'

      - run: npm ci

      - run: npx hexo generate

      - uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public
          publish_branch: main
          force_orphan: false

```

After that, every commit to the Blog-Source branch will trigger an automatic deployment of the blog.

There are many benefits to this:

- The blog's hosting repository now has the ability to preserve the blog's content, rather than just a pile of compiled HTML files;
- I can modify the blog anytime, anywhere — for example, this entire section above was actually **written by me on my phone while out and about, via the GitHub APP**. After committing, the CI pipeline can deploy it for me directly.


---

Below is a workflow I started designing myself, but it turned out to be complete garbage, so I abandoned it (still hurting over the tokens I burned).

Below are some miscellaneous notes from back then, to be analyzed and cleaned up some other day.

> Today I first tried out a new workflow for deploying a hexo blog. I'll open-source it some other day; first, let me list the TODOs:
>
> Open-source this framework, stripping out anything related to my own blog, anything related to the specific hexo theme, and anything that isn't cross-platform
>
> Open-sourcing it definitely means writing a README, so I'll figure these things out while writing the documentation
>
> I found that I can't put a README on the blog's main branch, otherwise it gets overwritten on every deployment. So I plan to restructure the repo's branch layout into an empty main branch containing only a README, with the other branches each handling their own tasks. That is: turn the current main branch into the gh-pages branch, then create a new main branch as the default branch but with nothing in it except a README, and see how that works
>
> ```TEXT
> remote: Enumerating objects: 310, done.
> remote: Counting objects: 100% (49/49), done.
> remote: Compressing objects: 100% (37/37), done.
> remote: Total 310 (delta 16), reused 16 (delta 5), pack-reused 261 (from 1)
> Receiving objects: 100% (310/310), 9.18 MiB | 518.00 KiB/s, done.
> ```
>
> This part was way too slow and needs to be fixed
>
> Ask whether the two actions conflict when concurrently modifying the blog content and the blog framework configuration — I often see failures, and I wonder whether this has any impact
>
> The purpose of creating the `docs-archive` branch:
>
> - To give the hosting repository the ability to preserve the original documents;
> - To allow editing markdown documents directly on the web, after which the action will automatically run the deployment workflow.
>
> But there's a problem: suppose I finish editing / create some file on the web side — then I'd find that:
>
> - Under the current **local** workflow, **I have no way to fetch the documents I created / modified remotely**;
> - Consequently, if I want to update the local documents, **they will inevitably overwrite the content previously modified / created remotely**.
>
> This problem is really a conflict between two workflows:
>
> - Modifying files remotely is essentially the traditional git approach: edit some markdown file, then add, commit, push (though these three operations are basically merged into one on the web side), which then triggers the CI/CD pipeline built with GitHub Actions and triggers the deployment;
> - Modifying files in the local `./source/_posts` folder follows the hexo approach, using the currently hacked-up `npm run publish`, but essentially it's still a combination of hexo CLI commands. **In short, the local documents are not actually managed with git at all! There's no `.git` directory in these folders at all, and deploying from local to remote still relies on that hexo set of operations**.
> - Actually, I do have a solution:
>     - Pull my `./source/_posts` folder out so it doesn't participate in the previous hexo-style workflow, and instead use the git approach to push it directly to the `docs-archive` branch;
>     - Likewise, changes to the blog configuration can be handled by pulling the contents of the `hexo-source` branch out, and doing a separate push each time;
>     - The rest is left to GitHub's CI/CD pipeline
> - Looking at it this way, the current workflow still needs to be changed, but I feel this is a bit cumbersome, because previously a single one-click `hexo clean && hexo generate && hexo deploy` could handle everything, whereas now I'd have to split it into two git repositories and push them separately. I wonder if there's a better solution.
>
> I need to rename .github/workflows/docs-archive.yml — the `docs-archive.yml` name could be changed to `docs-deploy.yml`, but I don't know whether this would conflict with what I said earlier: "ask whether the two actions conflict when concurrently modifying the blog content and the blog framework configuration — I often see failures, and I wonder whether this has any impact"? I think maybe it would be better to split it into two separate GitHub Actions yml files?
>
> Also, I should still have a chance to roll back to the original workflow, right? I just need to reset the main branch to before, and then continue using the previous `hexo deploy` workflow, right?
>
> ```TEXT
> npm run preview
>   Local preview: clean -> generate -> server
> 
> npm run build
>   Local build verification: clean -> generate
> 
> npm run publish:posts
>   Sync source/_posts to docs-archive
>   Then GitHub Actions automatically publishes the blog
> 
> npm run publish:source
>   Sync the current Hexo framework, theme, configuration, and scripts to hexo-source
>   Then GitHub Actions automatically publishes the blog
> 
> npm run publish
>   First publish:source, then publish:posts
>   Suitable for when you've changed both the blog framework and the posts
> ```
>
> ---
>
> Around 11 PM on 4/27, after talking with Kisechan, I found that the workflow I designed was completely crushed by the one he normally uses, so I plan to just switch over to his approach.
>
> Everything above can be considered void.
>
> TODO: Have the GPT from that session summarize my original design and write it directly here — just have the LLM summarize it as-is.
>
> Later I'll come back and add Kisechan's workflow here myself. Honestly, the reason I realized he completely crushed me is that I found all the problems mentioned above can be solved by his workflow. So here I should write down why I think his approach completely crushes mine, starting from the problems above.
