Expose the environment name and slug as OpenTofu variables
What
Forwards the GitLab environment name and slug of the job as the TF_VAR_ci_environment_name and TF_VAR_ci_environment_slug OpenTofu variables, following the existing convention for auto-forwarded predefined CI/CD variables.
Why
Namespacing resources per environment is one of the most common things an OpenTofu configuration needs to do, and the environment name was the one obvious piece of job context the component did not hand over. Users currently have to plumb it through themselves.
ci_environment_slug is the one to prefer for resource naming: it contains only lowercase letters, digits and dashes, starts with a letter and is at most 24 characters, whereas the name may contain slashes (e.g. review/my-branch).
Note on the empty-value guard
Unlike the other forwarded variables, CI_ENVIRONMENT_NAME/CI_ENVIRONMENT_SLUG are only defined for jobs that declare an environment:. Exporting them unconditionally would set them to an empty string in the fmt, validate, test, delete-state and custom-command jobs — and OpenTofu treats an empty TF_VAR_ as an explicit empty value, silently overriding the default of a declared variable. This was verified empirically against OpenTofu 1.12.1, so they are only exported when they actually have a value.
The guard is on the resolved value, so a user-supplied TF_VAR_ci_environment_name still wins in a job without an environment.
Testing
- Three unit tests in
tests/unit/gitlab-tofu.bats(forwarded / override respected / stays unset). - All four paths additionally verified by hand in source mode.
shellcheckclean,make docsidempotent.