Run a pre job before GitLab pipeline
Asked Answered
N

2

6

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?

Newmown answered 18/9, 2022 at 16:55 Comment(3)
You're setting a .pre stage on your job, but you also need to define that stage in your stages list before any other stagesEpicurus
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/Newmown
.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 for reference.Dalia
M
1

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

Meaganmeager answered 18/9, 2022 at 20:26 Comment(1)
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!!!.Newmown
O
1

I think this would work

.job_template: &job_configuration
  before_script:
    "your script here"

YourStage:
  <<: *job_configuration
Overturn answered 2/8 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.