Blog of Li

个人Blog 一年更新约一次

0%

个人Blog的搭建方案(3) 自动构建-续

继续上次体验了Travis CI和Netifly的自动构建,感觉这个非常有意思,所以又尝试了两个自动构建工具,Azure Pipeline和Github Actions.

Azure Pipeline

Build Status

Azure是微软旗下的开发平台/工具,其中的自动构建就是Azure Pipeline. Github支持私有仓库的一个项目,以及1800分钟构建时间,和公开仓库的10个项目和1800分钟构建时间。对于私有仓库的构建,还是非常适合的。虽然也是yaml文件配置,但是和Travis CI的配置文件格式并不通用,需要简单的改一改才能用。

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
# azure-pipelines.yml
trigger:
- docs

pool:
vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'

- script: |
npm install -g hexo-cli
npm install
displayName: 'npm install and build'

- script: |
wget https://github.com/jgm/pandoc/releases/download/2.9/pandoc-2.9-1-amd64.deb
sudo dpkg -i ./pandoc-2.9-1-amd64.deb
displayName: 'Install pandoc'

- script: |
git submodule update --init --recursive
hexo g
displayName: 'Building'

- script: |
cd ./public
git init
git config user.name "lizhengyi"
git config user.email "[email protected]"
git add .
git commit -m "Update blog content by Azure Pipeline"
git push --force --quiet "https://$(GH_TOKEN)@github.com/li-positive-one/blog.git" master:master
displayName: 'Deploying'

Github Action

Actions Status

搞了这么多构建工具,才发现GIthub已经推出了自己的构建工具,之前在测试阶段,2019年11月正式开放。我何必舍近求远,用其他的工具呢,一站式完成网站岂不是更好。但是Github Action的配置也是最耗费我时间的。

在配置中,遇到的问题就是使用之前的push方法,也就是Personal access tokens不起作用。调试了好久,也不知道是哪里的问题,所以干脆换了套方法。生成了一套ssh-key,把公钥写入repo的Deploy keys并启用write权限,把私钥写入Secrets,新建一个条目,然后就可以在构建的配置文件里把这个变量写入到.ssh里,从而实现对repo的写入。这种方式虽然有点复杂,但是这是更加安全的方式,因为Deploy keys只涉及到这个仓库的权限,而Personal access tokens并不能细化到各个仓库的权限,一旦泄露,所有的仓库都有危险。

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
42
43
44
45
46
47
48
49
50
51
52
53
54
name: Hexo

on:
push:
branches:
- docs
jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v1
- name: Checkout submodules
run: |
git submodule update --init --recursive
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install pandoc
run: |
wget https://github.com/jgm/pandoc/releases/download/2.9/pandoc-2.9-1-amd64.deb
sudo dpkg -i ./pandoc-2.9-1-amd64.deb
- name: npm install, build, and test
run: |
npm install -g hexo-cli
npm install
env:
CI: true
- name: building
run: |
hexo g
- name: set ssh
env:
ACTION_DEPLOY_KEY: ${{ secrets.GH_KEY }}
run: |
# set up private key for deploy
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
- name: deploying
run: |
cd ./public
git init
git config user.name "lizhengyi"
git config user.email "[email protected]"
git add .
git commit -m "Update blog content by Github Actions"
git push --force --quiet [email protected]:li-positive-one/li-positive-one.github.io.git master:master