多次包含 .gitlab-ci.yml,每次使用不同的配置

问题描述 投票:0回答:4

我们有一个用于构建软件包的 GitLab CI/CD

.gitlab-ci.yaml
文件。这个
.gitlab-ci.yaml
有一个变量,用于确定应该为哪个操作系统版本构建软件包。我们希望在其他 GitLab 项目中使用
include
关键字来包含此
.gitlab-ci.yaml
以便我们能够构建包。我们希望为多个操作系统版本构建此软件包。但是,我们不能,因为 GitLab 不允许我们对同一个文件进行
include
两次。还有其他方法可以做到这一点吗?

为了更具体地看到这一点,假设我们要包含在其他项目中的

.gitlab-ci.yaml
文件是这样的:

# common/gitlab-templates/.gitlab-ci.yaml
variables:
  OS_RELEASE: 10.0

build-package:
  script: echo "building for $OS_RELEASE"

在另一个 GitLab 项目中,我们想做这样的事情:

# Build for version 8.0
include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'
    variables:
      OS_RELEASE: 8.0

# Build for version 9.0
include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'
    variables:
      OS_RELEASE: 9.0

# Build for version 10.0
include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'
    variables:
      OS_RELEASE: 10.0

但是,上面的

.gitlab-ci.yaml
语法无效。

我们如何解决这个问题?

gitlab-ci
4个回答
4
投票

也许你可以使用扩展?

    # common/gitlab-templates/.gitlab-ci.yaml
    variables:
      OS_RELEASE: change_me

    .build-package:
      script: echo "building for $OS_RELEASE"

在另一个 GitLab 项目中,我们想做这样的事情:

    # Build for version 8.0
    include:
      - project: 'common/gitlab-templates'
        file:    '.gitlab-ci.yml'

    build-package:
      extends: .build-package
      variables:
        OS_RELEASE: 8.0

    # Build for version 9.0
    include:
      - project: 'common/gitlab-templates'
        file:    '.gitlab-ci.yml'

    build-package:
      extends: .build-package
      variables:
        OS_RELEASE: 9.0

    # Build for version 10.0
    include:
      - project: 'common/gitlab-templates'
        file:    '.gitlab-ci.yml'

    build-package:
      extends: .build-package
      variables:
        OS_RELEASE: 10.0

4
投票

Robert-Jan 的答案很接近,但这将适用于您的主要

.gitlab-ci.yml
(经过测试和验证)并执行您正在尝试的操作:

include:
  - project: 'common/gitlab-templates'
    file:    '.gitlab-ci.yml'

# Build for version 8.0
build-package1:
  extends: .build-package
  variables:
    OS_RELEASE: "8.0"

# Build for version 9.0    
build-package2:
  extends: .build-package
  variables:
    OS_RELEASE: "9.0"

# Build for version 10.0
build-package3:
  extends: .build-package
  variables:
    OS_RELEASE: "10.0"

2
投票

我假设您想根据环境使用不同的变量。

您可以使用

.gitlab-ci-vars.yml
文件并定义多个 vars 部分,并根据您的环境加载不同的 vars 部分。考虑以下
.gitlab-ci-vars.yml

variables:
  FORCE_COLOR: 1

.vars-dev:
  variables:
    OS_RELEASE: 9.0
    
.vars-prod:
  variables:
    OS_RELEASE: 10.0
    

现在在

.gitlab-ci.yml
中使用它:

include:
  - ".gitlab-ci-vars.yml"

...

publish:dev:
  variables: !reference [.vars-dev, variables]
  resource_group: dev
  environment:
    name: dev
  only:
    - develop

publish:prod:
  variables: !reference [.vars-prod, variables]
  resource_group: prod
  environment:
    name: prod
  only:
    - master

这会将全局范围的环境变量与目标范围的环境合并。


0
投票

您可以使用不同的输入多次包含同一文件。但是,如果将多个同名作业添加到一个管道中,则每个附加作业都会覆盖前一个同名作业。您必须确保配置防止重复的作业名称。

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

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

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

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

检查定义使用 include 添加的配置的输入

© www.soinside.com 2019 - 2024. All rights reserved.