Skip to content

Simplify includes by flattening before_script, script and after_script arrays

Description

Wouldn't this make everyone's life with gitlab-ci.yml easy and fun?

.something: &something
- echo something
- ./dosomething

.somethingelse: &somethingelse
- echo somethingelse
- ./dosomethingelse

job1:
  script:
  - *something
  - echo job1

job2:
  script:
  - *something
  - *somethingelse
  - echo job2

At first, it sounds looks perfectly fine and reasonable. Why wouldn't it work?

But then one realizes it won't work because the array isn't flat anymore. The output object is the below - and nested arrays are problematic:

{job1: {script: [['echo something', './dosomething'], 'echo job1']}, job2: {script: [['echo something', './dosomething'], ['echo somethingelse', './dosomethingelse'], 'echo job2'}}

Proposal

Flatten the hell out of before_script, script, after_script and make life easy!

{script: [['echo something', './dosomething'], 'echo job1']} -> {script: ['echo something', './dosomething', 'echo job1']}

Limitations

This will work with anchor syntax, but not with the extends keyword.

Workaround

@ayufan proposed a workaround: https://gitlab.com/gitlab-org/gitlab-ce/issues/19677#note_13008199

.bundle_install: &bundle_install |
  echo aa
  echo bb

.bundle_exec: &bundle_exec |
  echo aa
  echo bb

test:
  script:
  - *bundle_install
  - *bundle_exec

The | syntax is a way to write a single command but appear as multi-line in YAML. The runner treats it as a single bash command. In other words, it's equivalent to running echo aa && echo bb- and this is an inferior solution since when the job fails on a very long multi-line statement it's not clear which command failed.

Links to related issues and merge requests / references

https://gitlab.com/gitlab-org/gitlab-ce/issues/19677

CC @ayufan @grzesiek @markpundsack @bikebilly @markglenfletcher

Edited by Jason Yavorsky