I have an external templates YAML file in my GitLab server:
# my-own-gitlab.com/templates/-/blob/main/generic.yml
...
.run_on_main_branch_tpl: &run_on_main_branch
rules:
- if: ($CI_COMMIT_REF_NAME == "main")
when: on_success
.deploy
image: nginx
...
I want to use this rule in the CI/CD file of another project that will be also pushed to my GitLab server:
include:
- project: 'templates'
ref: 'main'
file: 'generic.yml'
deploy_dev:
environment:
name: dev
<<: *run_on_main_branch
extends: .deploy
Upon pushing the code to the branch, GitLab displays an error in the pipelines page:
Unable to create pipeline
- `.gitlab-ci.yml`: Unknown alias: run_on_main_branch
I'm not sure if what I'm trying to do possible in GitLab.
That's not possible as YAML anchors are independent for each document, so it wouldn't be possible in GitLab either.
You can do something more specific within GitLab that is using !reference
to achieve the same behaviour. Your code would look like this:
# my-own-gitlab.com/templates/-/blob/main/generic.yml
...
.run_on_main_branch_tpl:
rules:
- if: ($CI_COMMIT_REF_NAME == "main")
when: on_success
.deploy
image: nginx
...
Importing file:
include:
- project: 'templates'
ref: 'main'
file: 'generic.yml'
deploy_dev:
environment:
name: dev
rules: !reference [.run_on_main_branch_tpl, rules]
extends: .deploy
You can also take a look at the documentation about reference keyword here