1

I'm fairly new to using gitlab-ci and as such, I've run into a problem where the following fails ci-lint because of my use of anchors/references:


image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_HOST: tcp://localhost:2375

.install_thing1: &install_thing1
  - do things
  - to install
  - thing1

.install_thing2: &install_thing2
  - do things to
  - install thing2

.setup_thing1: &setup_things1
  variables:
    VAR: var
    FOO: bar
  script:
    - all
    - the
    - things

before_script:
...

stages:
  - deploy-test
  - deploy-stage
  - deploy-prod

test:
  stage: deploy-test
  variables:
    RUN_ENV: "test"
...
  only:
    - tags
    - branches
  script:
    - *install_thing1
    - *install_thing2
    - *setup_thing1
    - other stuff
...

test:
  stage: deploy-stage
  variables:
    RUN_ENV: "stage"
...
  only:
    - master
  script:
    - *install_thing1
    - *install_thing2
    - *setup_thing1
    - other stuff

When I attempt to lint the gitlab-ci.yml, I get the following error:

Status: syntax is incorrect 
Error: jobs:test:script config should be a string or an array of strings

The error eludes to just needing an array for the script piece, which I believe I have. Use of the <<: *anchor pragma causes an error as well.

So, how can one accomplish what I'm trying to do here where I don't have to repeat the code in every -block?

Jim
  • 1,499
  • 1
  • 24
  • 43

1 Answers1

0

You can fix it and even make it more DRY, take a look at the Auto DevOps template Gitlab created.

It can fix your issue and even more improve your CI file, just have a template job like their auto_devops job, include it in a before_script and then you can combine and call multiple functions in a script block.

The anchors only give you limited flexibility.

(This concept made it possible for me to have one CI file for 20+ projects and a centralized functions file I wget and load in my before_script.)

Stefan van Gastel
  • 4,330
  • 23
  • 25