GitLab Runner 搭建指南:打造流暢的 CI/CD 工作流程

閱讀時間約 11 分鐘

在上一篇文章中,我們完成了Gitlab的安裝,但為了未來的CI/CD的作業,接下來就要進行Gitlab runner的建置,我們需要透過runner來執行與部署任務。

工作流程的實際操作

官方對於gitlab runner的定義如下:

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline

本篇將說明的以下部分:

  1. 部署Gitlab runner
  2. 向Gitlab進行註冊
  3. 簡單的CI/CD測試
  4. 結論

1. 部署Gitlab runner

安裝的方式很多,為了配合之前的文章,本篇同樣採用Docker-compose的方式來進行部署。

同一台主機安裝時,須注意:

  • Docker runner如果使用Docker Executor,需要使用指定的網路
  • 如果Gitlab Hostname使用localhost,註冊runner時,要特別指定clone_url
  • 如果不是使用80 Port,建議用官方做法
  • 注意Docker network,不然可能連不到GitLab
#--------------------------------------------------------
# S1-1. 修改原先的docker-compose, 加入runner
#--------------------------------------------------------
[root]# vim docker-compose.yaml
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
hostname: 'gitlab.test.example.poc'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab.test.example.poc:8929'
gitlab_rails['gitlab_shell_ssh_port'] = 2424
ports:
- '8929:8929'
- '8443:8443'
- '2424:2424'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
- '/var/run/docker.sock:/var/run/docker.sock'
shm_size: '256m'
gitlab-runner:
image: gitlab/gitlab-runner:latest
container_name: gitlab-runner
restart: always
networks:
- devops-net
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
- '$GITLAB_HOME/gitlab-runner:/etc/gitlab-runner'

[root]# docker-compose up -d
[root]# docker ps
[root]# docker logs -f gitlab-runner
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml
=> 需要進行註冊,取得token
#---------------------------------------------------
# S1-2. 登入Web
#---------------------------------------------------
http://gitlab.test.example.poc:8929
=> 修改初始密碼 (root/P@ssw0rd)
#---------------------------------------------------
# S1-3. 建立Group
#---------------------------------------------------
首頁 => "+" (Create group) => Name: testgroup
raw-image

2.向Gitlab進行註冊

#---------------------------------------------------
# S2-1. 取得給runner使用的token
#---------------------------------------------------
Build => Runners => 記住token
TOKEN: GR1348941ZRdhocL1zyrdBaL1WUyC
raw-image
#---------------------------------------------------
# S2-5. 註冊
#---------------------------------------------------
[root]# docker exec -it gitlab-runner gitlab-runner register
Enter the GitLab instance URL (for example, https://gitlab.com/): http://gitlab.test.example.poc:8929
Enter the registration token: GR1348941ZRdhocL1zyrdBaL1WUyC
Enter a description for the runner: test
Enter tags for the runner (comma-separated): test
Enter optional maintenance note for the runner: test
Enter an executor: custom: docker
Enter the default Docker image: python:3.9.13
raw-image
#---------------------------------------------------
# S2-6. 確認
#---------------------------------------------------
首頁 => 團隊 => Build => Runners => 看到Runner資訊
raw-image

3.簡單的CI/CD測試

#---------------------------------------------------
# S3-1. Create project
#---------------------------------------------------
Project => Create blank project => name: test-cicd => create
raw-image
#---------------------------------------------------
# S3-2. Create new file
#---------------------------------------------------
"+" => New File => 輸入以下內容 => commit change
stages:
- Welcome_test_cicd

Job_Hello_world:
tags:
- test
stage: Welcome_test_cicd
image: python:3.9.13
script:
- python3 -V

※ 說明:
tags:定義將使用的GitLab-Runner所帶有的標籤。
stage:定義這個任務將在哪個階段執行。
image:定義將使用哪一個docker映像檔建立容器並執行任務。(此項為Docker Executor才需要宣告。)
script:定義要執行的指令。
raw-image
raw-image
#---------------------------------------------------
# S3-3. Verify pipeline
#---------------------------------------------------
Build => Pipelines => (click)Passed
raw-image
raw-image
raw-image

4.結論

以上我們就完成了以Gitlab為基礎的概念完成了最簡單的Pipeline的測試,並且也實際操作了在Gitlab UI進行的Pipeline設定與執行。

一般實務上,許多Kubernetes的工程師在管理工作範圍內,也可能會牽涉到Pipeline的維護,所以建議學習上至少要掌握一種以上的Pipeline工具,可以更全面的滿足實務上的需求,而不是只能專注在基礎架構上的維護。


※ References:

10會員
40內容數
記錄IT社畜的自我學習筆記,如同專題名稱,主要是怕自已忘記自已做過什麼、學到什麼。索性就分享我自已在學習Kubernetes這條路上的各種測試、學習心得。
留言0
查看全部
發表第一個留言支持創作者!