jeanpic
jeanpic

Reputation: 541

How to add commands to before_script inherited from template

Let's say I have a template which contains something like this:

.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

This template is included in another gitlab-ci.yml and I am looking to add some specific commands to the before_script of my build-job without overriding the before_script of the template-job. Is it possible and how?

Upvotes: 2

Views: 2345

Answers (2)

jeanpic
jeanpic

Reputation: 541

I found what I was looking for, I needed to use a reference tag.

Here's what I came up with:

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

Upvotes: 5

danielnelz
danielnelz

Reputation: 5184

Currently you cannot extend a before_script, just overwrite it. But there is an open issue regarding the extending behavior.

As a workaround you could just add the additional commands to your script section as before_script, script and after_script are ultimatley merged together to one block on execution.

.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

Upvotes: 0

Related Questions