搭建自己的GitLab-CI 和 GitLab-Runner 運行Laravel測試
Preface 前言
這篇文章將介紹如何使用自己的機器來搭建用於 Gitlab CI 的 runner. 在搭建自己的 CI Runner 之前,需要先明確一些概念:
Continuous Integration(持續集成)
CI 的全稱是 Continuous Integration (持續集成),是 extreme programming (極限編程) 的一部分。我們常用 CI 來做一些自動化工作,這種自動化工作會運行在一台集中的機器上,比如程序的打包,單元測試,部署等。這種構建方式避免了了打包環境差異引動的錯誤,並且通過 Gitlab 的 hook, 在代碼提交的各個環節自動地完成一系列的構建工作。
軟件集成是軟件開發過程中的一個環節,這個環節的工作一般會包括以下流程:合並代碼 ---->安裝依賴 ----> 編譯- ---> 測試 ----> 發布。
軟件集成的工作一般會比較細碎繁瑣,為了不影響開發效率,以前軟件集成這個環節一般不會經常進行或者只會等到項目后期再進行。但是有些問題,如果等到后期才發現,解決問題的代價很大,有可能導致項目延期或者失敗。因此,為了盡早發現軟件集成錯誤,鼓勵團隊成員應該經常集成他們的工作,通常每個成員每天應該至少集成一次。這就是所說的持續集成。所以說,持續集成是一種軟件開發實踐。
軟件集成的工作細碎繁瑣,以前是由人工完成的。但是現在鼓勵持續集成,那豈不是要累死人,還影響開發效率。所以,應該考慮將軟件集成這個工作自動化,這就出現了所謂的持續集成系統。
GitLab-CI
GitLab-CI就是一套配合 GitLab使用的持續集成系統(當然,還有其它的持續集成系統,同樣可以配合GitLab使用,比如Jenkins)。而且GitLab8.0以后的版本是默認集成了GitLab-CI並且 默認啟用的。GitLab-Runner
那GitLab-Runner又是什么東東呢?與GitLab-CI有什么關系呢?
GitLab-Runner是配合GitLab-CI進行使用的。一般地,GitLab里面的每一個工程都會定義一個屬於這個工程的軟件集成腳本,用來自動化地完成一些軟件集成工作。當這個工程的倉庫代碼發生變動時,比如有人push了代碼,GitLab就會將這個變動通知GitLab-CI。這時
GitLab-CI會找出與這個工程相關聯的Runner,並通知這些Runner把代碼更新到本地並執行預定義好的執行腳本。
所以,GitLab-Runner就是一個用來執行軟件集成腳本的東西。你可以想象一下:Runner就像一個個的工人,而GitLab-CI就是這些工人的一個管理中心,所有工人都要在GitLab-CI里面登記注冊,並且表明自己是為哪個工程服務的。當相應的工程發生變化時,GitLab-CI就會通知相應的工人執行軟件集成腳本。如下圖所示:
Runner可以分布在不同的主機上,同一個主機上也可以有多個Runner。
Runner類型
GitLab-Runner可以分類兩種類型:Shared Runner(共享型)和Specific Runner(指定型)。
Shared Runner:這種Runner(工人)是所有工程都能夠用的。只有系統管理員能夠創建Shared Runner。
Specific Runner:這種Runner(工人)只能為指定的工程服務。擁有該工程訪問權限的人都能夠為該工程創建Shared Runner。
本文操作目標:搭建 GitLab 以及使用 GitLab 的 CI Runner 服務,對項目進行測試。操作過一次,才知道並非想像中的那么復雜,也沒有像想中的那么簡單!
1、准備工作
Centos7 機器,2台。 一台安裝GitLab和Runner,一台用來充當Runner的操作機。
2、Docker安裝
中文文檔地址:http://www.docker.org.cn/index.html
官方網站:https://docs.docker.com/linux/started/
Docker支持的安裝方式:
Docker有很多種安裝的選擇,其中支持最好的是Ubuntu系統。我們推薦您在Ubuntu下面安裝,因為docker是在Ubuntu下面開發的,安裝包測試比較充分,可以保證軟件包的可用性。Mac, windows和其他的一些linux發行版本無法原生運行Docker,可以使用虛擬軟件創建一個ubuntu的虛擬機並在里面運行docker。
CentOS 6 上安裝后怎么最高只有 Docker 1.7 這個版本。
Docker 已經不再支持 CentOS 6 了,現在看到的是很久以前的老版本,之后再也沒有發布過 CentOS 6 的版本,所以不要再在 CentOS 6上用 Docker 了。換 CentOS 7 或者 Ubuntu 吧。
Ubuntu下載地址:http://releases.ubuntu.com/16.04.3/
CentOS7 64位下載地址:http://isoredirect.centos.org/centos/7/isos/x86_64/
Docker的安裝及基礎使用,本章不做闡述。
3、GitLab安裝
從Gitblit轉移至GitLab,發現其復雜度真不是一個數量級的。如果不需要在源碼上做很多服務,還是Gitblit用的隨心。
使用Docker安裝GitLab,找了一個9.x的中文版。
漢化的 GitLab 社區版 Docker Image
下載地址:https://hub.docker.com/r/twang2218/gitlab-ce-zh/
$ docker pull docker.io/twang2218/gitlab-ce-zh $ docker images docker.io/twang2218/gitlab-ce-zh latest b8165a7e3d68 3 months ago 1.39 GB $ docker run --detach \ --hostname 192.168.1.8 \ --publish 8082:8082 --name gitlab9 \ --restart always \ --volume /home/gitlab:/var/opt/gitlab/git-data \ docker.io/twang2218/gitlab-ce-zh $ docker ps
這里有一點要說明,端口映射,寫的是8082到8082,就是說訪問物理主機(192.168.1.8)的8082端口,相當於訪問docker主機的8082端口。
但是GitLab默認的服務端口是80,要下入docker主機,改一下配置。
# 進入docker主機的ssh $ docker exec -it gitlab9 /bin/bash # 修改 /etc/gitlab/gitlab.rb bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8) root@192:/# vim /etc/gitlab/gitlab.rb #修改項目 external_url 'http://192.168.1.8:8082'
應用配置更新
gitlab-ctl reconfigure
雷點:直接重啟GitLab服務,配置是不會更新的。
此時,訪問 http://192.168.1.8:8082,基本是OK的了。更新密碼,創建項目之流,基本不需要指導。
.gitlab-ci.yml
資料顯示,GitLab8之后的版本,默認集成,並啟用了GitLabCI,所以無需額外的任何配置。還是親兒子營養多。
唯一的要求,就是你的項目代碼里,必須有一個叫 .gitlab-ci.yml
的文件。使用GitLab管理后台,可以添加文件,添加時可以選擇對應的模板文件,還是很貼心的。有如下圖示:
文件大概的內容,就是一堆任務,包構建命令(能夠把項目需要的環境構建出來,以便運行測試)測試命令,及其它你想執行的命令。
.gitlab-ci.yml 文件內容:
# This file is a template, and might need editing before it works on your project 這個文件是一個模板,在您的項目工作之前可能需要進行編輯。 # Official framework image. Look for the different tagged releases at: 官方框架的形象。尋找不同的標記版本: # https://hub.docker.com/r/library/php image: php:latest # Pick zero or more services to be used on all builds 選擇在所有構建中使用的0或更多的服務。 # Only needed when using a docker container to run your tests in 只有在使用docker容器來運行測試時才需要。 # Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-a-service services: - mysql:latest variables: MYSQL_DATABASE: project_name MYSQL_ROOT_PASSWORD: secret # This folder is cached between builds 此文件夾在構建之間進行緩存 # http://docs.gitlab.com/ce/ci/yaml/README.html#cache cache: paths: - vendor/ - node_modules/ # This is a basic example for a gem or script which doesn't use 這是一個沒有使用的gem或腳本的基本示例 # services such as redis or postgres 諸如redis或postgres之類的服務 before_script: # Update packages 更新包 - apt-get update -yqq # Upgrade to Node 7 更新node 7 - curl -sL https://deb.nodesource.com/setup_7.x | bash - # Install dependencies 安裝依賴 - apt-get install git nodejs libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq # Install php extensions 安裝php擴展 - docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache # Install Composer and project dependencies 安裝composer和項目依賴 - curl -sS https://getcomposer.org/installer | php - php composer.phar install # Install Node dependencies 安裝node的依賴 # comment this out if you don't have a node dependency 如果沒有節點依賴,請注釋掉 - npm install # Copy over testing configuration 復制測試配置。 # Don't forget to set the database config in .env.testing correctly 不要忘記在.env中設置數據庫配置。測試正確 # DB_HOST=mysql # DB_DATABASE=project_name # DB_USERNAME=root # DB_PASSWORD=secret - cp .env.testing .env # Run npm build # comment this out if you don't have a frontend build 若果沒有前端build,請注釋掉 # you can change this to to your frontend building script like 你可以把它修改為你的前端構建腳本 # npm run build - npm run dev # Generate an application key. Re-cache. 生成一個應用程序 - php artisan key:generate - php artisan config:cache # Run database migrations. 執行數據庫遷移 - php artisan migrate # Run database seed 執行數據填充 - php artisan db:seed test: script: # run laravel tests 執行測試 - php vendor/bin/phpunit --coverage-text --colors=never # run frontend tests 執行前端測試 # if you have any task for testing frontend 如果你有測試前端的任務 # set it in your package.json script # comment this out if you don't have a frontend test 如果你沒有前端測試,請注釋掉 - npm test
這里直接使用Laravel模板。然后需要針對自己的系統,對腳本微調:
- apt-get替換成yum。因為我的目標機是centos7
- 刪除docker相關的內容。因為我的目標操作機沒有安裝docker,是一個台空白機
- 將 https 替換成 http
- 增加tags laravel,(很重要)增加完如下
test: # 下兩行 很重要 tags: - laravel script: # 進入后台項目目錄 - cd backend # run laravel tests - php vendor/bin/phpunit --coverage-text --colors=never
說明:腳本的任何錯誤,將造成任務運行失敗
GitLab管理端,也可以看到此文件的語法檢查:
4、GitLab - Runner 安裝 注冊
先說一下大致流程:
1,安裝runner
2,runner注冊到 GitLab
3,當代碼提交后,GitLab根據 .yml的配置,通知runner,起來,干活啦
4,runner收到任務,開始執行作業
5,GitLab接收並顯示 runner 的運行結果
Runner的安裝
GitLab Runner 可以在GNU / Linux,macOS,FreeBSD和Windows上安裝和使用。有三種安裝方法。使用Docker,手動下載二進制文件,或者使用rpm / deb軟件包的存儲庫。
以下可以找到有關不同安裝方法的信息:https://docs.gitlab.com/runner/install/
這里使用Docker的安裝方式:
無腦執行 $ docker pull gitlab/gitlab-runner:latest $ docker run -d --name gitlab-runner --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest
下面開始實施注冊

