Add a partial yaml tag to the ci yaml to support delayed evaluation yaml alias
TL/DR
I would like a way to include items to be expanded after the merge of yml files like !expand: included_script_partial because Using YAML aliases across different YAML files sourced by include is not supported. and extends does not merge the various script blocks. Basically this is a delayed evaluation yaml alias.
Proposal should solve
Full Explanation
We have several script anchors that are C&Ped in almost every job, all of these are mostly just basic env variable setup scripts, but can also be useful common service auth or internal policy type scripts
eg
.set_semver: &set_semver
- export SEMVER=${SEMVER:-$(cat .next-version 2&>/dev/null || echo "${CI_COMMIT_REF_NAME/v/}")};
.set_team: &set_team |-
export TEAM=${TEAM:-${CI_PROJECT_NAMESPACE}};
...
job1:
script:
- *set_semver
- *set_team
- do_stuff ${TEAM} ${SEMVER}
job2:
before_script:
- *set_semver
- do_pre_validation
script:
- do_other_stuff ${SEMVER}
job2:
script:
- *set_semver
- *set_team
- do_more_stuff ${TEAM} ${SEMVER}
after_script:
- do_job_cleanup
Since I have these alias/anchors C&Ped in 120+ gitlab-ci files I would like to be able to switch them to be included, but Using YAML aliases across different YAML files sourced by include is not supported.
Proposed format would be something like
--- #env-setup-script.yml
.set_team: !partial
- export TEAM=${TEAM:-${CI_PROJECT_NAMESPACE}}
.set_semver: !partial
- export SEMVER=${SEMVER:-$(cat .next-version 2&>/dev/null || echo "${CI_COMMIT_REF_NAME/v/}")}
- ...
.env_setup: !partial
- !expand: .set_semver
- !expand: .set_team
.global_cache: !partial
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- public/
- vendor/
policy: pull-push
--- #pre merged .gitlab-ci.yml
include: env-setup-script.yml
job1:
cache: !expand: .global_cache
policy: pull
script:
- !expand: .set_semver
- do_something
- !expand: .set_team
- do_stuff ${TEAM} ${SEMVER}
job2:
script:
- !expand: .env_setup
- do_stuff ${TEAM} ${SEMVER}
--- #post merged/processed .gitlab-ci.yml
job1:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- public/
- vendor/
policy: pull
script:
- export SEMVER=${SEMVER:-$(cat .next-version 2&>/dev/null || echo "${CI_COMMIT_REF_NAME/v/}")}
- ...
- do_something
- export TEAM=${TEAM:-${CI_PROJECT_NAMESPACE}}
- do_stuff ${TEAM} ${SEMVER}
job2:
script:
- export SEMVER=${SEMVER:-$(cat .next-version 2&>/dev/null || echo "${CI_COMMIT_REF_NAME/v/}")}
- ...
- export TEAM=${TEAM:-${CI_PROJECT_NAMESPACE}}
- do_stuff ${TEAM} ${SEMVER}
The way I see this working is the !partial tag registers the hidden tag as something that can be expanded by the !expand tag. I chose the yaml tag format because it makes it explicit that this is special in some way.