這是阿里內網的一篇文章,感有用,故轉載來了,自己實際搭建了,故記錄一下:
背景
在日常工作中,我們往往會搭建基于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