Split GitLab CI Jobs into multiple files
Asked Answered
T

3

9

I've been trying to get more familiar with GitLab's CI functionality and find the idea of splitting up a CI pipeline into multiple separate jobs interesting. This would allow me to maintain one project of "known jobs" and include them in other projects.

So far, I have something like this:

$ ls
jobA.yaml jobB.yaml jobC.yaml jobD.yaml

Those 4 are all identical (for now), and have the following:

job-name:
   stage: my-stage # Might be needed to differentiate later on
   tags: runner-tag # used to figure out where/how the job should be done: directly on a server, in a container, etc
   script:
      - echo "beep beep"

In the actual .gitlab-ci.yaml I want to use, I would then (I think) put something like this. In this case, I would use the jobs defined in the project for itself:

include:
        project: '$CI_PROJECT_PATH'
        file: "*.yaml"
stages:
  - my-stage

That gives me back a linter error though. Perhaps I'm misreading the documentation, but I think that should be possible somehow....

Tierney answered 4/6, 2021 at 10:6 Comment(2)
Which gitlab version are you using? Wildcard includes were only introduced with gitlab 13.12Gabrila
I think we have 13.12.2Tierney
G
7

According to the docs, wildcard includes are only possible with local. Furthermore you need to move your jobA.yaml to a directory as otherwise you will include your .gitlab-ci.yml as well with a wildcard on the top level.

So the following works with JobA.yaml in config:

include:
  - local: 'config/*.yaml'

stages:
  - my-stage
Gabrila answered 7/6, 2021 at 5:42 Comment(0)
P
6

This should be a comment, but can't put formatted code in there..

We use a main yml, which just include all the others. It is not wildcards like you have.

Have you tried changing "file" to "local"? with the leading "- "?

include:
  - template: Code-Quality.gitlab-ci.yml
  - local: '/.gitlab/py.yml'
  - local: '/.gitlab/static.yml'
  - local: '/.gitlab/lint.yml'
  - local: '/.gitlab/docs.yml'
  - local: '/.gitlab/publish.yml'
Prevenient answered 4/6, 2021 at 13:31 Comment(0)
O
0

your yaml syntax is not correct, it is missing a "-" in front of your project.

include: 
  - project: '$CI_PROJECT_PATH'
    file: "*.yaml"
Osteen answered 19/7, 2024 at 8:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.