[Share Experiences] GitHub Actions自动部署hexo博客
Tofloor
poster avatar
青稚
deepin
2024-01-22 10:45
Author

准备

首先电脑部署生成hexo博客,参考安装npm和搭建hexo博客

开始教程

前提

node.js环境和git都已安装且hexo可以正常运行与部署。

注意文章里面的master是仓库分支名,根据自身情况修改main或其他分支名。

1.改_config.yml 文件中的deploy为如下内容:

deploy:
  type: git
  repo: [email protected]:用户名/Pages仓库名.git
  branch: master

生成key,github部署时需要:

ssh-keygen -t rsa -b 4096 -C "Hexo Deploy Key" -f github-deploy-key -N ""

会在当前目录生成:

github-deploy-key —— 私钥

github-deploy-key.pub —— 公钥

新建一个博客仓库存放hexo源文件,把博客文件放到里面,之后设置博客代码仓库 Settings -> secrets and variables->Repository secrets

名字填写:

HEXO_DEPLOY_KEY (大写)

后面Actions会用到不要写错哦!

填入 github-deploy-key(私钥)里面的内容。

公钥放到Pages对应的代码仓库里面

访问pages对应的代码仓库 Settings -> Deploy keys->Add deploy key

名字:HEXO_DEPLOY_PUB( 可自定义)

在 Key 填入 github-deploy-key.pub(公钥)中的内容,别忘了Allow write access 勾上。

在博客代码仓库里点 Actions

然后创建一个新文件

.github/workflows/xxx.yml(xxx名字可自取)

内容如下:



name: Hexo Deploy



on:

   workflow_dispatch:

    branches:

      - master



jobs:

  build:

    runs-on: ubuntu-latest

    if: github.event.repository.owner.id == github.event.sender.id



    steps:

      - name: Checkout source

        uses: actions/checkout@v4

        with:

          ref: master



      - name: Setup Node.js

        uses: actions/setup-node@v4

        with:

          node-version: '20'



      - name: Setup Hexo

        env:

          ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}

        run: |

          sudo timedatectl set-timezone 'Asia/Shanghai'

          mkdir -p ~/.ssh/

          echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa

          chmod 700 ~/.ssh

          chmod 600 ~/.ssh/id_rsa

          ssh-keyscan github.com >> ~/.ssh/known_hosts

          git config --global user.email "改成你的邮箱"

          git config --global user.name "改成你的用户名"

          npm install hexo-cli -g

          npm install



      - name: Deploy

        run: |

          hexo clean

          hexo deploy
   

其中:node-version: '20',指nodejs版本。

master指分支,个人建议新建好仓库给main分支改名为master

结尾:别忘了给主题目录里面的.git目录删除,防止git推送失败。

git上传博客文件到github,这个懂得都懂,这里不讲解了。

这么做以后只需要上传.md文件,再手动点击Actions(Actions会自动在Actions内生成public目录,并把里面的全部文件上传到Pages仓库),舒适。

Reply Favorite View the author
All Replies
阿尼樱奈奈
Moderator
2024-01-22 10:49
#1

有空我试一下这个like

Reply View the author
yanjuner
Super Moderator
2024-01-22 10:57
#2

厉害了厉害了

Reply View the author
出售星辰之书的书商
Moderator
2024-01-22 11:21
#3

感谢分享 待会去试试

like

Reply View the author
爱开发
deepin
2024-01-22 13:37
#4

我补充一个 GitLab CI + Hugo

# 自托管方式

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  SSH_PRIVATE_KEY: $SSH_PRIVATE_KEY
  GHPROXY: $GHPROXY
  WEB_PORT: $WEB_PORT
  WEB_USER: $WEB_USER
  WEB_HOST: $WEB_HOST
  WEB_PATH: $WEB_PATH

image: registry.gitlab.com/pages/hugo/hugo_extended:latest

stages:
  - deploy

before_script: |
  if [[ "${GHPROXY:-}" = "YES" ]]; then
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.bfsu.edu.cn/g' /etc/apk/repositories
  fi

  apk add --no-cache openssh-client rsync bash

  mkdir -p public

deploy:
  stage: deploy
  script: |
    mkdir ~/.ssh
    echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
    chmod 600 ~/.ssh/id_rsa
    ssh-keyscan -p $WEB_PORT $WEB_HOST >> ~/.ssh/known_hosts
    eval $(ssh-agent -s)
    ssh-add <(echo "$SSH_PRIVATE_KEY")
    hugo --minify
    # npm install
    # npm run build
    rsync -avz -e "ssh -p $WEB_PORT -o StrictHostKeyChecking=no" --delete docs/* "${WEB_USER}@${WEB_HOST}:${WEB_PATH}/"
  only:
    - main

Reply View the author