GitLab-CI 這個是一套配合GitLab使用的持續集成系統,是GitLab自帶的,也就是你裝GitLab的那臺服務器上就帶有的。無需多考慮。.gitlab-ci.yml的腳本解析就由它來負責。java
GitLab-Runner 這個是腳本執行的承載者,.gitlab-ci.yml的script部分的運行就是由runner來負責的。GitLab-CI瀏覽過項目裏的.gitlab-ci.yml文件以後,根據裏面的規則,分配到各個Runner來運行相應的腳本script。這些腳本有的是測試項目用的,有的是部署用的。git
GitLab-CI與GitLab-Runner關係示意圖web
安裝GitLab-Runner 在centOS上安裝gitlab-ci-multi-runnershell
這樣就裝好了gitlab-ci-multi-runner,然而咱們只是裝好了gitlab-runner,固然咱們要接着向gitlab-CI註冊這個runner,否則gitlab-CI在push事件到來的時候怎麼知道要調用誰呢?這裏也能夠發現和webhook方式的區別,webhook方式是咱們主動配置了一個鏈接給gitlab;gitlab-runner只要註冊一下就行了。apache
那麼咱們就註冊一下tomcat
而後就註冊好了,在gitlab中相應的位置就能夠看到你註冊好的runner信息。ruby
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
bash
$ yum install gitlab-ci-multi-runner
服務器
$ gitlab-ci-multi-runner register
app
#引導會讓你輸入gitlab的url,輸入本身的url,例如http://gitlab.example.com/
#引導會讓你輸入token,去相應的項目下找到token,例如ase12c235qazd32
#引導會讓你輸入tag,一個項目可能有多個runner,是根據tag來區別runner的,輸入若干個就行了,好比web,hook,deploy
#引導會讓你輸入executor,這個是要用什麼方式來執行腳本,圖方便輸入shell就行了。
編寫.gitlab-ci.yml
在項目根目錄下編寫.gitlab-ci.yml這樣在push以後,gitlab-ci就會自動識別來解析了
stages: - deploy - build - ops deploy-web1_job: stage: deploy tags: - gitRunner的tag標籤 only: - master script: - bash deploy 項目分組名 項目名 master build-web1_job: stage: build tags: - gitRunner的tag標籤 only: - master script: - source /etc/profile - cd /data/wwwroot/master/項目分組名/項目名 - /data/wwwroot/apache-maven-3.6.0/bin/mvn clean package -Dmaven.test.skip=true ops-web1_job: stage: ops tags: - gitRunner的tag標籤 only: - master script: - rm -f /data/wwwroot/tomcat/apache-tomcat-8.5.35/webapps/項目名.war - cp /data/wwwroot/master/項目分組名/項目名/target/項目名.war /data/wwwroot/tomcat/apache-tomcat-8.5.35/webapps/項目名.war
這裏咱們只有一個stage是deploy。only指定了只有在master分支push的時候纔會被執行。tags是shell,對應了剛纔註冊runner的時候的tags。
最重要的script部分deploy Example_Group Example_Project,這裏是一條shell指令,爲了方便通用性,deploy是我在服務器上編寫的一個腳本,傳入參數是Example_Group Example_Project分別是項目組名和項目名。執行這一條指令就可以自動部署到/xxx/Example_Group/Example_Project的服務器目錄下。那麼隨便什麼項目都用這個格式去套就行了,這樣新項目的自動部署也不須要登陸到服務器上去修改了。
編寫deploy腳本
在gitlab-runner的~/.local/bin/目錄下新建deploy文件
$ su gitlab-runner
$ mkdir ~/.local/bin
$ cd ~/.local/bin
$ touch deploy
並編輯成以下內容
if [ $# -ne 3 ] then echo "arguments error!" exit 1 else deploy_path="/data/wwwroot/$3/$1/$2" if [ ! -d "$deploy_path" ] then git clone "gitlab服務器地址:${1}/${2}.git" $deploy_path cd $deploy_path git checkout $3 else cd $deploy_path git pull fi fi
加上執行權限,而後把這個腳本放在gitlab-runner的~/.local/bin下就能夠生效了(爲了避免用寫難看的./deploy)
$ chmod +x ~/.local/bin/deploy
而且把~/.local/bin加到$PATH路徑中(用戶執行命令時候可以查找到這個目錄),只要在~/.profile末尾加入這一句話
PATH="$HOME/.local/bin:$PATH"
配置ssh登陸
上面的deploy腳本是用ssh方式來和gitlab聯繫的。因此要給gitlab-runner這個用戶配置一個gitlab上能ssh的用戶。首先在gitlab-runner下生成一個密鑰對
$ mkdir ~/.ssh
$ cd ~/.ssh
$ ssh-keygen
# 提示輸入一直按回車默認就能夠了
$ cat id_rsa.pub
移交部署目錄權限
有些同窗可能說腳本執行失敗了,有一個緣由是/var/example的全部者是root,gitlab-runner並無權限新建文件。因此咱們把/var/example目錄的全部者交給gitlab-runner
$ chown -hR gitlab-runner:gitlab-runner /var/www
若是仍是不成功,能夠在服務器上手工deploy XX XX一次,第一次訪問這個服務器的時候,有個命令行提示是要把sign添加進已知服務器列表,須要手工輸入個yes。若是在服務器上可以正常deploy,那麼
這樣就大功告成了。
注:若是在發佈運行的過程當中一直報ssh的密鑰驗證不經過 則執行這個: ssh-keyscan -H 服務器ip >> ~/.ssh/known_hosts