Mac環境中建置Gitlab-runner

目前在開發環境中,自動化整合與佈署已經是不可或缺的工作,而Gitlab有提供了自動化整合,讓程式碼異動commit進repo時能自動執行啟動,然後gitlab-runner會把能夠執行的job抓下來執行並把結果回傳到gitlab,來達到自動化測試與自動化佈署

install gitlab-runner

在安裝gitlab-runner 環境,因為是需要使用個人帳號執行 pod install 並且需要自動登入,否則會遇到 “gitlab-runner is not running“的問題,所以建議是先在Mac環境中建立獨立的帳號,以下先安裝homebrew 套件,再使用homebrew 安裝gitlab-runner

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install gitlab-runner
$ brew services start gitlab-runner

建立gitlab-runner 資料夾

$ mkdir ~/gitlab_runner
$ cd ~/gitlab_runner

Register gitlab-runner

在註冊gitlab-runner 之前需要先跟gitlab 取得hostname 與token
在Project 的setting 中有個CI/CD , 再打開runners 的設定後,可以取得hostname 和token

$ gitlab-runner register
Runtime platform                                    arch=amd64 os=darwin pid=36852 revision=003fe500 version=12.7.1
WARNING: Running in user-mode.
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://kevinliao.myds.me:30000/
Please enter the gitlab-ci token for this runner:
WwLj3BzUN9sdgtgzziZw
Please enter the gitlab-ci description for this runner:
[Chienyus-iMac.local]: ios-runner
Please enter the gitlab-ci tags for this runner (comma separated):
ios,android,react-native
Registering runner... succeeded                     runner=WwLj3BzU
Please enter the executor: docker-ssh, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, custom, parallels, kubernetes, docker:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

這裡有個很重要的一點,就是不能使用sudo 去跑system-mode 因為pod install 要求使用user-mode , 如果使用system-mode 會報錯而無法執行。
所以 Gitlab-runner for Mac 不能使用ssh 進行操作 要用 terminal GUI

  • Please enter the gitlab-ci description for this runner 輸入runner 的名稱
  • Please enter the gitlab-ci tags for this runner (comma separated) 輸入runner 的tag 後續可以在yml 檔中使用tag讓特定的runner來執行
  • Please enter the executor: docker-ssh, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, custom, parallels, kubernetes, docker 因為我們這裡使用shell 來執行

Registering runer … succeeded 後可以看到gitlab-runner 已經成功,如果不能可以使用以下指令檢查gitlab-runner 狀態

$ gitlab-runner install //自動執行gitlab-runner service
$ gitlab-runner staus  // 目前運作狀態
$ gitlab-runner status gitlab-runner list //列出所有的runner
$ gitlab-runner verify --delete //重新驗證所有的runner,並移除無法使用的

專案加入.gitlab-ci.yml 檔案

stages:
  - test

variables:
  LC_ALL: "en_US.UTF-8"

before_script:
- pod install

test:
 stage: test
 script:
    - xcodebuild clean build -workspace helloCI.xcworkspace -scheme helloCI -allowProvisioningUpdates
    - xcodebuild test -workspace helloCI.xcworkspace -scheme helloCI -destination 'platform=iOS Simulator,name=iPhone 11,OS=13.3' | xcpretty -s
 tags:
  - ios

使用Docker 建置 GitLab CI

最近有需求在Mac環境中,使用Docker建置Gitlab的開發環境與CI /CD , 所以記錄一下相關的步驟與細節,Gitlab是一個用Ruby on Rails 開發的開源項目,可以通過web 界面進行專案管理與測試。

使用Docker 佈署可以很容易進行安裝Gitlab與版本管理。

安裝Docker Desktop

https://docs.docker.com/docker-for-mac/install/

  • 點擊Docker.app進行安裝

啟動後會提示您使用系統密碼授權Docker.app。 需要特權訪問才能安裝網絡組件和Docker應用程序的鏈接。

頂部狀態欄中的Docker菜單表示Docker Desktop正在運行,並且可以從終端進行訪問。

如果您剛剛安裝了該應用程序,則還會收到一條消息,其中包含建議的後續步驟以及指向文檔的鏈接。 單擊Docker menu以消除此彈出通知。

  • 開啟Teminal 確認docker 指令可以正常
$ docker -v

下載Gitlab image

$ docker pull gitlab/gitlab-ce

https://docs.gitlab.com/omnibus/docker/

建立Gitlab Container

$   sudo docker run -d \
    --hostname xxxx.xxxx.xx \
    --name gitlab \
    --restart always \
    --publish 30001:22 --publish 30000:80 --publish 30002:443 \
    --volume ~/gitlab/data:/var/opt/gitlab \
    --volume ~/gitlab/logs:/var/log/gitlab \
    --volume ~/gitlab/config:/etc/gitlab \
    gitlab/gitlab-ce
  • hostname 建立gitlab 的位置可使用domain or ip
  • name container 名稱
  • restart 設定為docker 服務啟動後自動重啟
  • publish 將主機的30000, 30001, 30002 對應到 80, 22, 443
  • volume 將gitlab 的檔案對應到實體主機的路徑,container 刪除後重建與更新後不影響系統資料

修改gitlab.rb 文件

因為已經有把gitlab.rb 文件對應到 ~/gitlab/config

$ vi ~/gitlab/config/gitlab.rb

修改的內容如下:

  • external_url ‘http://hostname:30000
  • gitlab_rails[‘gitlab_ssh_host’] = ‘hostname’
  • gitlab_rails[‘gitlab_shell_ssh_port’] = 30001
  • nginx[‘listen_port’] = 80

修改email 的配置

一樣在gitlab.rb 中增加smtp的配置

gitlab_rails[‘gitlab_email_from’] = “xxxx@163.com”  
gitlab_rails[‘gitlab_email_reply_to’] = ‘xxxx@163.com‘  
gitlab_rails[‘smtp_enable’] = true
gitlab_rails[‘smtp_address’] = “smtp.163.com
gitlab_rails[‘smtp_port’] = 465 
gitlab_rails[‘smtp_user_name’] = “xxxx@163.com”  
gitlab_rails[‘smtp_password’] = “xxxxpassword”   gitlab_rails[‘smtp_domain’] = “163.com”   gitlab_rails[‘smtp_authentication’] = “login”          gitlab_rails[‘smtp_enable_starttls_auto’] = true 
gitlab_rails[‘smtp_tls’] =true    gitlab_rails[‘smtp_openssl_verify_mode’] = “peer” 

重啟gitlab service

$ sudo docker exec -it gitlab /bin/bash
$ gitlab-ctl reconfigure