目前經常使用的持續集成工具主要是jenkins與gitlab-ci ,我已在另外一博文中詳細記錄了jenkins部署過程(其中包括gitlab的搭建),此篇介紹gitlab-ci的使用。nginx
GitLab-Runner是配合GitLab-CI進行使用的。通常地,GitLab裏面的每個工程都會定義一個屬於這個工程的軟件集成腳本,用來自動化地完成一些軟件集成工做。當這個工程的倉庫代碼發生變更時,好比有人push了代碼,GitLab就會將這個變更通知GitLab-CI。這時GitLab-CI會找出與這個工程相關聯的Runner,並通知這些Runner把代碼更新到本地並執行預約義好的執行腳本。 已在線下機房安裝好gitlab,以前jenkins項目博文有介紹詳細安裝方法,gitlab-runner端安裝在阿里雲ECS上,且還需安裝maven在 runner所在服務器上,maven安裝博客有教程,註冊時經過gitlab的公網或域名方式的url註冊。
我司防火牆的80端口被封了,須要將gitlab的默認80端口修改成8088:git
vim /etc/gitlab/gitlab.rb external_url 'http://gitlab.home.com:8088' #添加上端口 nginx['listen_port'] = 8088 #添加 vim /var/opt/gitlab/nginx/conf/gitlab-http.conf 、 server { listen *:8088; #改其內置nginx監聽端口 gitlab-ctl reconfigure #重置配置文件 gitlab-ctl restart #重啓服務
本地gitlab 版本查看 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
版本選擇
本文gitlab爲10.5.2,runner爲gitlab-ci-multi-runner-9.5.1-1.x86_64.rpmweb
[root@localhost ~]# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
[root@localhost ~]# yum -y install gitlab-ci-multi-runner-10.0.0-1-x86_64
[root@localhost ~]# gitlab-ci-multi-runner register Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://172.19.0.101:8088/ Please enter the gitlab-ci token for this runner: hy-zVkJBwTyG3WyvS3Sr Please enter the gitlab-ci description for this runner: [localhost.localdomain]: test_nothing Please enter the gitlab-ci tags for this runner (comma separated): nothing_runner Whether to run untagged builds [true/false]: [false]: false Whether to lock Runner to current project [true/false]: [false]: false Registering runner... succeeded runner=hy-zVkJB Please enter the executor: docker+machine, docker-ssh+machine, docker-ssh, shell, ssh, virtualbox, kubernetes, docker, parallels: shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@localhost ~]# gitlab-ci-multi-runner run [root@localhost ~]# ps -ef|grep runner root 9294 1 0 04:13 ? 00:00:00 /usr/bin/gitlab-ci-multi-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user gitlab-runner 此時在gitlab的web界面能夠看到本runner
runner會執行添加的命令,要注意pom.xml的路徑,默認在項目根目錄中,若不在,須要以項目根目錄爲相對路徑加上該文件所在路徑,如:mvn clean package -f libra/ ,如下腳本能夠去掉before_script,打包命令改成 mvn clean package。在註冊runner時,要根據提示,是否要執行不帶tag的項目。 before_script: - mvn clean stages: - deploy deploy_job: stage: deploy only: - master script: - mvn package - sh /test_project/test-backend/runner_tomcat.sh tags: - test-tag
以上runner_tomcat.sh腳本定義了打包後的與應用相關的鏡像處理與到阿里雲的鏡像推送。docker
vim /test_project/test-backend/runner_tomcat.sh #!/bin/bash namespace=testimage project_name=aaa-a war_name=aaa.war image_name=registry-vpc.cn-shanghai.aliyuncs.com/$namespace/$project_name old_version=`sudo docker images|grep $image_name|awk '{print $2}'|sort -r|awk 'NR==1{print}'` new_version=$(expr $old_version + 1) newimage=$image_name:$new_version Dockerfile_path=/test_project/$project_name/ war_path=`find / -cmin -20 -name $war_name 2>/dev/null | grep -v "/var/lib/docker" | grep -v "/test_project/$project_name/"` source /etc/profile rm -rf /test_project/$project_name/$war_name && mv $war_path /test_project/$project_name/ && sudo docker login --username=ur_name --password=test1245\& registry-vpc.cn-shanghai.aliyuncs.com sudo docker build -t $newimage $Dockerfile_path && sudo docker push $newimage
隨意修改有 .gitlab-ci.yaml文件的項目的代碼,提交。查看流水線,也能夠看鏡像倉庫是否推送了該版本鏡像
初次運行應該會報錯,如下是我屢次報錯總結shell
緣由:能夠 ps -ef|grep runner 查看,有多個runner進程,分別屬於gitlab-runner與root
解決方法:kill掉全部runner進程,以root身份執行 gitlab-ci-multi-runner start ,查看進程數,只有一個
vim
解決方法:tomcat
vim /etc/sudoers gitlab-runner ALL=(root) /bin/docker,NOPASSWD: ALL #加上NOPASSWD: ALL,執行命令時不用再輸入passwd for gitlab-runner
解決方法:對該羣組沒操做權限,將你登錄gitlab的此用戶加到該羣組成員中,訪客身份無效。bash