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

2022/12/20 3:23:56

本文主要是介绍使用GitLab CI持续集成并自动FTP同步,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

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

主要分为两个过程:

持续集成

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

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

 1


 5


 9


13


17


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


 5


 9


13


17


21


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


 5


 9


13


17


21

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日原始发布于本作者博客]



这篇关于使用GitLab CI持续集成并自动FTP同步的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程