iT邦幫忙

2025 iThome 鐵人賽

DAY 7
2

之前的幾天,介紹了關於 inputs 的使用,有提到 inputs 是 GitLab CI/CD Components 的一部分,因此,在接下來會開始更深入的介紹 GitLab CI/CD 的 Components。
GitLab CI/CD Components 是從 GitLab 16.0 開始進行實驗階段,而後在 GitLab 17.0 正式釋出,Components 除了之前提到的 inputs 用來定義 Component 的輸入規格優點以外,還有一些優點:

  • Components 有一個專屬的發布平台名為 CI/CD Catalog,開發者可以把自己的元件發佈到這個平台上,使用者也可以從平台上尋找別人公開發佈出來的 Pipeline 元件來使用。
  • Components 可以基於版本號做釋出及使用,其大大的避免 GitLab CI/CD YAML 在重複使用時,版本管理上的問題。
  • 一個 GitLab 專案中,可以有1 到 30 個(GitLab 16.9) Components,一起做版本控管。
    以下,我們就開始認識 GitLab CI/CD Components。

如何使用

首先,這邊要先透過 GitLab 官方提供的 Component Template 來建立第一個 CI/CD Component:

1. 建立 CI/CD Component 專案

  • 透過 GitLab 的新建專案(New Project),選擇「Create from template」:

Create from template

  • 在比較下方的位置,找到 「GitLab CI/Cd Components」選擇 「Use template」,開始使用這個 Template。

GitLab CI/Cd Components

  • 接著輸入專案所需的一些資訊,這邊以「Hello Component 2025」為例,輸入完相關資訊後,點選「Create project」

Create project

  • 完成 CI/CD Component 專案的建立
    在建立專案中,可以看到這個 Template 中已經包含了一個 templates 資料夾及其元件與相關 .gitlab-ci.yml 檔案。

    ── templates/
       └── my-component.yml
    ── LICENSE.md
    ── README.md
    ── .gitlab-ci.yml
    ``
    
    
  • 這是一個已經可以使用的 Component 專案,接著只要為專案建立版本 Git Tag,就可以提供給其他使用者使用(沒有上 Tag 也可以透過 Git SHA 使用)。這邊我們為目前的主分支建立一個 Tag 1.0.0。建立完畢後,會觸發一個釋出版本的 Pipeline,這時候其他專案就可以以版本號 1.0.0 使用這個專案的 Component。

Component Version Create Pipeline

2. 開始使用 CI/CD Component

有了可以使用的 Component,接著試著使用這個 CI/CD Component,在另外的專案中,編輯 .gitlab-ci.yml,可以使用以下的範例

include:
  - component: $CI_SERVER_FQDN/mo-playground/gitlab-ci-example-2025/hello-component-2025/my-component@384cbe6028c44ae286c3d3a9e2f71276c6132510
    inputs:
      stage: build
  - component: $CI_SERVER_FQDN/mo-playground/gitlab-ci-example-2025/hello-component-2025/my-component@1.0.0
    inputs:
      stage: build
      job_name: job_from_version

在完成編輯後,換看到觸發了一個 pipeline ,這個 Pipeline 中會有兩個 Job,分別是 job-templatejob_from_version,其執行的結果會是:

$ echo "Starting job job-template"
Starting job job-template

$ echo "Starting job job_from_version"
Starting job job_from_version

A pipeline with two job

範例說明

1. 從使用 Component 端的專案開始說起:

從範例中 include 的內容中有兩個 component,其對應的數值為 Component 的路徑,Component 的專案路徑格式為:

<fully-qualified-domain-name>/<project-path>/<component-name>@<specific-version>
  • fully-qualified-domain-name:以 GitLib.com 來說,就是 https://gitlab.com,GitLab 也有提供一個 pre-defined 變數$CI_SERVER_FQDN
  • project-path:專案路徑,以這次的 Hello Component 範例來說,就是 mo-playground/gitlab-ci-example-2025/hello-component-2025,查看路徑最快的方法就是看目前 GitLab 的網址上連結。
  • component-name:以 Hello Component 範例中,templates 資料夾,裡頭有一個 my-component.yml 則這個檔案的檔案名稱,即為 component-name。
  • specific-version:版本號的部分,可以是使用的 Component 對應的 Git SHA,如上面第一個引入 component 的方法:384cbe6028c44ae286c3d3a9e2f71276c6132510,也可以如第二個引入的 component 使用的方法,直接使用版本號 1.0.0 這個 Tag。

2. 回到 Hello Component 2025 專案:

這個專案是由 GitLab 官方提供的 Component Template 直接 Import 而來,是一個已經包含 Component 的基本樣貌,如基礎元件 my-component.yml 並且在 .gitlab-ci.yml 中也可以看到對於這個元件的測試、版本釋出等工作。
一個 Component 專案,必須要包含:

  • README.md:Markdown 格式的專案說明檔
  • 置於專案跟目錄的 templates/ 資料夾,裡頭用來置放 CI/CD Component。
  • component 可以是單一個檔案,如這個範例中的 my-component.yml,也可以是一個資料夾的形式,例如資料夾名稱 my-other-component,以資料夾的形式建立 Component,則其資料夾內必須要有一個 template.yml 用來撰寫該 component。
    Component 專案另外通常也會加入:
  • .gitlab-ci.yml 用來做 Component 的 CI/CD,如 Component 測試或版本釋出。
  • LICENSE.md 用來宣告這個 Component 的授權模式。

總結

在今天的案例中,初探 GitLab CI/CD Component 的樣貌,包含了建立第一個 Component,還有在另外的專案中使用這個剛建立好的 Component,也稍微描述了可能可以怎麼使用,還有一個 Component 基礎需要包含的檔案及結構初探。接下來的內容,我們會更深入來了解 GitLab CI/CD Component。我是墨嗓(陳佑竹),期待這次的內容能帶給你實用的啟發與幫助。

參考範例

參考連結


上一篇
Day06 - 用 GitLab 取值函式 操縱 input 的輸入值
下一篇
Day08 - 在 GitLab Catalog 平台上找需要的 Components
系列文
GitLab CI 2025:深入玩轉流水線與實戰紀錄11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言