Search code examples
gitlabgitlab-cipipeline

add gitlab-ci job when including an gitlab-ci template


here is my problem: I have a GitLab-ci.yml template in a separate repository with all basic stages/steps in it. In another repo with the actual code, I include that template and overwrite a variable for unit testing for example.

Here is how the base template looks like:

---
image: docker:git

variables:
  TEST_CMD: unit-test $UNIT_TEST_CMD_OPTIONS

stages:
  - pre-build
  - build
  - test

actual_build:
  stage: build
  <<: *only-default
  script:
    - echo "build it"
    - ...

....

Now I include that template into my actual repo gitlab-ci.yml file to execute the whole pipeline:

---
include:
  - project: blahfubar/gitlab-ci-template
    ref: master
    file: basic.gitlab-ci.yml

variables:
  UNIT_TEST_CMD_OPTIONS: --testsuite unit

Now the problem: I want to add an extra job just in this repo to i.e. execute a security check there, but this does not work and with the "extends:"-keyword only jobs can be extended.

---
include:
  - project: blahfubar/gitlab-ci-template
    ref: master
    file: basic.gitlab-ci.yml

variables:
  UNIT_TEST_CMD_OPTIONS: --testsuite unit

security-step:
  stage: test
  script:
    - ./security-check

If I do it like that, only the "security-step" will be executed, but not all the other steps from the included template.

May anyone has a solution for that because I didn't find anything in the gitlab-ci docs.

Thanks in advance :)


Solution

  • If I understand correctly, the !reference keyword in Gitlab may help you, but probably not fully resolve your issue: https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#reference-tags

    Your security-step could look like this:

    security-step:
      stage: test
      script:
        - !reference [actual_build, script]
        - ./security-check
    

    This will include all your build script lines before the security step.