Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

本地基于Docker+Gitlab+Gilab CI搭建持续集成环境 #29

Open
lqshow opened this issue Mar 24, 2018 · 3 comments
Open

本地基于Docker+Gitlab+Gilab CI搭建持续集成环境 #29

lqshow opened this issue Mar 24, 2018 · 3 comments

Comments

@lqshow
Copy link
Owner

lqshow commented Mar 24, 2018

背景

前段时间在调研持续集成的框架,在本地部署过一套环境,在此记录一下

安装本地环境

Gitlab

安装Gitlab

docker run -d \
--hostname gitlab.basebit.ai \
--publish 443:443 --publish 80:80 --publish 24:22 \
--name docker-gitlab \
--restart always \
--volume ~/workspace/docker/gitlab/config:/etc/gitlab \
--volume ~/workspace/docker/gitlab/logs:/var/log/gitlab \
--volume ~/workspace/docker/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest

修改本机host文件

# vi /etc/hosts
# 加入指定自定义域名
127.0.0.1 gitlab.basebit.ai

测试本机设置是否成功

ssh -T git@gitlab.basebit.ai

使用24端口具体碰到到问题参见:gitlab docker

Gitlab Runner

安装Gitlab Runner

docker run -d --name docker-gitlab-runner --restart always \
  --link docker-gitlab:gitlab.basebit.ai \
  -v ~/workspace/docker/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /usr/local/bin/docker:/usr/bin/docker \
  gitlab/gitlab-runner:latest

注: linux和macOs的不同处

# linux
-v /usr/bin/docker:/usr/bin/docker

# macOs
-v /usr/local/bin/docker:/usr/bin/docker

注册Runner

  1. 进入gitlab runner容器

    docker exec -it docker-gitlab-runner /bin/bash
  2. 在容器内执行注册命令

    sudo gitlab-runner register -n \
       --url http://gitlab.basebit.ai/ \
       --registration-token xxxxxxxxxxxxxx \
       --executor docker \
       --description "Runner Description" \
       --docker-image "docker:latest" \
       --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
       --docker-extra-hosts "gitlab.basebit.ai:172.17.0.2"

Specific Runners和Shared Runners的区别

runner desc
Specific Runners 指定运行某一个Gitlab CI 的项目
Shared Runners 运行所有的 CI 项目(只有管理员权限可配置Shared Runners)

碰到的问题

GitLab与Runners通过API进行通信, 所以只要Runners能访问网络就能与GitLab通信

由于使用的是本地域名,如果在注册runner中未设置过host,碰到连接失败问题参见如下
Gitlab-CI: Failed to connect to gitlab.basebit.ai port 80: Connection refused

项目加入CI/CD

  1. 需在仓库根目录创建一个.gitlab-ci.yml 文件
  2. 并为该项目指派一个Runner
  3. 以上2步完成后,每次push的commit或者Merge Request代码到Git仓库, Runner就会自动运行pipeline,开始自动化集成。

样例参考: gitlab ci nodejs

参考

@pointline
Copy link

pointline commented Mar 18, 2020

遇到问题
在runner中注册使用--executor docker,在gitlab构建时报错

bash fatal: unable to access 'http://gitlab.example.com:8929/root/test.git/': Failed to connect to gitlab.example.com port 8929: Connection refused ERROR: Job failed: exit code 1

就是git clone 找不到地址

注册

gitlab-runner register -n \
   --url http://172.21.0.3:8929/ \
   --registration-token xxxxxxxxxxxxxxxxx\
   --executor docker \
   --description "Runner Description" \
   --docker-image "node:10.16.3-alpine" \
   --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
   --docker-extra-hosts "gitlab.example.com:172.21.0.3"

我想应该是runner创建的docker容器是否访问不到172.21.0.3

请问有什么解决办法吗?谢谢

@lqshow
Copy link
Owner Author

lqshow commented Mar 18, 2020

@pointline 需要增加一个 docker 的 host 映射,不过我看你配置里是加过的,问题应该出在 --url 上,你将 value 调整成 gitlab.example.com 试下看

@pointline
Copy link

@lqshow 感谢

gitlab-runner register -n \
   --url http://gitlab.example.com:8929/ \
   --registration-token dFaZ-PdbPX_xBybUkjq4 \
   --executor docker \
   --description "Runner Description" \
   --docker-image "node:10.16.3-alpine" \
   --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
   --docker-extra-hosts "gitlab.example.com:172.21.0.3" \
   --docker-network-mode gitlab_gitlab-network

我查到到问题,发现在runner过程中创建的新的container是独立,没有建立在与gitlab和gitlab-runner同一网络。所以导致clone是无法访问,需要这个新container指定--docker-network-mode同一网络就可访问了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants