如何在gitlab-ci.yml作业模板脚本部分中串联或创建变量?

时间:2020-07-08 18:38:51

标签: gitlab-ci

我的测试脚本始终是相同的“香蕉”,但是我需要在其他作业中添加其他内容,例如“橙色”,“石灰”,“草莓”

.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

我该如何实现? “香蕉”部分相当长,看起来像:

- 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"

所以我基本上只替换这部分“ cypress / integration / store / 26-login / *。js”

1 个答案:

答案 0 :(得分:1)

GitLab CI/CD docs中有一个示例。

您可以在变量定义内使用其他变量(或使用$$对其进行转义):

您的.gitlab-ci.yml可以通过以下方式简化:

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'