Run a pre job before GitLab pipeline

5.3k views Asked by At

I want to run a job each time a new pipeline gets triggered. It's a kind of preparation job which should always be executed before every other job defined inside the .gitlab-ci.yml

For Example

stages:
  - build
  - test

my-prep-job:
  stage: .pre
  script:
    # this is the job I want to run every time a pipeline gets triggered before other jobs
    # also it will have an artifact that I want to use in rest of the job
  ...
  artifacts:
    ...

Build:
  stage: build
  ...

Test:
  stage: test
  ....

Please, let me know if this is possible or if there is other way around.

Thanks in Advance...

Edit

I did try adding .pre under stages. Thing is I had to rewrite the rules and add it to my-prep-job stages as well.

stages:
  - .pre # I did add it over here
  - build
  - test

Also I had to add the rules to this stage as well so that it would not run on it's own on just a normal commit/push.

Is there any possibility to extend ".pre" stage of GitLab pipeline?

1

There are 1 answers

1
Lalaluka On

You could use !reference tags to include certain keyword sections. For example:

.pre
  script:
    - echo from pre

example:
  stage: test
  script:
    - !reference [.pre, script]
    - ...

Will include the script part of .pre into the example job.

You can use !reference for most of the job keywords like artifacts or rules