整體步驟概括
- 準備兩臺虛擬機器,最小安裝,配置ip能ping通外網即可,安裝docker
- 一臺虛擬機器跑gitlab,一臺跑gitlab runner和springboot專案
- 將springboot專案打成systemctl系統服務
- 編寫gitlab.ci自動構建你的springboot專案
自動構建的基本思路是當dev分支合併到master分支時,觸發構建,自動釋出,下面是詳細步驟
1.通過docker、docker-compose搭建gitlab
使用的是一個開源庫
這一步基本參考這個庫就能搭建成功了
- 確定你安裝好了docker和docker-compose
- 下載官方docker-compose檔案
wget https://raw.githubusercontent.com/sameersbn/docker-gitlab/master/docker-compose.yml
修改兩個地方 gitlab_port就是你訪問的埠地址,gitlab_host則是你配置的虛擬機器ip 這時你的目錄結構應該是這樣的 cd至gitlab目錄下執行命令docker-compose up
等待docker映象安裝完成 完成後等待幾分鐘gitlab初始化,訪問http://yourdomain:10080,重置密碼 這裡我已經建好專案了,到此 第一步搭建gitlab服務已經完成接下來是第二步
2.註冊gitlab-runner完成專案自動構建
- 新建dev分支
checkout你的dev分支,開發只對dev分支操作,master分支則用於ci構建 - 編寫gitlab-ci.yml
pages:
stage: deploy
script:
- cd /gitlab-project/demo
- git pull
- mvn package
- systemctl restart demo
only:
- master
複製程式碼
可以看到ci只針對master分支,即dev分支merge至master分支時 觸發構建
- 搭建runner伺服器 前面說到需要準備兩臺伺服器,一臺搭建gitlab,一臺搭建runner gitlab-runner比較簡單,查一查如何安裝即可,安裝完畢輸入
gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
#這裡輸入你的gitlab域名
# executor輸入shell
複製程式碼
域名和token在Specific Runners這裡


Description=demo
[Service]
WorkingDirectory=/gitlab-project/demo
PrivateTmp=true
Type=simple
ExecStart=/usr/java/jdk1.8.0_181-amd64/bin/java -jar /gitlab-project/demo/target/demo-0.0.1-SNAPSHOT.jar -java.io.tmpdir=/gitlab-project/tmp --spring.config.location=/gitlab-project/demo/target/classes/application.yml
ExecStop=kill -9 `ps -ef|grep java|grep -v grep|grep demo|awk '{print $2}'`
[Install]
WantedBy=multi-user.target
複製程式碼
gitlab-project/demo是這個demo專案的路徑,在進行ci前 你應該git clone專案至對應路徑
3. 最後一步啦
現在專案的某個介面返回資料是這樣的

我們修改一下



選擇對應的job檢視,已經在跑了


curl一下

小夥伴們還不趕緊試試?