持续集成部署博客脚本

持续集成(Continuous integration,简称 CI)部署 Hexo 博客比起每次都手动构建部署方便很多,现在都是本地用 GitKraken 把更新的博客源文件推送到 GitHub 仓库,然后由 GitHub Actions 进行后续的构建、部署操作,同时还可以把运行结果推送到 Telegram。这里分享几个我之前用过的持续集成脚本。

1. GitHub Actions

deployment.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
##################################
# Aliyun OSS
##################################
- name: Setup Aliyun oss util
uses: manyuanrong/[email protected]
with:
endpoint: ${{ secrets.OSS_ENDPOINT }}
access-key-id: ${{ secrets.OSS_KEY_ID }}
access-key-secret: ${{ secrets.OSS_KEY_SECRET }}

- name: Deploy html to oss
env:
OSS_BUCKET: ${{ secrets.OSS_BUCKET }} bucket name without endpoint domain
run: |
rm -rf public/css/ public/js/ public/images/
ossutil rm -rf oss://$OSS_BUCKET/
ossutil cp -rf public/ oss://$OSS_BUCKET/


##################################
# Tencent Cloud COS
##################################

- name: Setup Python
uses: actions/setup-python@v1
with:
python-version: '3.x'

- name: Get pip cache
id: pip-cache
run: |
python -c "from pip._internal.locations import USER_CACHE_DIR; print('::set-output name=dir::' + USER_CACHE_DIR)"

- name: Get coscmd requirements
run: wget https://raw.githubusercontent.com/tencentyun/coscmd/master/requirements.txt

- name: Pip cache
uses: actions/cache@v1
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Setup coscmd
env:
SECRET_ID: ${{ secrets.COS_SECRET_ID }}
SECRET_KEY: ${{ secrets.COS_SECRET_KEY }}
BUCKET: ${{ secrets.COS_BUCKET }} #bucket name with qcloud_user_id
REGION: ${{ secrets.COS_REGION }} #eg. ap-shanghai
run: |
pip install -U coscmd
coscmd config -a $SECRET_ID -s $SECRET_KEY -b $BUCKET -r $REGION -m 30

- name: Deploy assets to cos
run: |
coscmd upload -rs --delete public/css assets/css-blog -f
coscmd upload -rs --delete public/js assets/js -f
coscmd upload -rs --delete source/images assets/images -f
coscmd upload -rs --delete source/live2d assets/live2d -f
rm -rf public/css public/js public/images public/live2d


##################################
# Deploy to Github
##################################
- name: Deploy to Github
env:
GIT_NAME: lei2rock
GIT_EMAIL: ${{ secrets.GIT_EMAIL }}
REPO: github.com/lei2rock/blog
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
cd ./public && git init && git add .
git config --global user.name $GIT_NAME
git config --global user.email $GIT_EMAIL
git commit -m "Site deployed by GitHub Actions"
git push --force --quiet "https://$GH_TOKEN@$REPO" master:master

2. Netlify

netlify.toml
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
# Settings in the [build] context are global and are applied to all contexts 
# unless otherwise overridden by more specific contexts.
[build]
# Directory to change to before starting a build.
# This is where we will look for package.json/.nvmrc/etc.
base = "/"

# Directory (relative to root of your repo) that contains the deploy-ready
# HTML files and assets generated by the build. If a base directory has
# been specified, include it in the publish directory path.
publish = "/public"

# Default build command.
command = "hexo generate --silent"

[[redirects]]
from = "https://lei2rock-blog.netlify.com/*"
to = "https://blog.dlzhang.com/:splat"
status = 301
force = true

[[redirects]]
from = "/cloud/*"
to = "https://bucket-name.file.myqcloud.com/:splat"
status = 200
force = true

3. Travis CI

.travis.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
33
34
language: node_js
node_js: stable

branches:
only:
- src

cache:
yarn: true
directories:
- node_modules

before_install:
- export TZ='Asia/Shanghai'

install:
- yarn global add hexo-cli
- yarn

script:
- hexo generate

after_success:
- git config --global user.name $GIT_NAME
- git config --global user.email $GIT_EMAIL
- sed -i "s/GH_TOKEN/$GH_TOKEN/g" ./_config.yml
- hexo deploy -m "Travis CI Build ${TRAVIS_BUILD_NUMBER}"

notifications:
email:
recipients:
- $GIT_EMAIL
on_success: change # default: change
on_failure: always # default: always