Gitlab – .gitlab-ci.yml (Job 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 可以在 Job 定義 Rules 來避免太頻繁的 pipelines 工作.

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

  • rules: if
    需搭配 Variables 來做條件式判斷,也可利用 when 一併使用.

    variables:
      $Deploy_DISABLED : 'True'
    
    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"
      rules:
        - if: '$BUILD_DISABLED'
          when: never
    

    說明:

    • 多增加 $Deploy_DISABLED 變數 (variables ) 值為 True
      variables:
        $Deploy_DISABLED : 'True'
      
    • 在 deploy stage 會判斷 $Deploy_DISABLED 變數 (variables ) 值是否為 True (就不執行 when: never)
      deploy-job:
        stage: deploy
        script:
          - echo "running scripts in the deploy job"
        rules:
          - if: '$BUILD_DISABLED'
            when: never
      
  • rules: changes
    該檔案有被變更時才會產生 pipeline .
  • rules: exists
    在 Project 根目錄下有這個檔案才會產生 pipeline .

    stages:
      - build
      - test
      - deploy
     
    default:
      tags:
        - VM-Win10-PowerShell
      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"
      rules:
        - exists:
            - Image23.img
    
    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"
    

    說明:
    在 Project 根目錄下有 Image23.img 這個檔案才會執行該 Job .

    build-job:
      stage: build
      script:
        - echo "running scripts in the build job"
      rules:
        - exists:
            - Image23.img
    
  • when
    Can only be always or never when used with workflow.
  • variables
沒有解決問題,試試搜尋本站其他內容

發佈留言

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

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