如何向从模板继承的 before_script 添加命令

How to add commands to before_script inherited from template

假设我有一个包含如下内容的模板:

.some-scripts: &some-scripts |
    set -e

    function somefunction() {
       
    }

.template-job:
  before_script:
    - *some-scripts
    - echo "Example command"
    - somefunction

build-job:
  extends: .template-job
  stage: build
  script: 
    - mvn build

此模板包含在另一个 gitlab-ci.yml 中,我希望在不覆盖模板的 before_script 的情况下向我的构建作业的 before_script 添加一些特定命令-工作。这可能吗?如何实现?

目前您无法扩展 before_script,只能覆盖它。但是关于扩展行为有一个开放的issue

作为一种解决方法,您可以将其他命令添加到 script 部分,因为 before_scriptscriptafter_script 在执行时最终合并为一个块.

.template-job:
  before_script:
    - echo "Example command"
    - echo "Second command"

build-job:
  extends: .template-job
  stage: build
  script: 
    - echo "third command"
    - echo "fourth command"
    - mvn build

我找到了我要找的东西,我需要使用 reference tag

这是我想出的:

build-job:
  stage: build
  before_script: 
    - !reference [.template-job, before_script]
    - mycommand
    - mysecondcommand
  script: 
    - mvn build