创建action

在blog根目录创建:

1
.github\workflows\gh-pages.yml

内容如下

 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
name: github pages

on:
  push:
    branches:
      - master  # Set a branch to deploy

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          # extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: p3p3pp3/p3p3pp3.github.io
          publish_branch: master  # default: gh-pages
          publish_dir: ./public

配置deploy_key

生成一组ssh-rsa秘钥; 在存储blog的私有仓库中新建名为ACTIONS_DEPLOY_KEY的Action secrets,将私钥复制进去; 在blog发布的github.io仓库中,添加Deploy Keys,将公钥复制进去;

完成

再次提交blog的内容更新,就能看到github action自动运行hugo进行生成并发布到xxx.github.io中了。

Deploy配置说明

⭐️ Deploy to external repository external_repository By default, your files are published to the repository which is running this action. If you want to publish to another repository on GitHub, set the environment variable external_repository to /.

For example:

1
2
3
4
5
6
7
- name: Deploy
  uses: peaceiris/actions-gh-pages@v3
  with:
    deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
    external_repository: username/external-repository
    publish_branch: your-branch  # default: gh-pages
    publish_dir: ./public

You can use deploy_key or personal_token. When you use deploy_key, set your private key to the repository which includes this action and set your public key to your external repository.

Note that GITHUB_TOKEN has no permission to access to external repositories. Please create a personal access token and set it to personal_token like personal_token: ${{ secrets.PERSONAL_TOKEN }}.

Use case:

A GitHub Free Plan account cannot use the GitHub Pages in a private repository. To make your source contents private and deploy it with the GitHub Pages, you can deploy your site from a private repository to a public repository using this option.

peaceiris/homepage: A private repository running this action with external_repository: peaceiris/peaceiris.github.io peaceiris/peaceiris.github.io: A public repository using GitHub Pages