GitLab CI 是 GitLab 提供的 CI/CD 服務,主要透過 .gitlab-ci.yml 設定。
Pipeline 由 job 和 stage 組成:
Job 會由 runner 執行,當 runner 數量夠多時,在同一個 stage 裡的 job 會被不同的 runner 平行執行。
只有當一個 stage 中的所有 job 都成功執行完成後,pipeline 才會移動到下個 stage;只要其中一個 job 出錯,下一個 stage 就不會執行,pipeline 也會提早結束。
典型的 pipeline 可能的組成有:
build,包含 job compile 或 buildtest,包含 job testdeploy,包含 job deploy-to-production藉由 GitLab CI 官方提供的 keyword,可以達成多樣化的 pipeline 設定。
有些 job 會產出成果,像是 e2e 測試工具 Cypress 會在測試完成後自動產生螢幕錄影檔,這時就可以在 job 中定義 artifacts,在 job 執行完成後,就會執行一段 Uploading artifacts 的 script,其目的就是將成果檔案上傳至 GitLab Server,最後在頁面的右側就可以看到 Job artifacts 的區域並可以選擇下載。
用 paths 指定要產出的檔案名稱
e2e-test: artifacts: paths: # for e2e test, assign video and screenshot path # https://docs.cypress.io/guides/continuous-integration/gitlab-ci - cypress/videos/**/*.mp4 - cypress/screenshots/**/*.png用 name 指定下載下來的檔案名稱(default 為 artifacts.zip)
e2e-test: artifacts: # 取該分支的名稱作為檔名 name: '$CI_COMMIT_REF_NAME'用 expire_in 指定 artifacts 的保存期限(default 為 30 天)
e2e-test: artifacts: expire_in: 90 minutes expire_in: 1 day expire_in: never用 when 指定何時需要上傳檔案(default 為 on_success)
e2e-test: artifacts: when: on_success # upload artifacts only when the job succeeds when: on_failure # upload artifacts only when the job fails when: always # always upload artifacts如果在測試時有包含 coverage,則可以用 regex 設定 coverage 選項,讓 job log 裡出現 coverage 的欄位。當測試通過時,coverage 會同時顯示在 MR 中和 job 的列表中。
test: # for jest (NodeJS) coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'cache 的作用比較像是 dependency,諸如安裝在 GitLab runner 上的套件會被 cache 起來。
當 cache:key:files 裡設定的檔案改變時,才生成新的 key
variables: # 若沒有找到 cache:key,則會使用一開始設定好的 fallback cache key CACHE_FALLBACK_KEY: fallback-keycache: key: files: # 只要 yarn.lock 檔案沒有變動,就會一直使用同一份 cache - yarn.lockvariables: # 若沒有找到 cache:key,則會使用一開始設定好的 fallback cache key CACHE_FALLBACK_KEY: fallback-keycache: key: files: # 只要 yarn.lock 檔案沒有變動,就會一直使用同一份 cache - yarn.lockcache:policy 代表 cache 的上傳和下載行為,default 為 pull-push,代表 job 開始時會下載現存的 cache,結束時則會重新上傳新的 cache,除此之外還有 pull 和 push 兩種選項
cache:paths 代表哪些檔案需要被 cache 起來,以前端專案的例子就是 node_modules,Cypress 也可以額外設定 cache
variables: # for Cypress # ref: https://docs.cypress.io/guides/continuous-integration/gitlab-ci#Parallelization CYPRESS_CACHE_FOLDER: '$CI_PROJECT_DIR/cache/Cypress'cache: paths: - cache/Cypress - node_modules將一些 job 的 cache 作用 disable
deploy: cache: []GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.