I recently decided to use GitHub's Actions on GitHub to build a stable and persistent blog. It may not be fast to access in China, but it is always available.
However, if I push the blog to the GitHub site repository using traditional methods, it means that I need to re-push the site every time I update the blog.
Of course, this is not a problem for people who write blogs using local Markdown files. But unfortunately, I built Qexo as an online editor, and the traditional deployment method is obviously not feasible. I have been worried about this problem before.
Until later, when I was renewing Microsoft e5 using GitHub Actions, I suddenly remembered that there is still this thing, so I used it to establish a fully automated deployment GitHub hosted site.
Step.1 Local Running Check#
Before deployment, we need to ensure that the site can run smoothly locally and generate normal static site files.
Deploy a Hexo blog (refer to the official documentation of Hexo.io for deploying Hexo)
After the deployment is complete, run the following command in the root directory of the site (the command can be abbreviated as hexo s)
hexo server
After accessing http://localhost:4000, make sure that the site can display content smoothly and run normally. Then run the following command to check if the site's static files are generated normally (hexo clean cannot be abbreviated, but hexo generate can be abbreviated as hexo g)
hexo clean && hexo generate
If it can be generated normally, run hexo clean again to clear the static files. The local running check steps are completed.
Step.2 Configuration File#
Regarding how to upload the repository to GitHub, please search for information by yourself. Here, only the relevant configuration after uploading is introduced.
First, confirm that the Hexo configuration document config.yml has the following configuration
deploy:
type: git
repository: [email protected]:yzsong06/Demo.git
branch: main
Please set the Repository to your GitHub Pages repository, such as xxx.github.io. Note the use of SSH address!
Step.3 Generate Keys#
For some users...
- For Windows 10 users, you can install Ubuntu in the Store.
- For Android users, you can install Termux and install OpenSSH.
- For Linux users, um... you should understand since you are using Linux (Doge).
ssh-keygen -t rsa -b 4096 -C "Hexo Deploy Key" -f github-deploy-key -N ""
This will generate two files in the current directory:
github-deploy-key —— private key
github-deploy-key.pub —— public key
We put the private key into the code repository where we store the original Hexo files, which is used to trigger Actions.
Put the public key into the corresponding repository of GitHub Pages and enable write permission for Actions to write during Hexo deployment.
Configure the private key:
- Open the repository where Hexo is saved on GitHub, and go to Settings -> Secrets -> New secret.
- In the Name section, enter: HEXO_DEPLOY_KEY. Pay attention to the case, which will be used by GitHub Actions later. Make sure not to make any mistakes.
- In the Value section, enter the content of github-deploy-key.
Configure the public key:
- Next, we need to access the repository where the web pages are stored, which is the repository after Hexo deployment, such as Username.github.io. Go to Settings -> Deploy keys.
- Click Add deploy key to add a new public key.
- Enter HEXO_DEPLOY_PUB in the Title field, or you can enter any other custom name. This is just a remark.
- Paste the content of the github-deploy-key.pub file into the Key field.
- Check Allow write access to enable write permission. Otherwise, it will fail to write to the repository during deployment.
Step.4 Create Workflow#
First, create a new file .github/workflows/push.yml in the Hexo repository. You can choose your own file name, but it must be placed in the .github/workflows directory. You can also use the new feature of Action to create the file. The content of the file is as follows:
name: Deploy Site
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout@v1
- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Configure environment variables
env:
HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
run: |
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name "yzsong06"
git config --global user.email "[email protected]"
- name: Install deployment runtime environment
run: |
npm i -g hexo-cli
npm i hexo-deployer-git --save
npm i
- name: Generate static site files
run: |
hexo clean && hexo generate
- name: Deploy site using hexo-deployer
run: |
hexo deploy
Step.5 Completion#
After saving the file, you can generally see the feedback results on the Actions page of the repository. If the job has a green check mark, it means that the deployment is successful!