堅持不懈的尋找方案終于有了結(jié)果——使用GitLab CI持續(xù)集成并自動部署到FTP。
這樣減少了相當(dāng)多的人力工作。
主要分為兩個過程:
持續(xù)集成
第一步很關(guān)鍵,但是也很簡單。創(chuàng)建GitLab私有倉庫以后,在項目根目錄添加.gitlab-ci.yml配置文件,以便上傳代碼后GitLab CI使用。
其中有兩個關(guān)鍵點。第一個是標(biāo)記expire_in,不然會自動刪除的。第二個是我這里配置了分支是master,這個依自己情況而定。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2122
# 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 文件夾當(dāng)成附件,保存起來
only:
- master
自動部署
自動部署這個折騰了很久。先描述過程,后面再說自己挖的坑。
增加FTP服務(wù)器配置后,.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
2425
# 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 文件夾當(dāng)成附件,保存起來
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/"
上述配置已更新為下面內(nèi)容 (相關(guān)鏈接 解決Hexo使用GitLab持續(xù)集成部署阿里云虛機(jī)lftp無法全部完成的問題 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2223
# 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工具,再上傳。特別注意,這里為了不在代碼中暴露關(guān)鍵信息,使用了$USERNAME $PASSWORD $HOST來獲取CI/CD中提前配置好的變量值。
踩過的坑
這個坑嘛,估計無人能懂。一開始部署的時候,覺得好像沒有什么問題,但是最后生成的文件都是0kb。想著應(yīng)該能有輸出吧。
上CI/CD日志一看,一臉懵逼,居然說沒有l(wèi)ayout,那指定是主題出問題了。
突然想到自己的主題原來是拿去做了開源項目給大家使用的,一檢查,原來是作為submodule放在這個項目中的,所以沒有上傳到倉庫去。最后依照這篇文章的方法去掉了submodule,一切恢復(fù)正常。http://www.itdecent.cn/p/7acb3a2fc05c
世上本沒有坑,倒騰的次數(shù)多了,自然就挖好了坑。

[2020年01月13日原始發(fā)布于本作者博客]
> 查看原文:https://www.gsgundam.com/archive/2020-01-14-gitlab-ci-hexo-auto-deploy/