3

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
Abid Khan
  • 145
  • 2
  • 9
  • You're setting a `.pre` stage on your job, but you also need to define that stage in your `stages` list before any other stages – BrokenBinary Sep 18 '22 at 17:21
  • I have. But it made me do add a lot of stuff. I was thinking if we could somehow extends the ".pre" stage of GitLab. Is that even possible/ – Abid Khan Sep 18 '22 at 17:37
  • `.pre` is an implied stage. You can use it for any job and it does not need to be defined explicitly in `stages:`. Also if pipeline rules reult in jobs _only_ in the `.pre` or `.post` stages, the pipeline will not run by defualt. See the [stages documention](https://docs.gitlab.com/ee/ci/yaml/#stages) for reference. – sytech Sep 18 '22 at 20:26

1 Answers1

0

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

Lalaluka
  • 910
  • 1
  • 11
  • 16
  • But as you can see, when you reference it, you basically just adding script to the `example` stage. I am not sure the artifacts would work, as I have tried it. And for it to be available to other jobs, it first needs to be executed, right!!!. – Abid Khan Sep 19 '22 at 04:45