How to concatenate or create a variable in gitlab-ci.yml job template script section?

Viewed 3406

I have a test script that is always the same "banana" but I need to append something else in different jobs, such as "orange", "lime", "strawberry"

.job_template:
  services:
    - docker:dind
  script: banana

job1:
  extends: .job_template
  script:
    - banana orange

job2:
  extends: .job_template
  script:
    - banana lime

job3:
  extends: .job_template
  script:
    - banana strawberry

How can I achieve this? The "banana" part is pretty long, it looks like:

- docker run castsofwaredocker/gitlab:cypressrunner_master https://${CI_COMMIT_REF_NAME}.helloworld.io --record --key "xxxx" --parallel --group "prod" --ci-build-id $CI_PIPELINE_ID "--spec cypress/integration/store/26-login/*.js"

So I'm basically only replacing this part "cypress/integration/store/26-login/*.js"

1 Answers

There is an example in GitLab CI/CD docs.

You are able to use other variables inside your variable definition (or escape them with $$):

Your .gitlab-ci.yml can be simplified in this way:

variables:
  BANANA_CMD: 'docker run castsofwaredocker/gitlab:cypressrunner_master https://${CI_COMMIT_REF_NAME}.helloworld.io --record --key "xxxx" --parallel --group "prod" --ci-build-id $CI_PIPELINE_ID'

.job_template:
  services:
    - docker:dind
  script: eval '$BANANA_CMD "--spec cypress/integration/store/26-login/*.js"'

job1:
  extends: .job_template
  script:
    - eval '$BANANA_CMD orange'

job2:
  extends: .job_template
  script:
    - eval '$BANANA_CMD lime'

job3:
  extends: .job_template
  script:
    - eval '$BANANA_CMD strawberry'
Related