【问题标题】:Include .gitlab-ci.yml multiple times with different configuration each time每次使用不同的配置多次包含 .gitlab-ci.yml
【发布时间】:2023-01-23 14:44:31
【问题描述】:

我们有一个构建软件包的 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 语法。

我们如何解决这个问题?

【问题讨论】:

  • 请注意,如果这确实有效,您将获得三个全部名为 build-package 的作业:这些作业将相互覆盖,而不是按预期执行。

标签: gitlab-ci


【解决方案1】:

也许你可以使用 extends ?

    # 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

【讨论】:

    【解决方案2】:

    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"
    

    【讨论】:

      【解决方案3】:

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

      您可以使用 .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
      

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

      【讨论】:

      • 我希望 include'ed YAML 文件中的“代码”针对每个操作系统版本运行一次。您的示例不会那样做。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      相关资源
      最近更新 更多