Is it possible to override the global before_script
with an empty before_script
within a job?
Asked
Active
Viewed 2.8k times
58

Jonathan Hall
- 75,165
- 16
- 143
- 189

Alexander Herold
- 1,057
- 2
- 9
- 20
2 Answers
82
Yes, the local before_script
overrides the global before_script
.
To keep the .yml sintax valid, use a command that does nothing.
before_script:
- global before script
job:
before_script:
- ''

rpadovani
- 7,101
- 2
- 31
- 50
-
14How does one do the opposite? Not override the parent `before_script`? – David Callanan Jul 31 '19 at 11:26
-
@DavidCallanan you can either not declare `before_script` in your job to avoid overriding it or reference the global `before_script` to extend it in your job. https://docs.gitlab.com/ee/ci/yaml/#reference-tags – Antony Fuentes Sep 15 '21 at 17:59
-
1To create a reference to the global `before_script`, enter this line into your local `before_script` as if it were a regular command: `- !reference [before_script]`. This way you can not override it but extend it with additional commands. – gekkedev Apr 28 '22 at 09:17
-
@jakub-kania solution is much "clearer" in my opinion – Izydorr Jan 16 '23 at 11:21
74
You should use an empty array notation.
before_script:
- global before script
job:
before_script: []
script:
- test

Jakub Kania
- 15,665
- 2
- 37
- 47
-
-
@nCrazed You mean the global before_script? It really shouldn't. Please check if your pasted it correctly. – Jakub Kania Oct 23 '18 at 20:37
-
-
@nCrazed Please check the jobs in this project: https://gitlab.com/blckct/before_script , that project contains a demo and it appears to be working just like it should. – Jakub Kania Oct 25 '18 at 07:59
-
I've been testing it locally via `gitlab-runner` (11.3.1), could that explain the discrepancy in the behavior? :| – nCrazed Oct 26 '18 at 08:23
-
@nCrazed If you can confirm problem locally you should report it to Gitlab. – Jakub Kania Jan 24 '19 at 11:50
-
1Works for me, and syntacticaly I like it better than assigning an empty string as in the accepted answer (which also works) – Auspex Nov 24 '22 at 12:42