簡單一點,我們搞一個特定的Runners,頁面內會提供注冊需要的url和密鑰:
2,執行注冊命令
需要進入運行runner的docker主機,執行相關命令,過程如下:
# 進入docker主機的shell $ docker exec -it gitlab-runner /bin/bash # 注冊命令 root@94652dec6e02:/# gitlab-ci-multi-runner register Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://192.168.1.8:8082/ Please enter the gitlab-ci token for this runner: zQRBTAAmh1Zc9BxU6G61 Please enter the gitlab-ci description for this runner: [94652dec6e02]: just for test Please enter the gitlab-ci tags for this runner (comma separated): laravel Whether to run untagged builds [true/false]: [false]: Whether to lock the Runner to current project [true/false]: [true]: Registering runner... succeeded runner=zQRBTAAm Please enter the executor: docker-ssh+machine, kubernetes, docker, docker-ssh, shell, ssh, parallels, virtualbox, docker+machine: ssh Please enter the SSH server address (e.g. my.server.com): 192.168.1.4 Please enter the SSH server port (e.g. 22): Please enter the SSH user (e.g. root): xx Please enter the SSH password (e.g. docker.io): xx Please enter path to SSH identity file (e.g. /home/user/.ssh/id_rsa): Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
其中的 tags 非常重要。 要和 .yml文件中的tags一致,否則會出來諸如“未找到有效的Runner”等錯誤提示,使流水線作業擱置
可以檢查本地注冊情況:
# gitlab-runner list Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml first test Executor=ssh Token=e23c4a3320a90c3e81074de0f87fdc URL=http://192.168.1.8:8082/ just for test Executor=ssh Token=7ff71f37a5c9b108dbcb2234b574f2 URL=http://192.168.1.8:8082/
然后不要忘記,要啟動runner服務:
# gitlab-runner start
這里我選擇了ssh
的方式。此方式相當於給runner找一個『肉機』去跑項目的集成測試代碼。其它方式待研究
同時,注冊成功之后,GitLab管理后台,也可以看到注冊信息
5、測試
文行致此,已萬事俱備
push你的代碼至版本庫,GitLabCI即開始工作,如果你想看到綠色的成功圖標,根據提示一步一步調試你的 .yml腳本吧
作者:勤勞一沙鷗
鏈接:http://www.jianshu.com/p/238714700b67
來源:簡書