Preface#
Some time ago, I switched from Hexo to WordPress, used it for a year, and finally switched back to Hexo. However, the steps to push Hexo to the server were too cumbersome, so I wrote a workflow to automate this step.
Github Action#
GitHub Actions is a platform for continuous integration and continuous delivery that allows you to automate your build, test, and deployment processes.
GitHub provides virtual machines for Linux, Windows, and macOS to run your workflows, or you can host your own self-hosted runners in your data center or cloud infrastructure.
Advantages#
- Supports multi-domain and multi-site deployment
- No issues with connecting to GitHub
- Can be automatically deployed
- No longer requires complex commands
Workflow#
Here I share my workflow. After writing an article, submitting it to the repository will automatically build and update it. After deploying the workflow, you can use GitHub Pages, Netlify, Vercel, Cloudflare Pages, etc., to achieve multi-line and multi-domain.
Prerequisites#
First, create repository A, which will be used to store the web files generated by Hexo. The format can be xxx.github.io
. Then create repository B (recommended to change to private
) and upload the blog source code to the repository. Find Actions, click "set up a workflow yourself" to create a new workflow.
Enter the following code:
name: Automatic Deployment
# Trigger Action when changes are pushed to the main branch
on:
push:
branches:
- main
# Change according to your repository situation (fill in the branch where the source code is located)
release:
types:
- published
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Check Branch
uses: actions/checkout@v2
with:
ref: main
fetch-depth: 0
- name: Sync local file timestamps
run: |
git ls-files -z | while read -d '' path; do touch -d $(git log -1 --format="@%ct" "$path") "$path"; done
- name: Install Node
uses: actions/setup-node@v1
with:
node-version: "16.x"
- name: Install Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g
- name: Cache Hexo
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
run: |
npm install gulp-cli -g # Install gulp globally
npm install --save
- name: Generate Static Files
run: |
hexo clean
hexo generate
gulp
- name: Deploy to Github
run: |
cd ./public
git init
git config --global user.name "xxx"
git config --global user.email "xxx@xxx.com"
git add .
git commit -m '${{ github.event.head_commit.message }}'
git push --force --all https://user:token@github.com/user/user.github.io
- Note!⚠️ Replace xxx in git config --global user.name "xxx" with your GitHub username (not your nickname).
- Note!⚠️ Replace xxx@xxx.com in git config --global user.email "xxx@xxx.com" with your GitHub
primary email
. - Note!⚠️ In git push --force --all https://user:token@github.com/user/user.github.io, replace the first user with your username, and token with your GitHub token. If you don't have one, please create it on the
settings
page. Then change theuser
after github.com to your username, and changeuser.github.io
to the name of the repository where you will store your blog web pages.
Additional#
Adding Ignore Items#
Since the contents that can be installed using commands are not included in the source code that needs to be submitted, we need to add these contents to the ignore list to indicate that they should not be uploaded to GitHub. This can significantly reduce the number of files to be submitted and speed up the submission process. Open .gitignore
(create one if it doesn't exist) and enter the following content:
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/
.deploy_git*/
.idea
themes/butterfly/.git
The last line should be changed to the actual .git location, as I am using the butterfly theme, and different theme folder names vary! If you are not using the butterfly theme, remember to replace the last line with the theme you are currently using.
Checking Deployment Status#
Open the private repository on GitHub where the source code is stored, and find Actions.
Find the corresponding deployment log based on the recent Commit records, click Deploy to check the deployment status.
If everything is checked, congratulations, you can now enjoy automatic deployment.
This article is synchronized and updated to xLog by Mix Space. The original link is https://blog.alcex.cn/posts/Hexo/2023429a1