Gitlab – .gitlab-ci.yml (Anchors , needs , when)

Loading

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

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

將我們的專案 .gitlab-ci.yml 設定如下( 範例是 參考文章 https://stackoverflow.com/questions/64205410/gitlab-ci-specify-that-job-c-should-run-after-job-b-if-job-a-fails ).

這個範例可以用來控制 CI/CD 的流程.

stages:
  - deploy
  - test
  - cleanup

default:
  tags:
    - VM-Win10-PowerShell
    
deploy_job:
  stage: deploy
  script:
    - echo "Deployed"
  when: always

test_job:
  stage: test
  script:
    - echo Executing tests
  when: on_success

# a YAML anchor reduces repetition
.cleanup_job: &cleanup_job
  stage: cleanup
  script:
    - echo Cleaned up deployment

cleanup_deployment_success:
  when: on_success
  <<: *cleanup_job

cleanup_deployment_failure:
  needs: ["test_job"]
  when: on_failure
  <<: *cleanup_job

執行結果

cleanup 的流程為

  • test_job 成功時執行 cleanup_deployment_success
  • test_job 失敗時執行 cleanup_deployment_failure

說明:

  • Anchors
    Anchors 可以定義重覆的動作讓其他 Job 來呼叫,詳細說明請參考 https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#anchors

    .cleanup_job: &cleanup_job
      stage: cleanup
      script:
        - echo Cleaned up deployment
    

    . 開頭的表示為隱藏的 yaml 設定.後面的 &cleanup_job 表示為 Anchors 的名稱,其他 Job 要使用時用這個名稱.

    .cleanup_job: &cleanup_job
    

    在 cleanup_deployment_success 與 cleanup_deployment_failure 都有使用到 &cleanup_job 這個 Anchors ,格式如下.

      <<: *cleanup_job
    

    執行時實際內容會翻譯如下

    cleanup_deployment_success:
      when: on_success
      stage: cleanup
      script:
        - echo Cleaned up deployment
    
    cleanup_deployment_failure:
      needs: ["test_job"]
      when: on_failure
      stage: cleanup
      script:
        - echo Cleaned up deployment
    
  • needs
    範例裡定義 Cleanup Stage 有 2 個 Job ( cleanup_deployment_success 與 cleanup_deployment_failure ) ,會依序來執行,但我們希望後面的 cleanup_deployment_failure 不用等待 cleanup_deployment_success 執行完才執行,透過 needs 就可以指定完成哪個 Job 之後就可以執行了. ,詳細說明請參考 https://docs.gitlab.com/ee/ci/yaml/#needs

    cleanup_deployment_failure:
      needs: ["test_job"]
      when: on_failure
      <<: *cleanup_job
    

    在完成 test_job 之後就會執行 cleanup_deployment_failure 這個 Job.

  • when
  • 使用 when 來決定該 Job 在哪些情況才執行.

    • on_success (default): Run the job only when no jobs in earlier stages fail or have allow_failure: true.
    • on_failure: Run the job only when at least one job in an earlier stage fails. A job in an earlier stage with allow_failure: true is always considered successful.
    • never: Don’t run the job regardless of the status of jobs in earlier stages. Can only be used in a rules section or workflow: rules.
    • always: Run the job regardless of the status of jobs in earlier stages. Can also be used in workflow:rules.
    • manual: Run the job only when triggered manually.
    • delayed: Delay the execution of a job for a specified duration.
沒有解決問題,試試搜尋本站其他內容

發佈留言

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

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