这是阿里内网的一篇文章,感有用,故转载来了,自己实际搭建了,故记录一下:
背景
在日常工作中,我们往往会搭建基于jenkins的集成测试环境来保证每次代码功能的正确性,相关jenkins插件也非常丰富,比较适合复杂的集成环境。jenkins虽好,但需要投入人力维护,那有没有一种比较轻量级别的集成工具,答案是肯定的。GitLab自从8.0版本之后,自动集成GitLab-CI功能。那接下来让我们窥探下docker+GitLab-CI结合的集成环境。
GitLab 开启build功能
GitLab项目-》项目设置-》勾选builds选项-》保存。开启后会发现多了几个菜单,打开setting,选择runner,提示Runner安装指令。其中Runner有两种模式:Specific runners 和 Shared runners,如下图:
Specific runners: 这类Runner是被指定为某个project提供构建服务的,这意味着系统中的任何用户,都可以建立自己的Runner,并把它指派给自己的某一个project。
Shared runners: 这类Runner是全局的,意思是为整个GitLab系统范围内的project提供构建服务,只有系统管理员能创建这类Runner。
更多入门信息请参考@许晓斌的文章:http://www.atatech.org/articles/49152
基于docker的GitLab-Muti-Runner安装、启动&注册
准备工作
准备一台已装有Docker engine的ECS机器,由于alidocker不支持CMD,顾以下均原生docker以及Specific Runners为示例进行说明。
安装&启动
docker run -d --name gitlab-runner --restart always \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /home/fanzhong/gitlab-runner/config:/etc/gitlab-runner \ gitlab/gitlab-runner:latest
脚本说明:
/var/run/docker.sock:/var/run/docker.sock:后续docker相关操作有关,务必配置
/home/fanzhong/gitlab-runner/config:/etc/gitlab-runner:mount此文件,方便配置gitlab-runner相关配置以及资源等,建议配置;
注册Runner至GitLab
注册的过程,实际是将Runner与GitLab web端关联起来,其注册过程如下:
docker exec -it gitlab-runner gitlab-ci-multi-runner registerRunning in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):http://gitlab.alibaba-inc.com/ciPlease enter the gitlab-ci token for this runner:xxxxxxxxxxxxxPlease enter the gitlab-ci description for this runner:Please enter the gitlab-ci tags for this runner (comma separated):dockerRegistering runner... succeeded runner=270fa832Please enter the executor: docker-ssh, parallels, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, docker:dockerPlease enter the default Docker image (eg. ruby:2.1):reg.docker.alibaba-inc.com/aliexpress/spring-boot-start-scripts-ci:no-nginxRunner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
其中的Coordinator URL 和 token来自GitLab。最终GitLab-Runner生成的相关配置config.toml内容如下:
concurrent = 1check_interval = 0[[runners]] name = "spring-boot-scripts" url = "http://gitlab.alibaba-inc.com/ci" token = "xxxxx" executor = "docker" [runners.docker] tls_verify = false image="reg.docker.alibaba-inc.com/aliexpress/spring-boot-start-scripts-ci:no-nginx" privileged = false disable_cache = false volumes = ["/cache"] [runners.cache] Insecure = false
.gitlab-ci.yml
在项目根目录下面添加yml配置文件,内容如下,更多配置信息详见.gitlab-ci.yml:
image: reg.docker.alibaba-inc.com/aliexpress/spring-boot-start-scripts-ci:no-nginxbefore_script: - rsync -avz /tmp/builds/spring-boot/startup-scripts/template/bin/* /home/admin/spring-boot-demo-application/bin/ | sed 's/^/ /g' - sed -i "s/template/spring-boot-demo-application/g" /home/admin/spring-boot-demo-application/bin/setenv.sh - rsync -avz /tmp/builds/spring-boot/startup-scripts/cai/* /home/admin/cai/ - sed -i "s/@appName@/spring-boot-demo-application/g" /home/admin/cai/conf/* - chmod u+x /home/admin/spring-boot-demo-application/bin/* /home/admin/cai/bin/nginxctl - chown -R admin:admin /home/admin/*stages: - testjob_no_nginx: stage: test script: - sed -i "s/NGINX_SKIP=0/NGINX_SKIP=1/g" /home/admin/spring-boot-demo-application/bin/setenv.sh - sh /home/admin/start.sh#job_nginx:# stage: test# script:# - sh /home/admin/start.sh# - curl localhost/status.taobao
用户镜像spring-boot-start-scripts-ci:no-nginx
此镜像必须支持CMD命令,否则yml中相关scripts不会运行,内部提供的几个OS基础镜像无法正常运行(alidocker不支持CMD),请基于docker官方镜像构建。
FROM centos:7RUN yum install -y rsync && \yum install -y sudo && \yum install -y unzip && \yum install -y dos2unix && \yum clean all && \sed -i -e 's/Defaults requiretty.*/ #Defaults requiretty/g' /etc/sudoers && \groupadd -g 1000 admin && useradd -u 1000 -g 1000 admin && \rpm -ivh --nodeps "http://yum.tbsite.net/taobao/5/x86_64/current/ajdk-8_0_0-b60/ajdk-8_0_0-b60-1.0.1-282804.el5.x86_64.rpm" && \echo "sudo -u admin /home/admin/spring-boot-demo-application/bin/jbossctl pubstart" > /home/admin/start.sh && \mkdir /home/admin/spring-boot-demo-application /home/admin/spring-boot-demo-application/bin /home/admin/spring-boot-demo-application/logs && \ln -s install/ajdk-8_0_0-b60 /opt/taobao/javaCOPY spring-boot-demo-application___int-hz.tgz spring-boot-demo-application___int-us.tgz app-conf.tgz /home/admin/spring-boot-demo-application/target/VOLUME /home/admin/logs /home/admin/spring-boot-demo-application/logs /home/admin/cai/logsCMD ["bash"]
示例代码
.gitlab-ci.yml
Dockerfile
config.toml