故事是这样的..
我们源码从github迁移到自主搭建的gitlab服务器管理,以前用github的时候是使用jenkins进行持续集成的,本来应用上jenkins我只要配一下webhook就可以了,可我就是任性。
我心想,既然已经迁移到gitlab了,为何不用用gitlab-ci呢,更何况gitlab宣称集成了gitlab-ci,应该很快就能应用上。
我正是这样把自己推进坑的。
名词解释
进坑前先理清一些名词,以及他们之间的关系。
1. Gitlab
GitLab是一个利用Ruby on Rails开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。
它拥有与GitHub类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。
2. Gitlab-CI
Gitlab-CI是GitLab Continuous Integration(Gitlab持续集成)的简称。
从Gitlab的8.0版本开始,gitlab就全面集成了Gitlab-CI,并且对所有项目默认开启。
只要在项目仓库的根目录添加.gitlab-ci.yml
文件,并且配置了Runner(运行器),那么每一次合并请求(MR)或者push都会触发CI pipeline。
3. Gitlab-runner
Gitlab-runner是.gitlab-ci.yml
脚本的运行器,Gitlab-runner是基于Gitlab-CI的API进行构建的相互隔离的机器(或虚拟机)。GitLab Runner 不需要和Gitlab安装在同一台机器上,但是考虑到GitLab Runner的资源消耗问题和安全问题,也不建议这两者安装在同一台机器上。
Gitlab Runner分为两种,Shared runners和Specific runners。
Specific runners只能被指定的项目使用,Shared runners则可以运行所有开启Allow shared runners
选项的项目。
4. Pipelines
Pipelines是定义于.gitlab-ci.yml
中的不同阶段的不同任务。
我把Pipelines理解为流水线,流水线包含有多个阶段(stages),每个阶段包含有一个或多个工序(jobs),比如先购料、组装、测试、包装再上线销售,每一次push或者MR都要经过流水线之后才可以合格出厂。而.gitlab-ci.yml
正是定义了这条流水线有哪些阶段,每个阶段要做什么事。
5. Badges
徽章,当Pipelines执行完成,会生成徽章,你可以将这些徽章加入到你的README.md文件或者你的网站。
徽章的链接形如:http://example.gitlab.com/namespace/project/badges/branch/build.svg
我们用gitlab项目的徽章作为例子:
安装配置
这里跳过Gitlab的安装,请自行谷歌。但是对于安装gitlab有一点提醒,就是建议使用官方推荐的集成安装包的方式安装,通过源码安装会有很多坑踩不完。
安装gitlab-ci-multi-runner
如果想要使用docker runner,则需要安装docker。(可选)
-sSL| sh``` 1
2因为docker需要linux内核在3.10或以上,安装前可以通过`uname -r`查看Linux内核版本。
2. 添加Gitlab的官方源:For Debian/Ubuntu
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
For CentOS
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
1 | 3. 安装 |
For Debian/Ubuntu
sudo apt-get install gitlab-ci-multi-runner
For CentOS
sudo yum install gitlab-ci-multi-runner1
24. 注册Runner
Runner需要注册到Gitlab才可以被项目所使用,一个gitlab-ci-multi-runner服务可以注册多个Runner。
$ sudo gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://mygitlab.com/ci
Please enter the gitlab-ci token for this runner
xxx-xxx-xxx
Please enter the gitlab-ci description for this runner
my-runner
INFO[0034] fcf5c619 Registering runner… succeeded
Please enter the executor: shell, docker, docker-ssh, ssh?
docker
Please enter the Docker image (eg. ruby:2.1):
node:4.5.0
INFO[0037] Runner registered successfully. Feel free to start it, but if it’s
running already the config should be automatically reloaded!1
25. 更新Runner
如果需要更新Runner,只需要执行以下脚本:
# For Debian/Ubuntu
sudo apt-get update
sudo apt-get install gitlab-ci-multi-runner
# For CentOS
sudo yum update
sudo yum install gitlab-ci-multi-runner
1 | 6. Runner[高级配置](https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/configuration/advanced-configuration.md) |
concurrent = 4
check_interval = 0
[[runners]]
name = “test”
url = “http://your-domain.com/ci"
token = “your-token”
executor = “docker”
[runners.docker]
tls_verify = false
image = “node:4.5.0”
privileged = false
disable_cache = false
volumes = [“/cache”]
[runners.cache]
[runners.kubernetes]
host = “”
cert_file = “”
key_file = “”
ca_file = “”
image = “”
namespace = “”
privileged = false
cpus = “”
memory = “”
service_cpus = “”
service_memory = “”1
2
3
4
5
6
7
8
到这里我们的Runner就安装配置好了,接下来是对项目根目录中`.gitlab-ci.yml`进行配置。
### 配置构建任务
1. 在项目根目录添加`.gitlab-ci.yml`文件
关于该文件的各项配置[请见](https://docs.gitlab.com/ce/ci/yaml/README.html)
2. 示例:
这里使用了我自己的docker image,配置了自己需要的环境
image: wuyanxin/node
variables:
MYSQL_DATABASE: wan_ark-unit
MYSQL_ALLOW_EMPTY_PASSWORD: “yes”
关于service请见: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-service
services:
- mysql:5.6
- redis:3.2.4
stages:
- test
- eslint
- deploy
before_script:
- echo 'REDIS_HOST=redis' >> .env
- echo 'DB_HOST=mysql' >> .env
- yarn install
test_service:
stage: test
script:
- npm run build
- npm test
eslint_src:
stage: eslint
script:
- npm run lint
allow_failure: true
deploy:
stage: deploy
script:
- echo 'deployd!'
only:
- master
`
这里使用了nodejs项目作为例子,其他语言类似语法。
- 执行结果
最后
这是我最近对于Gitlab CI的实验记录,对于Gitlab CI的使用体验我给82分。虽然在实验过程中踩了很多坑,真的踩到差点放弃了,所以记录一下我的实验过程,希望对他人有帮助。
关于在这个过程中踩到的坑以及构建速度优化请关注我下期文章。