发现问题

上次从hexo迁移到hugo后使用了别人的代码自动构建上传代码,运行正常,但是有两个小问题

  • workflow文件中直接写明了腾讯云的部署密钥,这样的话博客只能使用github的私有仓库储存
  • 抄过来的代码写的是用go编译安装hugo,这样拖慢了一次构建的时间

附上以前的代码

 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
name: Deloy to COS

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up Go
      uses: actions/setup-go@v1.0.0
      with:
        version: 1.13
    - name: Install Hugo
      run: |
        export GOPATH=$HOME
        mkdir $HOME/src
        cd $HOME/src
        git clone https://github.com/gohugoio/hugo.git
        cd hugo
        go install
    - name: Set up Python
      uses: actions/setup-python@v1
      with:
        python-version: 3.6
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
    - name: Install coscmd
      run: |
        pip install coscmd
    - name: deploy
      run: |
        $HOME/bin/hugo
        coscmd config -a **腾讯云AccessId** -s **腾讯云SecretKey** -b **桶名称** -r **桶地区** -m 10
        coscmd upload -r -s public/ /

解决问题

在workflow文件里面使用了secret环境变量

在项目的setting->Secrets中添加COS_SECRET_ID和COS_SECRET_KEY项,然后在代码中应用就行了。

Github Secrets
Github Secrets

代码片段如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    - name: deploy
      env:
        COS_BUCKET_NAME: blog-1251996892
        COS_BUCKET_LOCATION: ap-chengdu
      run: |
        export PATH=$HOME/hugo:$PATH
        git clone https://github.com/dillonzq/LoveIt.git ./themes/LoveIt
        hugo
        coscmd config -a ${{secrets.COS_SECRET_ID}} -s ${{secrets.COS_SECRET_KEY}} -b ${COS_BUCKET_NAME} -r ${COS_BUCKET_LOCATION} -m 10
        coscmd upload -r -s public/ /

加快构建速度

这里把go编译的部分去掉,直接使用hugo项目主页编译好的二进制文件就行了。

1
2
3
4
5
- name: Install Hugo
  run: |
    wget -nv https://github.com/gohugoio/hugo/releases/download/v0.64.0/hugo_0.64.0_Linux-64bit.tar.gz
    mkdir $HOME/hugo
    tar -zxvf hugo_0.64.0_Linux-64bit.tar.gz -C $HOME/hugo

完整代码

 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
name: Deloy to COS
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Checkout
      uses: actions/checkout@v2

    - name: Install Hugo
      run: |
        wget -nv https://github.com/gohugoio/hugo/releases/download/v0.64.1/hugo_extended_0.64.1_Linux-64bit.tar.gz -O hugo.tar.gz
        mkdir $HOME/hugo
        tar -zxvf hugo.tar.gz -C $HOME/hugo

    - name: Set up Python
      uses: actions/setup-python@v1
      with:
        python-version: 3.6

    - name: Install coscmd
      run: |
        pip install coscmd

    - name: deploy
      env:
        COS_BUCKET_NAME: blog-1251996892
        COS_BUCKET_LOCATION: ap-chengdu
      run: |
        export PATH=$HOME/hugo:$PATH
        git clone https://github.com/dillonzq/LoveIt.git ./themes/LoveIt
        hugo
        coscmd config -a ${{secrets.COS_SECRET_ID}} -s ${{secrets.COS_SECRET_KEY}} -b ${COS_BUCKET_NAME} -r ${COS_BUCKET_LOCATION} -m 10
        coscmd upload -r -s public/ /