How to merge or add a new stage in gitlab-ci.yml which includes a common template yml with default list of stages and job definitions

Viewed 5983

Currently in our project the gitlab-ci.yml includes a common template yml with default stages and job definitions. I want to include a stage and respective job specific to this project, which needs to run in between the stages defined in template yml. Below are the sample yml files which represent the scenario I am working on.

  1. .mvn-template.yml
stages:
  - build
  - static-analysis
  - unit-test
  - package
  - integration-test

compile:
  stage: build
  tags:
    - docker
  artifacts:
    paths:
      - target/
    expire_in: 14 days
  script:
    - mvn $MAVEN_CLI_OPTS compile
  interruptible: true
  
//and job definitions for - static-analysis
//  - unit-test
//  - package
//  - integration-test
  1. gitlab-ci.yml
include:
  - project: 'xxx/common-pipeline'
    ref: x.x.x
    file: '/.mvn-template.yml'
...

Now I want include a new stage specific this project, say contract-testing, in between

  • package
  • integration-test

So the contract-testing should run after package and before integration-test.

As this is specific to this project I cannot include in common template. Thus needs to be included in gitlab-ci.yml.

Could not figure out how to do this. I earlier worked with GitHub and just recently started on project with GitLab.

Can I include the stage in common template but don't define respective job there. And include the Job definition in gitlab-ci.yml? Not sure if this is the correct way. Probably there could be better options to handle such scenario.

Please help.

1 Answers

In my opinion, you can do it in 2 different ways :

  1. Add the stage contract-testing in your template file and define the specific job in the .gitlab-ci.yml of your project. The configuration will be valid : gitlab will concatenate and expand includes/anchors to build one single gitlab-ci at the end. Note : you can define a stage even if there is no job using this stage (a sort of an empty stage), it will not generate an error.

  2. If you don't want to have this "empty stage" included in all your projects referring the include, you can move the stages outside of you template and put them in the .gitlab-ci.yml of your project. Like for solution 1), you need to define the job in .gitlab-ci.yml of your project (not in the template).

Related