1. 程式人生 > >簡單搭建Gitlab CI持續集成環境

簡單搭建Gitlab CI持續集成環境

nib 介紹 ubuntu systemctl set detail nss 分享圖片 升級

簡單搭建Gitlab CI持續集成環境

簡單介紹Gitlab CI的功能

  • 從GitLab 8.X 開始,GitLab CI就已經集成在GitLab中,我們只要在項目中添加一個.gitlab-ci.yml文件,然後添加一個Runner,開啟Runner,即可進行持續集成。而且隨著GitLab的升級,GitLab CI變得越來越強大。

GitLab Runner

  • 在沒使用過Gitlab之前,我也有一個困惑,到底Gitlab Runner是什麽東西、它的作用是什麽?</br>GitLab Runner就是來執行這些構建任務的

  • 而此時又會多了一個困惑,Gitlab CI不是也是用來運行構建任務的嗎?</br>
    一般來說,構建任務都會占用很多的系統資源(譬如編譯代碼),而GitLab CI又是GitLab的一部分,如果由GitLab CI來運行構建任務的話,在執行構建任務的時候,GitLab的性能會大幅下降。GitLab CI最大的作用是管理各個項目的構建狀態,因此,運行構建任務這種浪費資源的事情就交給GitLab Runner來做拉!因為GitLab Runner可以安裝到不同的機器上,所以在構建任務運行期間並不會影響到GitLab的性能。

1、首先部署安裝Gitlab

首先安裝git

yum install -y git

安裝Gitlab依賴

yum install -y curl openssh-server openssh-clients postfix cronie policycoreutils-python

啟動ssh,postfix並設置開機啟動和配置防火墻規則

sudo systemctl enable sshd
sudo systemctl start sshd
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld

下載安裝Gitlab

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
yum install gitlab-ce

修改Gitlab配置,將external_url變量地址改為自己域名或IP地址

vim  /etc/gitlab/gitlab.rb

## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
external_url ‘http://gitlab.test.com‘

## Roles for multi-instance GitLab

重新啟動加載配置文件

gitlab-ctl reconfigure
gitlab-ctl restart

可以netstat -ntlp查看啟動的服務及端口(可以看出已經啟動了nginx服務及端口為80端口,所以可以直接訪問前面配置的域名或IP地址)

技術分享圖片

在瀏覽器上訪問地址(管理員賬號密碼在UI界面上進行設置)

2、接下來安裝與配置Gitlab Runner

點開Runners可以看到一個設置的manually![]技術分享圖片
點擊install GitLab Runner安裝Gitlab Runner

# For Debian/Ubuntu
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
$ sudo apt-get install gitlab-ci-multi-runner
# For CentOS
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
$ sudo yum install gitlab-ci-multi-runner

註冊Runner(這裏可以選擇註冊一個指定的Runner或者註冊一個共享的Runner)

指定的Runner可以理解為只能對某個份代碼有效的一個Runner,共享Runner可以理解為所有的代碼都可以應用得到同一個Runner,但是註冊共享Runner只有admin權限又才可。

  • 註冊一個共享的Runner(註冊指定Runner也是一樣的操作)</br>
    首先admin的賬號下看到Runner的設置manually的URL與token信息
    技術分享圖片
sudo gitlab-ci-multi-runner register

技術分享圖片

  • 輸入Gitlab CI地址
  • 輸入項目Gitlab CI token
  • 輸入Gitlab Runner描述
  • 輸入Gitlab Runner標簽
  • 輸入Gitlab Runner執行的語言

可以查看在Gitlab 共享Runner上多了一條Runner記錄
技術分享圖片
也可以使用list查看Runner的狀態:

gitlab-runner  list
Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml
cml_test*.*.172.123                           Executor=shell Token=ece68d167647507d1aa61d80ca0f05 URL=http://gitlab.test.com/
  • 接下來編寫.gitlab-ci.yml文件,推送到遠程代碼倉庫。
    這裏演示一個簡單的git pull操作
cat .gitlab-ci.yml
# 定義 stages
stages:
  - test

# 測試
test:
  stage: test
  script:
    # Deploy test
    - ansible cml_test*.*.172.123 -a "cd /home/www/test;git pull"

(這裏我使用了ansible去管理,更新代碼操作)
最後推送到遠程代碼倉庫上去。

git add .
git commit -m "fix .gitlab-ci.yml"
git push
  • 在相應的代碼庫下開啟的這個共享Runner。
    技術分享圖片

提交代碼觸發CI

技術分享圖片

簡單搭建Gitlab CI持續集成環境