基于gitlab搭建CI

自建gitlab

自建gitlab一大优势是,之前用翻墙走github速度也在1M/s左右,可是自建的gitlab提升了建立连接的速度,可以说体验又上升了一个档次。

用docker起一个gitlab服务尤其方便 GitLab Docker images

选择CI

如果有人像我一样对Jenkins的UI和配置不感兴趣,那么Gitlab CI runner是不错的选择,配置用YAML,清晰明了,UI集成在gitlab中。对比jenkins,上手容易,颜值更高。

Gitlab CI runner

  • 找一台ubuntu 16.04,机器配置按照项目大小和数量决定
  • 安装CI runner
    1
    $ sudo apt-get install gitlab-ci-multi-runner
  • 将runner注册到gitlab
    1
    $ sudo gitlab-ci-multi-runner register
  • 调整配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    concurrent = 16 # 更新并发build数量到16

    [[runners]]
    name = "10-1-10-10" # 这里是runner的名字
    url = "https://gitlab.com" # 所属gitlab的url
    token = "please-input-gitlab-register-token" # gitlab注册token
    executor = "docker" # 以docker形式运行每次CI任务
    [runners.docker]
    tls_verify = false
    image = "ruby:2.1" # docker运行的默认镜像
    privileged = false # 不授权
    volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"]
    extra_hosts = ["gitlab.com:some-internal-ip"]
    [runners.cache]

调整concurrent,最大限度利用机器
若镜像中使用docker push等命令,将宿主机docker.sock挂载到容器中
若gitlab与runner在同网络,设置docker容器内的gitlab host为内网IP,最小化网络延迟

Pipeline

pipeline指CI的流程可以当做管道,只有上一个操作成功之后,下一个操作继续执行,如利用gitlab ci构建build->test->release->deployment的流程,当代码push到master时,CI触发,如下
gitlabci

配置CI

.gitlab-ci.yml是gitlab项目的CI配置文件,文件形式是yaml

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
image: ruby:2.1
stages:
- test
- build
- release
- deployment
variables:
DOCKER_DRIVER: overlay
SERVICE_NAME: test
CONTAINER_TEST_IMAGE: $IMAGE_NAME:$CI_BUILD_REF_NAME
CONTAINER_RELEASE_IMAGE: $IMAGE_NAME:latest

test:1.8.0:
image: golang:1.8
stage: test
before_script:
- echo "before test"
script:
- echo "test"

build:test:
image: golang:1.8
stage: build
before_script:
- echo "before script"
script:
- mkdir -p binaries/
- touch binaries/test
artifacts:
paths:
- binaries/
expire_in: 1h
only:
- test

build:prod:
image: golang:1.8
stage: build
before_script:
- echo "before build"
script:
- mkdir -p binaries/
- touch binaries/prod
artifacts:
paths:
- binaries/
expire_in: 1h
only:
- master

release:test:
stage: release
script:
- echo "release $CONTAINER_TEST_IMAGE"
only:
- test
dependencies:
- build:test

release:prod:
stage: release
script:
- echo "release $CONTAINER_RELEASE_IMAGE"
only:
- master
dependencies:
- build:prod

deployment:test:
stage: deployment
image: kroniak/ssh-client
before_script:
- echo "before script"
script:
- echo "deployment test"
only:
- test

deployment:stage:
stage: deployment
image: kroniak/ssh-client
before_script:
- echo "before script"
only:
- master
script:
- echo "deployment stage"
  • stage声明管道有几个阶段,stage的顺序代表着前置stage完成后才能进入下一个stage,每个stage有1个或多个可执行点
  • 每个可执行操作里有一些配置
    • stage
      当前执行所属阶段,就是文件开头声明的stages列表中一个
    • image
      当前命令执行的基础镜像
    • before_script
      脚本执行前初始化命令
    • only
      限制执行的分支,例如只有test分支能触发deployment:test
    • script(required)
      执行的脚本
    • artifacts
      这次执行将产生的需要传递给下一个stage的文件
    • dependencies
      当前执行依赖的前置执行,不仅是声明一种依赖关系,同时上个执行生成的artifacts也会传递过来
  • 在gitlab项目中设置CI/CD Pipelinesvariables,可以为docker容器注入隐秘变量,如docker仓库秘钥,这些变量可以在CI运行脚本时被解析。

小结

Gitlab CI能自定义阶段和在容器里执行脚本,并且水平拓容便捷,和Gitlab的集成好。

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×