B. Smith
B. Smith

Reputation: 1193

How do you reuse a before_script from a shared yml file in Gitlab CI?

I know that you can reuse blocks of code in a before script using yaml anchors:

.something_before: &something_before
- echo 'something before'   


before_script:
  - *something_before
  - echo "Another script step"

but this doesn't seem to work when the .something_before is declared in a shared .yml file via the include:file. It also does not seem that extends works for before_script. Does anyone know a way of reusing some steps in a before_script from a shared .yml file?

EDIT: My use case is that I have 2 gitlab projects with almost identical before_script steps. I don't want to have to change both projects whenever there's a change, so I have a third, separate Gitlab project that has a .yml template that I am including via include:file in both projects. I want to put all the common code in that shared template, and just have like two lines before_script for the git project that has the two extra steps.

Upvotes: 24

Views: 32878

Answers (4)

seyed
seyed

Reputation: 2055

You can use the !reference tag.

.something:
  before_script:
    - echo 'something before'   


before_script:
  - !reference [".something", "before_script"]
  - echo "Another script step"

Upvotes: 29

VonC
VonC

Reputation: 1323403

See if GitLab 13.6 (November 2020) does make it easier:

Include multiple CI/CD configuration files as a list

Previously, when adding multiple files to your CI/CD configuration using the include:file syntax, you had to specify the project and ref for each file. In this release, you now have the ability to specify the project, ref, and provide a list of files all at once. This prevents you from having to repeat yourself and makes your pipeline configuration less verbose.

https://about.gitlab.com/images/13_6/list.png -- Include multiple CI/CD configuration files as a list

See Documentation) and Issue.


And even, with GitLab 14.9 (March 2022):

Include the same CI/CD template multiple times

Previously, trying to have standard CI/CD templates that you reuse in many places was complicated because each template could only be included in a pipeline once.

We dropped this limitation in this release, so you can include the same configuration file as many times as you like.
This makes your CI/CD configuration more flexible as you can define identical includes in multiple nested configurations, and rest assured that there will be no conflicts or duplication.

See Documentation and Issue.

Upvotes: 3

danielnelz
danielnelz

Reputation: 5136

Yaml anchors don't work with included files. You need to use the extends keyword. But what you want to achieve won't work with before_script as code in your template will be overwritten in the job which uses the template if there is a before_script as well.

Do you really need a before_script in your specific job or can you achieve the same with a normal script? If yes you can do something like this:

Template File:

.something_before:
  before_script:
    - echo 'something before'
    - echo 'something more before' 

Project Pipeline:

include:
  - project: 'my-group/my-project'
    file: '/something_before.yml'

stages:
  - something

something:
  stage: something
  extends: .something_before
  script:
    - echo 'additional stuff to do'

And your before_script section will be merged into the something job and executed before the script part.

Upvotes: 17

Sergio Tanaka
Sergio Tanaka

Reputation: 1435

You can use extends without any problem, but you will need to overwrite the entire before_script block.

If you want to change just a piece of your before_script, use a shell script to do it

Set the if condition inside of your template

before_script
  - | 
    if [ condition ]
    then
      commands here
    fi

AFTER EDIT: You can use variables to achieve it

Project 1: VAR = command 1

Project 2: VAR = command 2

You can set the content of env var on the gitlab-ci.yml file or on the CI/CD settings in each project!

Upvotes: 0

Related Questions