gitlab-cihtml
gitlab-ci 全称是 gitlab continuous integration,也就是基于gitlab的持续集成工具。中心思想是当每一次push到gitlab的时候,都会触发一次脚本执行,而后脚本的内容包括了测试,编译,部署等一系列自定义的内容。高版本的 GitLab 自带了 GitLab CI,因此不须要另外安装。前端
GitLab-Runnernode
GitLab-Runner 是脚本执行的承载者,GitLab-CI 事先注册好 GitLab-Runner,再 push 代码,对应的 Runner 就会执行你所定义的脚本。git
本地push -> .gitlab-ci.yml配置 -> GitLab-Runner执行脚本 -> 部署开发、测试、生产服务器docker
Gitlab Runner安装方式有两种,一种是直接二进制文件安装,一种是基于docker镜像安装。后端
二进制文件安装缓存
sudo curl --output /usr/local/bin/gitlab-ci-multi-runner https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-ci-multi-runner-darwin-amd64
复制代码
sudo chmod +x /usr/local/bin/gitlab-ci-multi-runner
复制代码
以上是官方安装文档,若是有问题,能够手动到版本下载列表下载对应的版本,而后复制到/usr/local/bin/目录下bash
而后在终端输入服务器
启动Runner,gitlab-ci-multi-runner start
,刷新页面curl
咱们能够经过 gitlab-ci-multi-runner list 查询你注册的runner ,用 gitlab-ci-multi-runner status 查看 runner 服务是否运行中。
基于Docker安装
sudo docker pull gitlab/gitlab-runner:latest
复制代码
sudo docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
复制代码
sudo docker exec -it gitlab-runner gitlab-ci-multi-runner register
注册方式同上
复制代码
GitLab CI的一切工做,都是由 .gitlab-ci.yml 来配置的。详细文档能够参考这里
在项目根目录下建立 .gitlab-ci.yml 文件
# 设置缓存
cache:
paths:
- node_modules/
- dist/
# 定义 stages,用来定义工做阶段,多个 stages 会按顺序进行
stages:
- build
- test
- deploy
# before_script 会在每一个 stages 执行以前运行
before_script:
- node -v
- yarn --version
- yarn global add umi
- yarn install
# 测试(对应上面stages)
test:
stage: test
script:
- echo 'yarn test'
- yarn test
# 构建
build:
stage: build
script:
- echo 'yarn build'
- yarn build
when: manual # 手动触发
# 部署
deploy:
stage: deploy
only:
- master
script:
- bash scripts/deploy.sh # 部署脚本文件
when: manual
复制代码
配置完成提交Gitpab后,每次push都会触发gitlab-ci。