迁移到Github Action


迁移到``Github-Action

说明

博客很久没有维护更新了,每年也就春节前后得空才有时间写写总结,然后更新一下。由于博客直接部署了Travis CI持续集成,可以参考之前的文章[[2020-02-06-使用TravisCI持续集成]], 也是非常省心。可是今天突然发现不能使用了,一顿抓狂。
后来仔细看了下Travis CI的说明,原来是修改了使用规格,在免费额度使用完以后,就必须收费才可以使用。这也倒不奇怪,作为免费使用的业务,以及方便了非常多的开源项目进行持续集成。随着github推出官方的github action持续集成工具后,市场空间不断被压缩,为求生存,收费也是可以理解的。
不过,作为一个不愿花钱的个人而言,交钱买服务,实在是没有必要,毕竟也不便宜。所以今天得空,打算研究下github action 想办法完成切换。

切换步骤

参考文章:https://zhuanlan.zhihu.com/p/161969042

创建Github Persional Access Token

主要生成方法可以见[[2020-02-06-使用TravisCI持续集成#^b293ba]]

设置仓库Secrets

找到hexo-source这个源代码仓库,注意不是部署page的仓库,是源代码仓库。找到Settings,左边栏选择 Secrets and variables-->Actions,点击New repository secret进行添加。我这里把Name写为GIT_REPO_TOKEN,然后内容写上面一步申请到的Persional Access Token。为了保持和之前的内容同步,这里顺便也把codinggitee同步添加下。
汇总如下,一共添加了3个token,一个username

  • GIT_REPO_TOKEN
  • CODING_REOP_TOKEN
  • GITEE_REPO_TOKEN
  • CODING_USER_NAME

创建工作流脚本

hexo-source源码仓库下面新建目录.github/workflows,这是action脚本的目录,里面使用*.yml脚本,即采用YAML格式,文件名可以任意起,我这里就叫为myblogci.yml
具体的内容如下:

# workflow的名称
name: lmwings blog actions

# 触发条件:在 push 到 master 触发wrokflow
on:
  push:
    branches: 
      - master

env:
  TZ: Asia/Shanghai

#定义每一项job
jobs:
  lmwings-blog:
    name: lmwings blog build & deploy
    runs-on: ubuntu-latest # 使用最新的 Ubuntu 系统作为编译部署的环境

    steps:
    # step1: 拉取代码
    - name: Checkout codes
      uses: actions/checkout@v2 

    # step2: 设置 node.js 环境
    - name: Use Node.js 12.x
      uses: actions/setup-node@v1 
      with:
        node-version: '12.x'

    # step3: 设置包缓存目录,避免每次下载
    - name: Cache node modules
      uses: actions/cache@v2
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build -
            ${{ runner.os}}-

    # step4: 下载 hexo-cli 脚手架及相关安装包,其中travis_env_init.sh是自己定义的文件
    - name: Install hexo dependencies
      env: 
        GIT_REPO_TOKEN: ${{ secrets.GIT_REPO_TOKEN }}
        GITEE_REPO_TOKEN: ${{ secrets.GITEE_REPO_TOKEN }}
        CODING_REPO_TOKEN: ${{ secrets.CODING_REPO_TOKEN }}
        CODING_USER_NAME: ${{ secrets.CODING_USER_NAME }}    
      run: |
        sh travis_env_init.sh

    # step5: 编译
    - name: Build Generate files
      run: |
        hexo clean
        hexo generate

    # step6: 部署
    - name: Deploy lmwings blog
      # 通过hexo的deploy命令将编译后的blog推送到pages仓库
      run: |
        cd ./public && git init && git add .
        git config --global user.name "lmwings"
        git config --global user.email "1321559220@qq.com"
        hexo deploy > /dev/null

提交上面的脚本后,发现并没有运行成功,下面是遇到的问题:
问题1:后台查看action的运行日志,发现提升npm安装不成功

npm ERR! code EEXIST

[38](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:39)npm ERR! syscall symlink

[39](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:40)npm ERR! path ../lib/node_modules/hexo-cli/bin/hexo

[40](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:41)npm ERR! dest /opt/hostedtoolcache/node/12.22.12/x64/bin/hexo

[41](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:42)npm ERR! errno -17

[42](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:43)npm ERR! EEXIST: file already exists, symlink '../lib/node_modules/hexo-cli/bin/hexo' -> '/opt/hostedtoolcache/node/12.22.12/x64/bin/hexo'

[43](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:44)npm ERR! File exists: /opt/hostedtoolcache/node/12.22.12/x64/bin/hexo

[44](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:45)npm ERR! Remove the existing file and try again, or run npm

[45](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:46)npm ERR! with --force to overwrite files recklessly.

[46](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:47)

[47](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:48)npm ERR! A complete log of this run can be found in:

[48](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:49)npm ERR! /home/runner/.npm/_logs/2023-03-12T05_25_33_207Z-debug.log

[49](https://github.com/lmwings/hexo-source/actions/runs/4395938659/jobs/7698063505#step:5:50)Error: Process completed with exit code 239.

解决方式:
因为前面已经加载过node环境,所以下面这个不需要再安装

#注释掉下面这句
#npm install -g hexo

问题二:发现插件hexo-permalink-pyinyin安装存在问题

npm ERR! code E404

[106](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:107)npm ERR! 404 Not Found - GET [https://registry.npmjs.org/hexo-permalink-pyinyin](https://registry.npmjs.org/hexo-permalink-pyinyin) - Not found

[107](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:108)npm ERR! 404

[108](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:109)npm ERR! 404 'hexo-permalink-pyinyin@latest' is not in the npm registry.

[109](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:110)npm ERR! 404 You should bug the author to publish it (or use the name yourself!)

[110](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:111)npm ERR! 404

[111](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:112)npm ERR! 404 Note that you can also install from a

[112](https://github.com/lmwings/hexo-source/actions/runs/4395992399/jobs/7698144884#step:4:113)npm ERR! 404 tarball, folder, http url, or git url

解决方法:
暂时注释下下面的插件:

npm i hexo-permalink-pyinyin --save

但是我的博客文章名称一般都是中文的,所以去掉这个插件其实是不合适的。可以代替的插件npm install hexo-abbrlink --save,后续也可以使用这个插件。
进一步分析发现,action使用的默认源是registry.npmjs.org,而这个插件的作者只发布到了npmjs.com,所以应该只要修改下registry就可以。
为了统一,这里统一设置为阿里源,目前名字已经修改。同时使用cnpm进行安装,在这个源里面提前搜索了下,有这个插件存在。

npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm install hexo-permalink-pinyin

实际提交后,遇到了新的问题。

问题三:出现安装cnpm失败

+ cnpm@9.0.1

[346](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:347)added 672 packages from 246 contributors in 73.771s

[347](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:348)internal/modules/cjs/loader.js:818

[348](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:349) throw err;

[349](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:350) ^

[350](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:351)

[351](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:352)Error: Cannot find module 'fs/promises'

[352](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:353)Require stack:

[353](https://github.com/lmwings/hexo-source/actions/runs/4396290772/jobs/7698636976#step:4:354)- /opt/hostedtoolcache/node/12.22.1

发现这个是一个共性的问题,应该是cnpmnode版本匹配的问题,所以使用低版本的cnpm安装。
解决方法:安装低版本比如7.1.0版本

npm uninstall -g cnpm
npm install -g cnpm@7.1.0 --registry=https://registry.npmmirror.com
cnpm install hexo-permalink-pinyin

问题四:解决了上面的问题以后,还剩下最后一个问题

To [https://github.com/lmwings/lmwings.github.io.git](https://github.com/lmwings/lmwings.github.io.git)

[31](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:32) + e487ca3...08d0a07 HEAD -> master (forced update)

[32](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:33)To [https://e.coding.net/lmwings/lmwings.git](https://e.coding.net/lmwings/lmwings.git)

[33](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:34) + e487ca3...08d0a07 HEAD -> master (forced update)

[34](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:35)fatal: could not read Password for 'https://***@gitee.com': No such device or address

[35](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:36)FATAL Something's wrong. Maybe you can find the solution here: [https://hexo.io/docs/troubleshooting.html](https://hexo.io/docs/troubleshooting.html)

[36](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:37)Error: Spawn failed

[37](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:38) at ChildProcess. (/home/runner/work/hexo-source/hexo-source/node_modules/hexo-util/lib/spawn.js:51:21)

[38](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:39) at ChildProcess.emit (events.js:314:20)

[39](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:40) at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

[40](https://github.com/lmwings/hexo-source/actions/runs/4396352677/jobs/7698733700#step:6:41)Error: Process completed with exit code 2.

可以看到这个是部署过程中的异常,主要是提交到国内平台giteecoding出现了异常。之前为了国内访问速度快一些,特意做的分流出来,由于长时间没有维护,国内代码平台规则变更,导致的问题。这个只要重新申请对应平台的tocken就ok

总结和反思

总的来说,切换难度不大。遇到的问题都为本身软件包安装的问题。由于国内代码平台规则升级频繁,需要常常关注,否则就很容易出问题,后续打算同步部署到Vercel中。


文章作者: Wang Kang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Wang Kang !
评论
 上一篇
coding上部署静态页面 coding上部署静态页面
coding上部署静态页面官方教程:https://coding.net/help/docs/devops/ci/practice/static-website-paas.html 通过「持续集成」把 HTML 自动部署到「云存储」,变成静
2023-03-12
下一篇 
chatGPT注册和使用 chatGPT注册和使用
chatGPT注册和使用 https://openai.com/blog/chatgpt/ 注册需要美国手机号,同时登陆访问需要国外的ip地址 验证手机号准备可以通过在线打码平台进行。如sms-activate 直接注册需要事先准备好邮箱
2023-02-08
  目录