文章目录
  1. 1. 持续集成
  2. 2. 自动部署
  3. 3. 踩过的坑

坚持不懈的寻找方案终于有了结果——使用GitLab CI持续集成并自动部署到FTP。

这样减少了相当多的人力工作。

主要分为两个过程:

持续集成

第一步很关键,但是也很简单。创建GitLab私有仓库以后,在项目根目录添加.gitlab-ci.yml配置文件,以便上传代码后GitLab CI使用。

其中有两个关键点。第一个是标记expire_in,不然会自动删除的。第二个是我这里配置了分支是master,这个依自己情况而定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/hexo
image: node:12.14.1

cache:
paths:
- node_modules/

before_script:
- npm install hexo-cli -g
- test -e package.json && npm install
- hexo generate

pages:
script:
- hexo generate
artifacts:
expire_in: 3 days # <== !!!
paths:
- public # <== 每次会将生成的 public 文件夹当成附件,保存起来
only:
- master

自动部署

自动部署这个折腾了很久。先描述过程,后面再说自己挖的坑。

增加FTP服务器配置后,.gitlab-ci.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
# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/hexo
image: node:12.14.1

cache:
paths:
- node_modules/

before_script:
- npm install hexo-cli -g
- test -e package.json && npm install
- hexo generate

pages:
script:
- hexo generate
- apt-get update -qq && apt-get install -y -qq lftp
artifacts:
expire_in: 3 days # <== !!!
paths:
- public # <== 每次会将生成的 public 文件夹当成附件,保存起来
only:
- master
after_script:
- lftp -c "set ftp:ssl-allow no; open -u $USERNAME,$PASSWORD $HOST; mirror -Rev public/ ./ --ignore-time --parallel=10 --exclude-glob .git* --exclude .git/"

上述配置已更新为下面内容 (相关链接 解决Hexo使用GitLab持续集成部署阿里云虚机lftp无法全部完成的问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/hexo
image: node:12.14.1

cache:
paths:
- node_modules/

before_script:
- npm install hexo-cli -g
- test -e package.json && npm install
- hexo generate

pages:
script:
- apt-get update -qq && apt-get install -y -qq lftp
- lftp -c "set ftp:ssl-allow false; set ftp:ignore-pasv-address true; set ftp:prefer-epsv no; set ftp:charset gbk; set file:charset utf-8; debug; open -u $USERNAME,$PASSWORD $HOST; mirror -R public/ ./htdocs --verbose -p --ignore-time --transfer-all --parallel=5 --exclude-glob .git* --exclude .git/"
artifacts:
expire_in: 3 days
paths:
- public
only:
- master

大致流程是先安装lftp工具,再上传。特别注意,这里为了不在代码中暴露关键信息,使用了$USERNAME $PASSWORD $HOST来获取CI/CD中提前配置好的变量值。

踩过的坑

这个坑嘛,估计无人能懂。一开始部署的时候,觉得好像没有什么问题,但是最后生成的文件都是0kb。想着应该能有输出吧。

上CI/CD日志一看,一脸懵逼,居然说没有layout,那指定是主题出问题了。

突然想到自己的主题原来是拿去做了开源项目给大家使用的,一检查,原来是作为submodule放在这个项目中的,所以没有上传到仓库去。最后依照这篇文章的方法去掉了submodule,一切恢复正常。
https://www.jianshu.com/p/7acb3a2fc05c

世上本没有坑,倒腾的次数多了,自然就挖好了坑。

♦ 本文固定连接:https://www.gsgundam.com/archive/2020-01-14-gitlab-ci-hexo-auto-deploy/

♦ 转载请注明:GSGundam 2020年01月13日发布于 GSGUNDAM砍柴工

♦ 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

♦ 原创不易,如果页面上有适合你的广告,不妨点击一下看看,支持作者。(广告来源:Google Adsense)

♦ 本文总阅读量