- Gitlab-Ci運行原理:
由以下兩個模塊組成
gitlab-ci server
gitlab-ci-runner
其中,gitlab-ci server負責調度、觸發Runner,以及獲取返回結果. 而gitlab-ci-runner則是主要負責來跑自動化CI(測試,編譯,打包等)。
基本流程是: 用戶提交代碼->檢查是否有.gitlab-ci.yml文件->如果無,則結束;-> 如果有,則調用runner執行腳本->獲取返回的結果。
1. 登錄Gitlab
前提: gitlab 已經部署完成並能登錄,runner已部署完成
版本:
2. 創建一個Project
創建好project后,就有了該project的URL。
3.注冊runner
注冊命令:
gitlab-ci-multi-runner register
#引導會讓你輸入gitlab的url,輸入自己的url,例如http://gitlab.example.com/
#引導會讓你輸入token,去相應的項目下找到token,例如ase12c235qazd32
#引導會讓你輸入tag,一個項目可能有多個runner,是根據tag來區別runner的,輸入若干個就好了,比如web,hook,deploy
#引導會讓你輸入executor,這個是要用什么方式來執行腳本,圖方便輸入shell就好了
4.添加.gitlab-ci.yml文件
注:在項目根目錄增加YAML格式的CI腳本文件.gitlab-ci.yml
stages:
- build
- deploy
build:
stage: build
script:
- ./build.sh
tags:
- udacs_prod
deploy:
stage: deploy
script:
- ./deploy.sh
tags:
- udacs_prod
注: 這里的標簽與創建的runner標簽需要保持一致
5.啟動runner
檢查runner的狀態:
gitlab-ci-multi-runner verify
Running in system-mode.
Verifying runner... is alive runner=70c3d017
Verifying runner... is alive runner=f27021ba
重啟所有runner:
gitlab-ci-multi-runner run
重啟單個runner:
gitlab-ci-multi-runner run-single --url http://IP:port/ --token runnerToken --executor shell
6. 上傳代碼
第一次上傳:
Step1: 復制代碼到本地
git clone http://IP:port/**.git
Step2: 進入代碼目錄
cd udacs-mp
Step3: 新建一個文件
touch README.md
Step3:添加到倉庫
git add .
Step4:添加注釋
git commit -m "init by 20180105"^C
Step5:提交代碼到matser分支(只有一個分支時,默認是matser)
git push -u origin master
Username for 'http://IP:port/**.git':
Password for 'http://IP:port/**.git':
Counting objects: 2224, done.
Delta compression using up to 24 threads.
Compressing objects: 100% (1977/1977), done.
Writing objects: 100% (2224/2224), 169.63 MiB | 4.62 MiB/s, done.
Total 2224 (delta 507), reused 0 (delta 0)
remote: Resolving deltas: 100% (507/507), done.
To 'http://IP:port/**.git':
* [new branch] master -> master
分支 master 設置為跟蹤來自 origin 的遠程分支 master。
非第一次提交:
Step1:執行命令git add --all,將所有文件都添加到倉庫中,如果想添加某一個文件,則將后面的--all換成你要提交的文件名即可。:
git add build.sh deploy.sh
Step2:需要將增加的文件commit到倉庫里去,執行命令git commmit -m "注釋語句":
git commit -m "*.sh"
[master f242b06] *.sh
Committer: root <root@localhost.localdomain>
Step3:此時還沒完,還要將commit的代碼push到遠程分支,由於我們本地只有master分支,所以我們可以直接執行命令git push
git push
warning: push.default 未設置,它的默認值將會在 Git 2.0 由 'matching'
修改為 'simple'。若要不再顯示本信息並在其默認值改變后維持當前使用習慣,
進行如下設置:
git config --global push.default matching
若要不再顯示本信息並從現在開始采用新的使用習慣,設置:
git config --global push.default simple
參見 'git help config' 並查找 'push.default' 以獲取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有時要使用老版本的 Git,
為保持兼容,請用 'current' 代替 'simple' 模式)
Username for 'http://IP:port/**.git':
Password for 'http://IP:port/**.git':
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 861 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To 'http://IP:port/**.git':
43a928a..f242b06 master -> master
提交完成后,就會自動觸發一次CI部署了。
重啟gitlab:
/etc/httpd/conf/httpd.conf
- 問題定位
問題1: 啟動runner時報錯信息:
gitlab-ci-multi-runner run
Starting multi-runner from /etc/gitlab-runner/config.toml ... builds=0
Running in system-mode.
Configuration loaded builds=0
Metrics server disabled
WARNING: Checking for jobs... failed runner=70c3d017 status=couldn't execute POST against http://IP/api/v4/jobs/request: Post http://IP/api/v4/jobs/request: dial tcp IP:80: getsockopt: connection refused
WARNING: Checking for jobs... failed runner=f27021ba status=502 Bad Gateway
WARNING: Checking for jobs... failed runner=70c3d017 status=couldn't execute POST against http://I/api/v4/jobs/request: Post http://IP/api/v4/jobs/request: dial tcp IP:80: getsockopt: connection refused
原因是:修改端口后,默認端口不再是80,需要修改runner的配置信息
問題2: gitlab-ci runner自動執行腳本時,提示腳本權限不夠。
原因是: 直接在gitlab的project上添加的腳本,是root權限,應該修改為gitlab-runner權限。 修改辦法,先刪掉文件,然后在客戶端添加腳本,確認權限后,再commit到服務端。
參考資料:
https://www.jianshu.com/p/2b43151fb92e