[CI] Templated jobs lose variables exported in global before_script
Summary
While non-templated jobs can access to such variables in both script
and after_script
contexts, templated one have it set only in script
Steps to reproduce
Reproduce playbook:
variables:
VAR_GLOBAL: '@var_global@'
before_script:
- export VAR_EXPORTED_IN_GLOBAL_before_script="@var_exported_in_global_before_script@"
- echo -e "[VARS DEBUG global before_script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
.run-test-template: &test_run_job_definition
script:
- export VAR_EXPORTED_IN_JOB_TEMPLATE_script="@var_exported_in_job_template_script@"
- echo -e "[VARS DEBUG job template script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
after_script:
- export VAR_EXPORTED_IN_JOB_TEMPLATE_after_script="@var_exported_in_job_template_after_script@"
- echo -e "[VARS DEBUG job template after_script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
job-from-template:
<<: *test_run_job_definition
variables:
VAR_DEFINED_IN_CONCRETE_JOB: '@var_defined_in_concrete_job@'
script:
- export VAR_EXPORTED_IN_JOB_ITSELF_script="@var_exported_in_job_itself_script@"
- echo -e "[VARS DEBUG templated-job itself script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
after_script:
- export VAR_EXPORTED_IN_JOB_ITSELF_after_script="@var_exported_in_job_itself_after_script@"
- echo -e "[VARS DEBUG templated-job itself after_script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
job-without-template:
script:
- export VAR_EXPORTED_IN_JOB_ITSELF_script="@var_exported_in_job_itself_script@"
- echo -e "[VARS DEBUG non-templated-job itself script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
What is the current bug behavior?
Main variable of interest there VAR_EXPORTED_IN_GLOBAL_before_script
(in example little bit more).
job-from-template
log
Running with gitlab-ci-multi-runner 9.1.0 (0118d89)
on builder.taskdata.work (c9ab8ad0)
Using Docker executor with image hubbitus/docker-egais-gitlab-builder-experiments ...
Using docker image sha256:690dac28f6fa0bc75417f96aa8056865816e1310e1c79f86a2ae71402820071c for predefined container...
Pulling docker image hubbitus/docker-egais-gitlab-builder-experiments ...
Using docker image hubbitus/docker-egais-gitlab-builder-experiments ID=sha256:b640aa388aca97cb98ac09a06fdb4fc5f04c8ba07b5d655ca921e3e18b03d62a for build container...
Running on runner-c9ab8ad0-project-63-concurrent-0 via 5eba21be1a5c...
Fetching changes...
HEAD is now at e8b783f Attempt 70: VARs debug: formatting
From http://git.taskdata.com/tests/ci-investigate
e8b783f..5de136b master -> origin/master
Checking out 5de136b1 as master...
Skipping Git submodules setup
$ export VAR_EXPORTED_IN_GLOBAL_before_script="@var_exported_in_global_before_script@"
$ echo -e "[VARS DEBUG global before_script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
[VARS DEBUG global before_script]
{VAR_GLOBAL=@var_global@}
{VAR_EXPORTED_IN_GLOBAL_before_script=@var_exported_in_global_before_script@}
{VAR_JOB_TEMPLATE=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_script=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=}
{VAR_DEFINED_IN_CONCRETE_JOB=@var_defined_in_concrete_job@}
{CI_BUILD_ID=20150}
{VAR_EXPORTED_IN_JOB_ITSELF_after_script=}
{VAR_EXPORTED_IN_JOB_ITSELF_script=}
$ export VAR_EXPORTED_IN_JOB_ITSELF_script="@var_exported_in_job_itself_script@"
$ echo -e "[VARS DEBUG templated-job itself script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
[VARS DEBUG templated-job itself script]
{VAR_GLOBAL=@var_global@}
{VAR_EXPORTED_IN_GLOBAL_before_script=@var_exported_in_global_before_script@}
{VAR_JOB_TEMPLATE=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_script=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=}
{VAR_DEFINED_IN_CONCRETE_JOB=@var_defined_in_concrete_job@}
{CI_BUILD_ID=20150}
{VAR_EXPORTED_IN_JOB_ITSELF_after_script=}
{VAR_EXPORTED_IN_JOB_ITSELF_script=@var_exported_in_job_itself_script@}
Running after script...
$ export VAR_EXPORTED_IN_JOB_ITSELF_after_script="@var_exported_in_job_itself_after_script@"
$ echo -e "[VARS DEBUG templated-job itself after_script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
[VARS DEBUG templated-job itself after_script]
{VAR_GLOBAL=@var_global@}
{VAR_EXPORTED_IN_GLOBAL_before_script=}
{VAR_JOB_TEMPLATE=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_script=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=}
{VAR_DEFINED_IN_CONCRETE_JOB=@var_defined_in_concrete_job@}
{CI_BUILD_ID=20150}
{VAR_EXPORTED_IN_JOB_ITSELF_after_script=@var_exported_in_job_itself_after_script@}
{VAR_EXPORTED_IN_JOB_ITSELF_script=}
Job succeeded
job-without-template
log:
Running with gitlab-ci-multi-runner 9.1.0 (0118d89)
on builder.taskdata.work (c9ab8ad0)
Using Docker executor with image hubbitus/docker-egais-gitlab-builder-experiments ...
Using docker image sha256:690dac28f6fa0bc75417f96aa8056865816e1310e1c79f86a2ae71402820071c for predefined container...
Pulling docker image hubbitus/docker-egais-gitlab-builder-experiments ...
Using docker image hubbitus/docker-egais-gitlab-builder-experiments ID=sha256:b640aa388aca97cb98ac09a06fdb4fc5f04c8ba07b5d655ca921e3e18b03d62a for build container...
Running on runner-c9ab8ad0-project-63-concurrent-1 via 5eba21be1a5c...
Fetching changes...
HEAD is now at e8b783f Attempt 70: VARs debug: formatting
From http://git.taskdata.com/tests/ci-investigate
e8b783f..5de136b master -> origin/master
Checking out 5de136b1 as master...
Skipping Git submodules setup
$ export VAR_EXPORTED_IN_GLOBAL_before_script="@var_exported_in_global_before_script@"
$ echo -e "[VARS DEBUG global before_script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
[VARS DEBUG global before_script]
{VAR_GLOBAL=@var_global@}
{VAR_EXPORTED_IN_GLOBAL_before_script=@var_exported_in_global_before_script@}
{VAR_JOB_TEMPLATE=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_script=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=}
{VAR_DEFINED_IN_CONCRETE_JOB=}
{CI_BUILD_ID=20151}
{VAR_EXPORTED_IN_JOB_ITSELF_after_script=}
{VAR_EXPORTED_IN_JOB_ITSELF_script=}
$ export VAR_EXPORTED_IN_JOB_ITSELF_script="@var_exported_in_job_itself_script@"
$ echo -e "[VARS DEBUG non-templated-job itself script]\n{VAR_GLOBAL=$VAR_GLOBAL}\n{VAR_EXPORTED_IN_GLOBAL_before_script=$VAR_EXPORTED_IN_GLOBAL_before_script}\n{VAR_JOB_TEMPLATE=$VAR_JOB_TEMPLATE}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_script}\n{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=$VAR_EXPORTED_IN_JOB_TEMPLATE_after_script}\n{VAR_DEFINED_IN_CONCRETE_JOB=$VAR_DEFINED_IN_CONCRETE_JOB}\n{CI_BUILD_ID=$CI_BUILD_ID}\n{VAR_EXPORTED_IN_JOB_ITSELF_after_script=$VAR_EXPORTED_IN_JOB_ITSELF_after_script}\n{VAR_EXPORTED_IN_JOB_ITSELF_script=$VAR_EXPORTED_IN_JOB_ITSELF_script}"
[VARS DEBUG non-templated-job itself script]
{VAR_GLOBAL=@var_global@}
{VAR_EXPORTED_IN_GLOBAL_before_script=@var_exported_in_global_before_script@}
{VAR_JOB_TEMPLATE=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_script=}
{VAR_EXPORTED_IN_JOB_TEMPLATE_after_script=}
{VAR_DEFINED_IN_CONCRETE_JOB=}
{CI_BUILD_ID=20151}
{VAR_EXPORTED_IN_JOB_ITSELF_after_script=}
{VAR_EXPORTED_IN_JOB_ITSELF_script=@var_exported_in_job_itself_script@}
Job succeeded
Please notice {VAR_EXPORTED_IN_GLOBAL_before_script=}
without value in first case.
What is the expected correct behavior?
VAR_EXPORTED_IN_GLOBAL_before_script
value should be present in both task executions.
Version
Gitlab CE 9.2.0 from official docker images.
Edited by Jackie Porter