Git与Gitlab CI持续集成

Git是当前非常流行的版本管理工具,很多主流的大公司都在使用,而Gitlab是一款非常流行的Git托管工具,所以Git与Gitlab的结合是必然,而CI、CD是Gitlab必须支持的功能。

CI与CD是什么?

  • CI(Continuous Integration)
    把代码集成到一个共享的仓库,并且每次代码发生变化自动执行构建、测试的流程。
  • CD(Continuous Delivery & Continuous Deployment)
    自动化的把代码发布到一个环境,当代码发生生变化,自动更新发布的环境。

Gitlab CI

GitLab CI是Gitlab托管工具提供对Git的CI/CD的支持,主要有一些特性:

  • 支持多语言、多平台
  • 并行执行,多个构建放在多台机器上执行
  • 实时log,很方便调试和跟踪
  • 支持流水线
  • 版本化管道
  • 支持docker
  • 支持docker镜像仓库
  • 自动扩展
  • 保护变量
  • 多环境支持

GitLab Runner

执行具体的构建是有Gitla Runner来完成,可以注册不同的Runner来完成不同构建,具体参考https://docs.gitlab.com/runner/install/

.gitlab-ci.yml

.gitlab-ci.yml是用来描述构建的过程。 具体的一个例子如下:

stages:
  - install_deps
  - test
  - build
  - deploy_test
  - deploy_production

cache:
  key: ${CI_BUILD_REF_NAME}
  paths:
    - node_modules/
    - dist/

# 安装依赖
install_deps:
  stage: install_deps
  only:
    - develop
    - master
  script:
    - npm install

# 运行测试用例
test:
  stage: test
  only:
    - develop
    - master
  script:
    - npm run test

# 编译
build:
  stage: build
  only:
    - develop
    - master
  script:
    - npm run clean
    - npm run build:client
    - npm run build:server

# 部署测试服务器
deploy_test:
  stage: deploy_test
  only:
    - develop
  script:
    - pm2 delete app || true
    - pm2 start app.js --name app

# 部署生产服务器
deploy_production:
  stage: deploy_production
  only:
    - master
  script:
    - bash scripts/deploy/deploy.sh


GitLab CI可以很好支持以Git中心的CI/CD, 特别是对docker支持很好,很灵活,对应容器化的实践是一个很好的选择。

Git refusing to merge unrelated histories
GITOPS的容器化是什么?
ajax-loader