堅持不懈的尋找方案終於有了結果——使用GitLab CI持續集成並自動部署到FTP。
這樣減少了相當多的人力工作。
主要分爲兩個過程
在介紹過程前還有一個更簡單方便的方法可以先告訴不喜歡倒騰的,那就是IIS7服務器管理工具。
IIS7服務器管理工具可以批量管理、定時上傳下載、同步操作、數據備份、到期提醒、自動更新。IIS7服務器管理工具適用於Windows操作系統和liunx操作系統;支持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/" |
大致流程是先安裝lftp工具,再上傳。特別注意,這裏爲了不在代碼中暴露關鍵信息,使用了$USERNAME
$PASSWORD
$HOST
來獲取CI/CD中提前配置好的變量值。