Automated deploying Hexo blogs by Github Actions

Github Actions can easily implement CI/CD workflows to help us do some work, such as automated testing, packaging, deployment and other operations.

When we run Jobs, it creates a container (runner) that supports Ubuntu, Windows, and MacOS. In the container we can install software, use the installed software to help us process some data, and then push the processed data somewhere.

In this article, we’ll introduce the use of GitHub Actions to automate the deployment of Hexo to GitHub Pages. Generally, blogs that use Hexo use hexo generate --deploy to deploy, but this can take a long time if there are a lot of articles, or if the local node.js version is not compatible with Hexo. Using GitHub Actions helps you to avoid these situations.

blog and xxx.github.io repository

You need two Git repositories, one for the blog source repository blog and one for the static page repository xxx.github.io

The blog source repository, blog, can be private.

The static page repository xxx.github.io, which must be public.

deploy keys

Create a key using ssh-keygen (you can also use id_rsa if you want)

In the blog source code repository page, add new secret via setting -> Security -> Secrets -> Actions -> New repository secret. The content is the private key.

image.png

In the static page repository page, add new Deploy keys via setting -> Security -> Deploy keys. The content is the public key. And select “Allow Write Access”.

Create GitHub Actions

Create a .github/workflows/xxx.yml file in the root of the blog source repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
name: HEXO_DEPLOY
on: [push]

env:
GIT_USER: ACce1er4t0r
GIT_EMAIL: lkjlkj174@gmail.com
THEME_REPO: ppoffice/hexo-theme-icarus
THEME_BRANCH: master
DEPLOY_REPO: ACce1er4t0r/ACce1er4t0r.github.io
DEPLOY_BRANCH: main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout source
uses: actions/checkout@v3

- name: Checkout theme repo
uses: actions/checkout@v3
with:
repository: ${{ env.THEME_REPO }}
ref: ${{ env.THEME_BRANCH }}
path: themes/icarus

- name: Checkout deploy repo
uses: actions/checkout@v3
with:
repository: ${{ env.DEPLOY_REPO }}
ref: ${{ env.DEPLOY_BRANCH }}
path: .deploy_git

- name: Use Node.js 14
uses: actions/setup-node@v2
with:
node-version: 14

- name: Setup key
env:
ACTION_DEPLOY_KEY: ${{ secrets.BLOG_DEPLOY }}
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email $GIT_USER
git config --global user.name $GIT_EMAIL
npm install
npm install --save hexo-component-inferno@^2.0.2

- name: Hexo deploy
run: |
npm run deploy

Meaning of the parameters

  • name: the name of the Action
  • on: Trigger condition, this task will be triggered when the condition is met, here on.push.branches.$.master means that the task will be executed when the master branch receives the push.
  • env: Environment variable objects
  • jobs: List of tasks under Action
    • jobs.{job}.name: Task name
    • jobs.{job}.runs-on: The required container for the task, optional values: ubuntu-latest, windows-latest, macos-latest.
    • jobs.{job}.strategy: policy, can be written in array format, the job will iterate through this array.
    • jobs.{job}.steps: A step where you can put what you want to do in steps.
      • jobs.{job}.steps.$.name: The name of the step, which will be output as a LOG when compiled.
      • jobs.{job}.steps.$.uses: The Action to be called, you can see more at https://github.com/actions.
      • jobs.{jobs}.steps.$.with: An object that calls the Action, see the description of the Action used for details.

Finally, just push the file to the master branch

Author

ACce1er4t0r

Posted on

2022-12-18

Updated on

2023-04-22

Licensed under