未分类

gitlab持续集成docker环境搭建

thumbnail

之前一直在使用gitlab触发jenkins的方式进行构建,觉得需要多个系统进行配合始终有些麻烦,进行要讲的是gitlab官方的gitlab-runner工具

前提准备

这里采用gitlab.local域名进行测试,本地host文件中添加

1
127.0.0.1 gitlab.local

配置docker-compose.yml

这里在一台机器上部署,采用docker-compose命令进行管理更方便

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
version: '2'
services:
gitlab-runner:
image: gitlab/gitlab-runner:latest
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./data/gitlab-runner:/etc/gitlab-runner
gitlab.local:
image: gitlab/gitlab-ce:latest
restart: always
hostname: gitlab.local
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url "http://gitlab.local"
gitlab_rails['time_zone'] = "Asia/Shanghai"
gitlab_rails['gitlab_shell_ssh_port'] = 2222
# smtp配置
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.xxxx.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "xxx@xxx.com"
gitlab_rails['smtp_password'] = "xxxxxx"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "xxx@xxx.com"
volumes:
- ./data/gitlab/config:/etc/gitlab
- ./data/gitlab/logs:/var/log/gitlab
- ./data/gitlab/data:/var/opt/gitlab
ports:
- 80:80
- 443:443
- 2222:2222

参数说明

  • restart: always 相当于开机启动
  • GITLAB_OMNIBUS_CONFIG 环境变量为gitlab配置项

配置gitlab-ce环境

以上步骤配置后,在docker-compose.yml文件所在目录执行以下命令

1
docker-compose up -d
  • 由于gitlab-ce首次启动较慢,大约等2分钟后我们在浏览器中打开http://gitlab.local,如果出现502,则继续等待
  • 如果启动成功则会出现设置密码页面,默认账号为root
  • 打开runner设置页面http://gitlab.local/admin/runners,我们需要用到页面中的这个注册令牌

    1
    2
    3
    4
    5
    How to setup a shared Runner for a new project
    1.安装一个与 GitLab CI 兼容的 Runner (如需了解更多的安装信息,请查看 GitLab Runner)
    2.在 Runner 设置时指定以下 URL: http://gitlab.local/
    3.在安装过程中使用以下注册令牌: ZiyZ9iBHgWfuTRSeTeVE
    4.启动 Runner!

配置gitlab-runner环境

以上步骤配置后,在docker-compose.yml文件所在目录执行以下命令

1
docker-compose exec gitlab-runner gitlab-ci-multi-runner register

然后根据提示进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Running in system-mode.                            

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.local
Please enter the gitlab-ci token for this runner:
ZiyZ9iBHgWfuTRSeTeVE
Please enter the gitlab-ci description for this runner:
[bb53932d6c67]: test
Please enter the gitlab-ci tags for this runner (comma separated):
docker
Whether to run untagged builds [true/false]:
[false]:
Whether to lock the Runner to current project [true/false]:
[true]:
Registering runner... succeeded runner=ZiyZ9iBH
Please enter the executor: virtualbox, docker+machine, kubernetes, docker, docker-ssh, parallels, shell, ssh, docker-ssh+machine:
docker
Please enter the default Docker image (e.g. ruby:2.1):
alpine:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

浏览器中刷新runner设置页面http://gitlab.local/admin/runners,如果列表中出现当前节点则表示配置成功


到这里大家可能会认为全都配置成功了,其实还得分一种情况

由于我们采用的http://gitlab.local,这是一个不存在的域名,我们在构建的时候采用的又是docker容器,因此必须保证这些容器能正常访问gitlab.local,我们需要增加一个extra_hosts配置项

在文件./data/gitlab-runner/config.toml增加一行extra_hosts配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
concurrent = 1
check_interval = 0

[[runners]]
name = "test"
url = "http://gitlab.local"
token = "04d89ab90942930a9401d0dffbc35b"
executor = "docker"
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
extra_hosts = ["gitlab.local:192.168.0.100"]
[runners.cache]

好了,到此结束,只要在项目中新增一个.gitlab-ci.yml文件就会在提交代码的时候自动构建了

提供一个express项目的配置参考文件

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
# This file is a template, and might need editing before it works on your project.
# Official framework image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/node/tags/
image: node:alpine

# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
paths:
- node_modules/

test:
stage: test
script:
- npm install
- npm test

pack:
stage: deploy
script:
- npm install
- npm pack
artifacts:
paths:
- ./*.tgz
分享到