###### tags: `gitlab` `CI/CD` [toc] <!-- :::danger .gitlab-ci.yml 已發生重大更新,請以分支中的 .gitlab-ci.yml 內容參考 ::: --> # Gitlab CI/CD Trigger 筆記 - 本篇中的範例以 Release 為前提而設計。與一般開發的不同的是,開發部屬的間隔可能30分鐘到數小時不等,而 Release 可能會是一周或一個 sprint,而兩者的 CI 腳本也有可能不同 ## 發動端 (source) - .gitlab-ci.yml ```=yml ci-cd:step1: stage: release image: docker variables: PARENT_PIPELINE_ID: $CI_PIPELINE_ID script: - echo hi firends - echo hi friends >> hi_firends.txt artifacts: untracked: false when: on_success expire_in: "1m" paths: - ${CI_PROJECT_DIR}/hi_firends.txt rules: - if: $CI_COMMIT_REF_NAME =~ /[Rr]elease.*/ when: manual ci-cd:step2: stage: release trigger: project: path/to/tagret/repo strategy: depend branch: $CI_COMMIT_REF_NAME variables: PARENT_PIPELINE_ID: $CI_PIPELINE_ID UPSTREAM_PROJECT_ID: $CI_PROJECT_ID UPSTREAM_JOB_ID: $CI_JOB_ID UPSTREAM_REF: $CI_COMMIT_REF_NAME needs: - ci-cd-test:step1 ``` - 說明 - 這邊設定了兩個 Job,`ci-cd:step1` 就是用來執行一般的 CI/CD 操作,`ci-cd:step2` 是一個 trigger Job,當出現 `trigger` 這個 keyword 時,[有些 keyword 就無法使用](https://docs.gitlab.com/ee/ci/yaml/?query=trigger#trigger) - project: 指定 target 來讓這個 trigger 觸發 - strategy: 當設定為 depend 時,則上游流水線會「相依」下游流水線的工作執行結果;換句話說,就是下游成功,上游才會標記為成功 [ref.](https://docs.gitlab.com/ee/ci/yaml/?query=trigger#triggerstrategy) - branch: 指定 target 分支名稱 ## 觸發端 (target) - .gitlab-ci.yml ```=yml release:step1: stage: release image: docker script: - 'curl --location --output artifacts.zip --header "JOB-TOKEN: $CI_JOB_TOKEN" "http://serveer.hotst/api/v4/projects/$UPSTREAM_PROJECT_ID/jobs/$UPSTREAM_JOB_ID/artifacts"' - unzip artifacts.zip rules: - if: $CI_COMMIT_REF_NAME =~ /[Rr]elease.*/ && $CI_PIPELINE_SOURCE == "pipeline" ``` - 說明 - `release:step1` 為一般 CI/CD Job,假如要使此 Job 在特定條件下才被 trigger 觸發,以此範例說明: - 當分支或 tag 名稱符合 `[Rr]elease.*` 正規表示式 - 且 `$CI_PIPELINE_SOURCE` 為 `pipeline` 參考 [rules examples](https://docs.gitlab.com/ee/ci/jobs/job_control.html), [CI_PIPELINE_SOURCE](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html) - `$CI_PIPELINE_SOURCE` 有 `trigger` & `pipeline` 兩種,[詳細說明](https://docs.gitlab.com/ee/ci/triggers/index.html#configure-cicd-jobs-to-run-in-triggered-pipelines) - 任何定義在上游 Job 中的變數,預設都會轉送到下游來,如下游有同名變數,會被上游的覆蓋 [ref.](https://docs.gitlab.com/ee/ci/yaml/?query=trigger#triggerforward) - ## 參考 - [Day26 - GitLab CI 啟動其它專案啟動流水線或動態產出新的流水線,談觸發 trigger](https://ithelp.ithome.com.tw/articles/10251909) - [Specify when jobs run with rules](https://docs.gitlab.com/ee/ci/jobs/job_control.html#specify-when-jobs-run-with-rules) - [predefined variables: CI_PIPELINE_SOURCE](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html) - [Configure CI/CD jobs to run in triggered pipelines](https://docs.gitlab.com/ee/ci/triggers/index.html#configure-cicd-jobs-to-run-in-triggered-pipelines) - [needs:pipeline:job](https://docs.gitlab.com/ee/ci/yaml/?query=trigger#needspipelinejob) - [Downstream pipelines](https://docs.gitlab.com/ee/ci/pipelines/downstream_pipelines.html?tab=Multi-project+pipeline) - Trigger - [Trigger a multi-project pipeline by using the API](https://docs.gitlab.com/ee/ci/pipelines/downstream_pipelines.html) - [Trigger pipelines by using the API](https://docs.gitlab.com/ee/ci/triggers/#use-curl) - [GitLab CI/CD job token](https://docs.gitlab.com/ee/ci/jobs/ci_job_token.html) - [Pipeline trigger tokens API](https://docs.gitlab.com/ee/api/pipeline_triggers.html#trigger-a-pipeline-with-a-token) - [Youtube - Multi-project pipelines](https://www.youtube.com/watch?v=g_PIwBM1J84&ab_channel=GitLabUnfiltered) - [Passing gitlab artifacts from pipeline to pipeline](https://medium.com/trendyol-tech/passing-gitlab-artifacts-to-pipeline-to-pipeline-feb9e887525a) - - curl - [Linux Curl Command 指令與基本操作入門教學](https://blog.techbridge.cc/2019/02/01/linux-curl-command-tutorial/) - Split long commands - [Format scripts and job logs](https://docs.gitlab.com/ee/ci/yaml/script.html#split-long-commands) - reuse the script of job - [extends](https://docs.gitlab.com/ee/ci/yaml/#extends) - [Optimize GitLab CI/CD configuration files](https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#use-extends-to-reuse-configuration-sections) - runner - [Configuring runners](https://docs.gitlab.com/ee/ci/runners/configure_runners.html) - [Day21 - GitLab CI 選擇指定的 runner 來承接工作,談標籤 tags](https://ithelp.ithome.com.tw/articles/10249674?sc=iThomeR) - [Advanced configuration](https://docs.gitlab.com/runner/configuration/advanced-configuration.html) - [Docker Machine Executor autoscale configuration](https://docs.gitlab.com/runner/configuration/autoscale.html#how-concurrent-limit-and-idlecount-generate-the-upper-limit-of-running-machines) - Artifacts - [Job Artifacts API](https://docs.gitlab.com/ee/api/job_artifacts.html) - [GitLab CI/CD artifacts reports types](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html) - API - self-managed - [Pipeline trigger tokens API (FREE)](http://192.168.100.202/help/api/pipeline_triggers.md) - [The scope of runners (FREE)](http://192.168.100.202/help/ci/runners/runners_scope) - [GitLab CI/CD job token (FREE)](http://192.168.100.202/help/ci/jobs/ci_job_token.md) - [`.gitlab-ci.yml` keyword reference (FREE)](http://192.168.100.202/help/ci/yaml/index) - [https://medium.com/@wasd2030980/gitlab-ci-cd-%E4%B9%8B-needs-077d5b284f06](https://medium.com/@wasd2030980/gitlab-ci-cd-%E4%B9%8B-needs-077d5b284f06) ## 採坑 - 使用 API 觸發 trigger 時,當使用的 TOKEN 為 `$CI_JOB_TOKEN`,`$CI_PIPELINE_SOURCE` 的值為 `pipeline`,如果是使用 Trigger Token,值則是 `trigger` - [`The requested URL returned error: 400` when triggering a pipeline](https://docs.gitlab.com/ee/ci/triggers/#the-requested-url-returned-error-400-when-triggering-a-pipeline) ## 附件 <!-- - ![image](https://hackmd.io/_uploads/SyiYmn496.png) --> - ![image](https://hackmd.io/_uploads/Bkm1hk4Tye.png) <!-- - ![image](https://hackmd.io/_uploads/BkGqQ2V96.png) --> - ![image](https://hackmd.io/_uploads/S1LZ3146yg.png) <!-- - ![image](https://hackmd.io/_uploads/SywoZcd9p.png) --> - ![image](https://hackmd.io/_uploads/ryxM2JEakl.png)