Gitlab – .gitlab-ci.ym (Workflow Rules)

Loading

上傳到 GitLab 的程式有辦法自行進行測試嗎? 可以透過 CI/CD 的功能. 我們須編輯 .gitlab-ci.yml 檔案來指定整個流程

下面的範例使用 Windows 當作 Runner , 須先建立好 Gitlab Runner – Windows shell ( PowerShell) – https://benjr.tw/106161

將我們的專案 .gitlab-ci.yml 設定如下( 範例是 參考文章 https://editor.leonh.space/2022/gitlab-ci/ ).

stages:
  - build
  - test
  - deploy

default:
  tags:
    - windows
  before_script:
    - Set-Variable -Name "time" -Value (date -Format "%H:%m")
    - echo ${time}
    - echo "started by ${GITLAB_USER_NAME}"

build-job:
  stage: build
  script:
    - echo "running scripts in the build job"

test-job1:
  stage: test
  script:
    - echo "running scripts in the test job 1"

test-job2:
  stage: test
  script:
    - echo "running scripts in the test job 2"

deploy-job:
  stage: deploy
  script:
    - echo "running scripts in the deploy job"

詳細說明請參考 – https://benjr.tw/106230

但這邊有個問題是預設只要 Project 內的程式有更新就會建立 pipeline 來進行測試,如果需要指定狀況才執行 pipeline 可以透過定義 workflow 來避免太頻繁的 pipelines 工作.

在原先 .gitlab-ci.yml 新增加 workflow 區塊.

stages:
  - build
  - test
  - deploy

default:
  tags:
    - windows
  before_script:
    - Set-Variable -Name "time" -Value (date -Format "%H:%m")
    - echo ${time}
    - echo "started by ${GITLAB_USER_NAME}"

build-job:
  stage: build
  script:
    - echo "running scripts in the build job"

test-job1:
  stage: test
  script:
    - echo "running scripts in the test job 1"

test-job2:
  stage: test
  script:
    - echo "running scripts in the test job 2"

deploy-job:
  stage: deploy
  script:
    - echo "running scripts in the deploy job"

workflow:
  rules:
    - exists:
        - Image23.img

在 workflow 區塊指定在什麼情況下會產生新 pipeline 來執行所有的 Jobs (用來避免太頻繁的 pipelines 工作).

workflow:
  rules:
    - exists:
        - Image23.img

有以下參數可用,詳細請參考 – https://docs.gitlab.com/ee/ci/yaml/index.html#rules

  • rules: if
    需搭配 Variables 來做條件式判斷.
  • rules: changes
    該檔案有被變更時才會產生 pipeline .
  • rules: exists
    在 Project 根目錄下有這個檔案才會產生 pipeline .
  • when
    Can only be always or never when used with workflow.
  • variables

這些關鍵字也都可以定義在 Job 上做更精細的控制. 詳細請參考 Job Rules – https://benjr.tw/106258

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料