GitLab CI - Reuse rules with !reference tag
Asked Answered
I

1

16

According to GitLab's CI documentation about !reference tag, it says:

Use the !reference custom YAML tag to select keyword configuration from other job sections and reuse it in the current section. Unlike YAML anchors, you can use !reference tags to reuse configuration from included configuration files as well.

Can we use the !reference tag to reuse rules or any other allowed keyword?

Intussuscept answered 9/4, 2021 at 21:37 Comment(4)
There is a bug (gitlab.com/gitlab-org/gitlab/-/issues/322992) regarding the use of !reference with rules and some other keywords. It seems that when this feature was introduced, not all of the keywords were fully tested.Phip
@JakobLiskow you're absolutely right. Meanwhile made a small test, and the result was what you explained. Thanks for the quick response.Intussuscept
fyi - a hacky way to reuse rules #67009507Phip
Worth noting that GitlabCI started supporting the "!reference" tag in rules since version 14.3 (check under Version history)Conferral
N
16

See GitLab 14.3 (September 2021):

Support merging CI/CD rules arrays with !reference

The YAML !reference tag introduced earlier this year helps you reuse and combine configuration.
This is a very flexible way to combine frequently reused configuration with job-specific configuration in one or more jobs.

However, using it with the rules keyword was not supported yet.

In 14.3, we’ve added !reference support to the rules keyword, so you can now mix and match rules more easily, including with configuration from different files.
You can use the CI/CD pipeline editor to view the merged configuration.

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfhWmf0cC2nZ7-sWV9QWRft/images/14_3/reuse_rules_in_different_jobs.png -- Support merging CI/CD rules arrays with !reference

See Documentation and Issue.

Neron answered 26/9, 2021 at 22:7 Comment(6)
But you one can't reference MORE THAN ONE rule set at a time? It does work for scripts but not for the rules, why?Dunderhead
@Dunderhead That I do not know: that would be a good question to add to the original issue gitlab.com/gitlab-org/gitlab/-/issues/322992 (or in a new issue)Neron
Current link to !reference is docs.gitlab.com/ee/ci/yaml/…Privateer
@Privateer Thank you for the update. I have edited the answer accordingly.Neron
What theme is this in your screenshot?Virtue
@Virtue Not sure, considering this screenshot comes from the GitLab release notes. Looking at vscodethemes, I would say... One Dark Theme, maybe? I honestly don't know.Neron

© 2022 - 2024 — McMap. All rights reserved.