Help us learn about your current experience with the documentation. Take the survey.

CI/CD 输入示例

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

CI/CD 输入 可以增加您 CI/CD 配置的灵活性。 使用这些示例作为指南,来配置您的 pipeline 以使用 inputs。

多次包含同一个文件

您可以使用不同的 inputs 多次包含同一个文件。但是,如果多个同名 job 被添加到同一个 pipeline 中,每个额外的 job 会覆盖之前同名 job。您必须确保配置能够防止重复的 job 名称。

例如,使用不同的 inputs 多次包含相同的配置:

include:
  - local: path/to/my-super-linter.yml
    inputs:
      linter: docs
      lint-path: "doc/"
  - local: path/to/my-super-linter.yml
    inputs:
      linter: yaml
      lint-path: "data/yaml/"

path/to/my-super-linter.yml 中的配置确保每次包含时 job 都有唯一的名称:

spec:
  inputs:
    linter:
    lint-path:
---
"run-$[[ inputs.linter ]]-lint":
  script: ./lint --$[[ inputs.linter ]] --path=$[[ inputs.lint-path ]]

inputs 中重用配置

要使用 inputs 重用配置,您可以使用 YAML 锚点

例如,为多个支持 rules 数组的组件重用相同的 rules 配置:

.my-job-rules: &my-job-rules
  - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

include:
  - component: $CI_SERVER_FQDN/project/path/component1@main
    inputs:
      job-rules: *my-job-rules
  - component: $CI_SERVER_FQDN/project/path/component2@main
    inputs:
      job-rules: *my-job-rules

您不能在 inputs 中使用 !reference 标签,但 issue 424481 提议添加此功能。

inputsneeds 结合使用

您可以将数组类型的 inputs 与 needs 结合使用,以实现复杂的 job 依赖关系。

例如,在一个名为 component.yml 的文件中:

spec:
  inputs:
    first_needs:
      type: array
    second_needs:
      type: array
---

test_job:
  script: echo "this job has needs"
  needs:
    - $[[ inputs.first_needs ]]
    - $[[ inputs.second_needs ]]

在此示例中,inputs 是 first_needssecond_needs,都是 数组类型输入。然后,在 .gitlab-ci.yml 文件中,您可以添加此配置并设置 input 值:

include:
  - local: 'component.yml'
    inputs:
      first_needs:
        - build1
      second_needs:
        - build2

当 pipeline 启动时,test_jobneeds 数组中的项目会被连接为:

test_job:
  script: echo "this job has needs"
  needs:
  - build1
  - build2

允许在包含时扩展 needs

您可以在包含的 job 中使用 needs,同时使用 spec:inputsneeds 数组添加额外的 job。

例如:

spec:
  inputs:
    test_job_needs:
      type: array
      default: []
---

build-job:
  script:
    - echo "My build job"

test-job:
  script:
    - echo "My test job"
  needs:
    - build-job
    - $[[ inputs.test_job_needs ]]

在此示例中:

  • test-job job 总是需要 build-job
  • 默认情况下,test job 不需要其他 job,因为 test_job_needs: 数组输入默认为空。

要在配置中设置 test-job 需要另一个 job,在包含文件时将其添加到 test_needs input 中。例如:

include:
  - component: $CI_SERVER_FQDN/project/path/[email protected]
    inputs:
      test_job_needs: [my-other-job]

my-other-job:
  script:
    - echo "I want build-job` in the component to need this job too"

为没有 needs 的包含 job 添加 needs

您可以为没有 needs 的包含 job 添加 needs。例如,在 CI/CD 组件的配置中:

spec:
  inputs:
    test_job:
      default: test-job
---

build-job:
  script:
    - echo "My build job"

"$[[ inputs.test_job ]]":
  script:
    - echo "My test job"

在此示例中,spec:inputs 部分允许自定义 job 名称。

然后,在包含组件后,您可以使用额外的 needs 配置来扩展该 job。例如:

include:
  - component: $CI_SERVER_FQDN/project/path/[email protected]
    inputs:
      test_job: my-test-job

my-test-job:
  needs: [my-other-job]

my-other-job:
  script:
    - echo "I want `my-test-job` to need this job"

inputsinclude 结合使用以实现更动态的 pipeline

您可以将 inputsinclude 结合使用,以选择要包含的额外 pipeline 配置文件。

例如:

spec:
  inputs:
    pipeline-type:
      type: string
      default: development
      options: ['development', 'canary', 'production']
      description: "The pipeline type, which determines which set of jobs to include."
---

include:
  - local: .gitlab/ci/$[[ inputs.pipeline-type ]].gitlab-ci.yml

在此示例中,默认包含 .gitlab/ci/development.gitlab-ci.yml 文件。但如果使用了不同的 pipeline-type input 选项,则会包含不同的配置文件。

在变量表达式中使用 CI/CD inputs

您可以使用 CI/CD inputs 来自定义变量表达式。例如:

example-job:
  script: echo "Testing"
  rules:
    - if: '"$[[ inputs.some_example ]]" == "test-branch"'

表达式分两步进行求值:

  1. 输入插值:在创建 pipeline 之前,inputs 会被替换为输入值。在此示例中,$[[ inputs.some_example ]] input 会被替换为 设置的值。例如,如果值是:

    • test-branch,表达式变为 if: '"test-branch" == "test-branch"'
    • $CI_COMMIT_BRANCH,表达式变为 if: '"$CI_COMMIT_BRANCH" == "test-branch"'
  2. 表达式求值:在 inputs 插值后,GitLab 尝试创建 pipeline。在创建 pipeline 期间,会求值表达式以确定要将哪些 job 添加到 pipeline 中。