如何在Anolis 8 安裝 極狐GitLab Runner

极狐GitLab發表於2024-03-12

GitLab 是一個全球知名的一體化 DevOps 平臺,很多人都透過私有化部署 GitLab 來進行原始碼託管。極狐GitLab 是 GitLab 在中國的發行版,專門為中國程式設計師服務。可以一鍵式部署極狐GitLab。

基於 Anolis 龍蜥 8 和 極狐GitLab 官方倉庫指令碼 安裝/配置 極狐GitLab Runner

說明

  1. 我們先了解在龍蜥 Anolis 8 中安裝 極狐GitLab]

安裝

  1. 嘗試獲取結果

    # 使用 curl 需要在請求頭中新增標識 User-Agent
    sudo curl 'https://packages.gitlab.com/install/repositories/runner/gitlab-runner/config_file.repo?os=centos&dist=8&source=script' --header 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
    
  2. yum源下載到 /etc/yum.repos.d/runner_gitlab-runner.repo

    # 使用 curl 需要在請求頭中新增標識 User-Agent
    sudo curl 'https://packages.gitlab.com/install/repositories/runner/gitlab-runner/config_file.repo?os=centos&dist=8&source=script' --header 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' > /etc/yum.repos.d/runner_gitlab-runner.repo
    
  3. 更新yum源(首次更新,可能會提示需要下載並匯入 gpgkey)

    # 更新yum源
    yum makecache
    # yum makecache -y
    
    # 清空yum源後再更新yum源
    # yum clean all && yum makecache
    # yum clean all && yum makecache -y
    
    # 更新命令及結果,為了方便,使用了 -y 進行確定
    
    [root@alios8 ~]# yum makecache -y
    AnolisOS-8 - AppStream                                                                                                     5.5 MB/s |  10 MB     00:01    
    AnolisOS-8 - BaseOS                                                                                                        4.4 MB/s | 7.9 MB     00:01    
    AnolisOS-8 - PowerTools                                                                                                    4.8 MB/s | 2.1 MB     00:00    
    runner_gitlab-runner                                                                                                       138  B/s | 862  B     00:06    
    runner_gitlab-runner                                                                                                       1.1 kB/s | 3.1 kB     00:02    
    Importing GPG key 0x51312F3F:
     Userid     : "GitLab B.V. (package repository signing key) <packages@gitlab.com>"
     Fingerprint: F640 3F65 44A3 8863 DAA0 B6E0 3F01 618A 5131 2F3F
     From       : https://packages.gitlab.com/runner/gitlab-runner/gpgkey
    runner_gitlab-runner                                                                                                       966  B/s | 3.1 kB     00:03    
    Importing GPG key 0x35DFA027:
     Userid     : "GitLab, Inc. <support@gitlab.com>"
     Fingerprint: 09E5 7083 F34C CA94 D541 BC58 A674 BF81 35DF A027
     From       : https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-4C80FB51394521E9.pub.gpg
    runner_gitlab-runner                                                                                                       1.0 kB/s |  12 kB     00:11    
    runner_gitlab-runner-source                                                                                                 65  B/s | 862  B     00:13    
    runner_gitlab-runner-source                                                                                                773  B/s | 3.1 kB     00:04    
    Importing GPG key 0x51312F3F:
     Userid     : "GitLab B.V. (package repository signing key) <packages@gitlab.com>"
     Fingerprint: F640 3F65 44A3 8863 DAA0 B6E0 3F01 618A 5131 2F3F
     From       : https://packages.gitlab.com/runner/gitlab-runner/gpgkey
    runner_gitlab-runner-source                                                                                                1.0 kB/s | 3.1 kB     00:03    
    Importing GPG key 0x35DFA027:
     Userid     : "GitLab, Inc. <support@gitlab.com>"
     Fingerprint: 09E5 7083 F34C CA94 D541 BC58 A674 BF81 35DF A027
     From       : https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-4C80FB51394521E9.pub.gpg
    runner_gitlab-runner-source                                                                                                 31  B/s | 296  B     00:09    
    Metadata cache created.
    [root@alios8 ~]# 
    
  4. 安裝

    yum -y install gitlab-runner
    

配置

  1. 配置證書信任
