使用hugo建立建立个人网站可以参考Hugo安装使用实例

使用github pages来部署个人网站可以参考GithubPages部署免费网站

下面将会介绍如何通过Github Actions来将以上两个操作关联在一起并自动化完成!

自动化流程图

建立Repositories

建立一个unixetc/ghsource.git属性为私有(private)的用来放置Hugo源码,然后再建一个unixetc/unixetc.gihub.io.git属性为公有(public)并设置Github Pages服务。

设置Deploy keys与Secrets

使用ssh-keygen命令来生成私钥与公钥。

$ ssh-keygen.exe -t rsa -b 4096 -C "alairs@live.cn"  #邮箱自定义
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/alair/.ssh/id_rsa):   #存放目录,可自定义
Created directory '/c/Users/alair/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/alair/.ssh/id_rsa  #私钥
Your public key has been saved in /c/Users/alair/.ssh/id_rsa.pub  #公钥 
The key fingerprint is:
SHA256:ZiNPDGtAKC6MA alairs@live.cn
The key's randomart image is:
+---[RSA 4096]----+
|      ..  +o.    |
|     .  .  o..   |
|  .   +  .... .  |
|o+ o o =  .o .   |
|+EO o = S . .    |
|o  B . % + .     |
| .o .oB *        |
| ...++ B         |
| ... .. o        |
+----[SHA256]-----+

unixetc/unixetc.gihub.io.git的设置中将公钥内容添加至Deploy keys,名称自定义,并勾选allow write access

deploykey

unixetc/hgsource.git的设置中将私钥内容添加至Secrets,命名为ACTIONSDEPLOYKEY,这个后面要用到。 Secrets

设置Github Actions

unixetc/hgsource.git设置Github Actions

github_actions

在新建的hgsource/.github/workfows/main.yml中填入以下内容:

name: Auto Build and Deploy  #actions名称,可自定义

on:
  push:
    branches:
      - master

jobs:
  build-deploy:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2  
       # with:
       #   submodules: true

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

      - name: Build Hugo Site
        run: hugo --minify        #生成站点

      - name: Deploy to GitHub pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          deploy_key: ${{ secrets.ACTIONSDEPLOYKEY }} # 同上面私钥(Private Key)变量名
          external_repository: unixetc/unixetc.github.io # Github Pages远程地址
          publish_dir: "./public" #推送目录
          keep_files: false # 删除已存在文件
          publish_branch: master  # 推送
          commit_message: ${{ github.event.head_commit.message }}

完成后保存即可。

自动生成并部署

unixetc/ghsource.git Clone下来。

$git clone https://github.com/unixetc/hgsource.git

参考Hugo安装使用实例生成站点。

$git add .
$git commit -m "update"
$ git push
Enumerating objects: 360, done.
Counting objects: 100% (360/360), done.
Delta compression using up to 4 threads
Compressing objects: 100% (355/355), done.
Writing objects: 100% (360/360), 3.09 MiB | 1.13 MiB/s, done.
Total 360 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/unixetc/hgsource.git
 * [new branch]      master -> master

Push完后,即可触发Github Actions,生成后的站点可以通过https://unixetc.com查看。