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

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

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

Github Actions自动生成Hugo站点并部署到Github Pages_Github

建立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.cnThe 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。

Github Actions自动生成Hugo站点并部署到Github Pages_Github_02

 

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

Github Actions自动生成Hugo站点并部署到Github Pages_Github_03

 

设置Github Actions

为unixetc/hgsource.git设置Github Actions

Github Actions自动生成Hugo站点并部署到Github Pages_Github_04

 

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

name: Auto Build and Deploy  #actions名称,可自定义on:  push:    branches:      - masterjobs:  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 pushEnumerating objects: 360, done.Counting objects: 100% (360/360), done.Delta compression using up to 4 threadsCompressing 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 0remote: Resolving deltas: 100% (3/3), done.To https://github.com/unixetc/hgsource.git * [new branch]      master -> master
Push完后,即可触发github actions,生成后的站点可以通过https://unixetc.github.io查看。