GITHUB ACTION test and guideline

经过10次测试:test123456789/10/,终于在github远程部署成功,此为记录。

由于经常往返鹿特丹-阿姆,在火车上的时光难免想写点东西。但是博客本地部署在家里的电脑上,写起来难免觉得别扭,了解到github可以自动进行部署,于是说干就干:

参考资料:

[1] https://isedu.top/index.php/archives/144/
[2] Claude 3.7 sonnet
[3] Manus

最难搞定的点 Troubleshooting

  1. github workflow的设置:需要生成一个public key和private key 分别放在github page 和private repo;
  2. Make sure your GitHub Pages repository is configured to deploy from the correct branch: 这个太容易忽略了,导致需要pull request,其实直接就可以merge到github page;
  3. If you’re using custom themes or plugins, ensure they’re properly included in your repository:这个是最坑爹的,直接upload本地的theme,可能存在不同的兼容性问题,workflow各种报错,最终解决办法是直接:
1
clone repo:https://github.com/silkiller/hexo-theme-oasis_updated.git

如果原始的theme结构需要更改,可以直接fork到自己的repo,相当于拿到theme的源代码,直接在源代码上修改,然后同步到github page中。
5. version Management:版本管理,最好用老一点的node版本,比如16,新版本存在一些兼容性问题。

Simplified Guide: Deploying Hexo Blog with GitHub Actions

This guide has been customized to meet your specific requirements:

  1. Creating a private repository to store your Hexo blog source files
  2. Setting up automatic deployment whenever you add or update content in that repository

Step 1: Create a Private Source Repository

  1. Go to GitHub and click on the “+” icon in the top-right corner, then select “New repository”.

  2. Name your repository something like hexo-blog-source or any name you prefer.

  3. Set the repository to “Private” to keep your drafts and source files protected.

  4. Click “Create repository” without adding any files yet.

  5. Push your local Hexo blog files to this new repository (Or Manually upload)

Step 2: Set Up SSH Keys for Deployment

For GitHub Actions to deploy to your GitHub Pages repository, you need to set up SSH keys:

  1. Generate a new SSH key pair:
1
ssh-keygen -t rsa -b 4096 -C "youngfor823@gmail.com" -f github-actions-deploy

This will create two files: github-actions-deploy (private key) and github-actions-deploy.pub (public key).

  1. Add the public key to your GitHub Pages repository (silkiller.github.io):

    • Go to your GitHub Pages repository
    • Navigate to “Settings” > “Deploy keys”
    • Click “Add deploy key”
    • Give it a title like “GitHub Actions Deploy Key”
    • Paste the contents of the github-actions-deploy.pub file
    • Check the “Allow write access” box
    • Click “Add key”
  2. Add the private key to your source repository:

    • Go to your source repository
    • Navigate to “Settings” > “Secrets and variables” > “Actions”
    • Click “New repository secret”
    • Name the secret DEPLOY_KEY
    • Paste the contents of the github-actions-deploy file (the private key)
    • Click “Add secret”

Step 3: Create the GitHub Actions Workflow

  1. In your private source repository, create a new directory structure .github/workflows/ if it doesn’t already exist.

  2. Create a new file named deploy.yml in the .github/workflows/ directory with the following content:

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
55
56
57
58
59
60
61
62
63
64
65
66
name: Deploy Hexo Blog

on:
push:
branches:
- master # **非常关键!!master, depending on your default branch name**
paths:
- 'source/**' # Trigger only when files in the source directory change
- 'themes/**' # Or when theme files change
- '_config.yml' # Or when config changes

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
with:
submodules: true # Fetch submodules (themes, etc.)
fetch-depth: 0 # Fetch all history for .git info

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16' # Use the Node.js version you're using locally

- name: Cache dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: Install dependencies
run: |
npm install -g hexo-cli


- name: Install landscape theme
run: |
# Check if theme is already installed
if [ ! -d "themes/landscape" ]; then
git clone https://github.com/silkiller/hexo-theme-oasis_updated.git themes/oasis
fi

# Install theme dependencies
cd themes/oasis
npm install
cd ../..


- name: Generate static files
run: hexo clean && hexo generate

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
deploy_key: ${{ secrets.DEPLOY_KEY }}
external_repository: silkiller/silkiller.github.io # Your GitHub Pages repository
publish_branch: master # The branch where your site is published,同步更改此处branch
publish_dir: ./public # Directory with the generated content
commit_message: ${{ github.event.head_commit.message }} # Use the same commit message
full_commit_message: ${{ github.event.head_commit.message }}
user_name: 'github-actions[bot]'
user_email: '41898282+github-actions[bot]@users.noreply.github.com'

Note the on.push.paths section which ensures the workflow only triggers when you make changes to your blog content, themes, or configuration.

Step 5: Adding New Blog Posts

Now, whenever you want to add a new blog post:

  1. Create your new post file in the source/_posts directory of your private repository.

  2. You can do this directly on GitHub by:

    • Navigating to your private repository
    • Going to the source/_posts directory
    • Clicking “Add file” > “Create new file”
    • Naming it with the .md extension
    • Adding your content with proper front matter
  3. Once you push the changes, GitHub Actions will automatically:

    • Detect the change in the source/_posts directory
    • Build your Hexo blog
    • Deploy it to your GitHub Pages repository