为了账号安全,请及时绑定邮箱和手机立即绑定

使用GitLab CI持续集成并自动FTP同步

标签:
Git

坚持不懈的寻找方案终于有了结果——使用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,一切恢复正常。

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

图片

[2020年01月13日原始发布于本作者博客]

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
0
获赞与收藏
0

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消