```
# 自己生成的證書,如果不進行信任,註冊時可能出現的問題:
# ERROR: Registering runner... failed                 runner=6iuLhyWx status=couldn't execute POST against https://gitlab.example.com/api/v4/runners: Post "https://gitlab.example.com/api/v4/runners": x509: certificate is not valid for any names, but wanted to match gitlab.example.com

# 下列執行時,需要本機能使用 GitLab 域名訪問
# 可以修改 本機 /etc/hosts 檔案解決

# 如果域名未解析,會出現
# ERROR: Registering runner... failed                 runner=z4uLTqoa status=couldn't execute POST against https://gitlab.example.com/api/v4/runners: Post "https://gitlab.example.com/api/v4/runners": dial tcp: lookup gitlab.example.com on 192.168.61.2:53: no such host

# 如果建立證書時,未設定“Common Name (e.g. server FQDN or YOUR name) []”時,會出現
# ERROR: Registering runner... failed                 runner=z4uLTqoa status=couldn't execute POST against https://gitlab.example.com/api/v4/runners: Post "https://gitlab.example.com/api/v4/runners": x509: certificate is not valid for any names, but wanted to match gitlab.example.com

# 重新設定了域名證書,需要執行 sudo gitlab-ctl restart,如果修改了 /etc/gitlab/gitlab.rb 檔案,需要先執行 sudo gitlab-ctl reconfigure,再執行 sudo gitlab-ctl restart

# 生成的證書不滿足GitLab Runner的檢查時,會出現下列錯誤,請根據前面章節中的內容重新生成
# ERROR: Registering runner... failed                 runner=z4uLTqoa status=couldn't execute POST against https://gitlab.example.com/api/v4/runners: Post "https://gitlab.example.com/api/v4/runners": x509: certificate relies on legacy Common Name field, use SANs instead

# 如果域名證書未生效,或者已過期,會出現下面的錯誤
# x509: certificate has expired or is not yet valid: current time 2022-11-15T20:45:12+08:00 is before 2022-11-15T19:49:27Z

#
#

sudo mkdir -p /etc/gitlab-runner/certs

# 本文使用域名是IP:192.168.80.14
# 可根據自己的需要,修改下方的域名及埠

# 使用客戶端下載 GitLab 例項的證書
openssl s_client -showcerts -connect 192.168.80.14:443 -servername 192.168.80.14 < /dev/null 2>/dev/null | openssl x509 -outform PEM > /etc/gitlab-runner/certs/192.168.80.14.crt
# openssl s_client -showcerts -connect gitlab.example.com:443 -servername gitlab.example.com < /dev/null 2>/dev/null | openssl x509 -outform PEM > /etc/gitlab-runner/certs/gitlab.example.com.crt

# 驗證檔案是否已正確安裝
echo | openssl s_client -CAfile /etc/gitlab-runner/certs/192.168.80.14.crt -connect 192.168.80.14:443 -servername 192.168.80.14
# echo | openssl s_client -CAfile /etc/gitlab-runner/certs/gitlab.example.com.crt -connect gitlab.example.com:443 -servername gitlab.example.com
```
  1. 註冊 極狐GitLab Runner

    gitlab-runner register
    
  2. 提示:Enter the GitLab instance URL:

    輸入極狐GitLab 的地址

    https://192.168.80.14
    # https://gitlab.example.com/
    
  3. 提示:Enter the registration token:

    # 複製 https://192.168.80.14/admin/runners 頁面中的 token(點選:註冊一個Runner即可獲取)
    # 複製 https://gitlab.example.com/admin/runners 頁面中的 token(點選:註冊一個Runner即可獲取)
    6iuLhyWxqypcyaNtUG_C
    
  4. 提示:Enter a description for the runner:

    輸入流水線的名稱,預設為 CentOS 主機名

  5. 提示:Enter tags for the runner (comma-separated):

    輸入流水線的標籤名(非Git倉庫的標籤),即:.gitlab-ci.yml 檔案中標籤,可為空,**為空時允許所有未指定標籤的流水線使用,可在GitLab頁面中進行隨時調整 **

  6. 提示:Enter optional maintenance note for the runner:

    輸入流水線的描述,可為空

  7. 提示:Registering runner... succeeded runner=6iuLhyWx
    Enter an executor: docker, docker-ssh, parallels, shell, docker-ssh+machine, instance, kubernetes, custom, ssh, virtualbox, docker+machine:

    選擇流水線執行器

    # 輸入 docker,用於後面的 GitLab Pages 做準備
    docker
    
  8. 提示:Enter the default Docker image (for example, ruby:2.7):

    輸入預設docker映象

  9. 出現下列語句,說明流水線已註冊成功: /etc/gitlab-runner/config.toml 是GitLab Runner配置檔案的位置

    Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
     
    Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml" 
    
  10. 修改流水線配置,使其同時可以執行多個任務

    vim /etc/gitlab-runner/config.toml
    
    # 同一時間支援執行的最大任務數
    concurrent = 10
    
  11. 在以下地址中可看到流水線的狀態

    1. <h ttps://gitlab .example. com/admin/runners>
    2. <h ttps://192.168.61.129/admin/runners>
  12. 列出所有

    gitlab-runner list
    
  13. 檢視狀態

    gitlab-runner status
    
  14. 停止

    gitlab-runner stop
    
  15. 啟動

    gitlab-runner start
    

更多關於極狐GitLab 的最佳實踐,請搜尋關注【極狐GitLab】公眾號或者登入極狐GitLab 官網 進行學習。

相關文章