目标 把 blog 源码放在一个仓库,之后只要 push 内容到该仓库,就用 GitHub action 自动构建博客,并把 public 目录下的内容推送到 user.github.io 仓库
开始 Workflow 首先,在 GitHub 创建一个名为 blog 的仓库,用来放博客源码。当然也可以是其他名字。
然后,在 blog 仓库下创建 .github/workflows/gh-pages.yml 文件。添加以下内容:
Hugo(点击展开)
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 name: GitHub Pages on: push: branches: - main pull_request: jobs: deploy: runs-on: ubuntu-20.04 concurrency: group: ${{ github.workflow }}-${{ github.ref }} steps: - uses: actions/checkout@v3 with: submodules: true fetch-depth: 0 - name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: '0.101.0' extended: true - name: Build run: hugo --minify - name: Deploy uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} external_repository: XXiaoA/XXiaoA.github.io publish_branch: main publish_dir: ./public commit_message: ${{ github.event.head_commit.message }}
Hexo(点击展开)
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 name: Deploy on: push: branches: - main workflow_dispatch: jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout Source (Private Repo) uses: actions/checkout@v4 with: submodules: recursive - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: "20" cache: 'npm' - name: Install Dependencies run: npm ci - name: Build Hexo run: | npx hexo clean npx hexo generate - name: Deploy to Public Repo uses: peaceiris/actions-gh-pages@v4 with: deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} external_repository: publish_branch: main publish_dir: ./public commit_message: "Site updated: ${{ github.event.head_commit.message }} "
可以自己做一些修改来满足个性化需求。记得把 external_repository 改为自己仓库的名字
Token 在终端运行以下命令
1 ssh-keygen -t rsa -b 4096 -C "hexo-deploy"
不出意外,会有两个文件生成:
接着打开 GitHub 的 blog 仓库,点击 Settings -> Secrets and variables -> Actions 添加你的私钥,命名为 ACTIONS_DEPLOY_KEY
然后点开 user.github.io 仓库。进入设置点击 Deploy keys 把你的公钥添加进去。记得点击 Allow write access 选项。
然后就大功告成了!🥳
References