$CI_COMMIT_REF_NAME is incorrect inside include when a manual pipeline is pre-loaded

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Summary

When used in an include and inside a manual pipeline that preloads, the variable $CI_COMMIT_REF_NAME contains the whole reference to the branch or tag, e.g. refs/heads/main instead of main or refs/tags/v0.0.1 instead of v0.0.1, when it shouldn't include the refs/{heads,tags} part, as in a job script, for example.

The behaviour is not consitant either between when the pipeline preloads (for example, to show variables to an end user) and when the pipeline runs.
This reported bug only occurs on pipeline preloading.

This is even more confusing due to the fact that the Build > Pipeline Editor > Full configuration is displaying the correct behaviour.

This is a repost of #339306 (closed); which have been closed due to inactivity.

Steps to reproduce

The below pipeline should able us to push a list of environments as a GitLab variable in a package named after the branch or tag that would, then, be loaded when running the pipeline manually.
Allowing the end user to choose from a closed list of available environments.
The said list of environments can be enhanced and tested from a branch (actually enabling the possibility to have a different list of environments from branch to branch).

include:
  - remote: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/envs/${CI_COMMIT_REF_NAME}/envs.yml"
    rules: 
      - if: $CI_PIPELINE_SOURCE != "push"

deploy:
  stage: deploy
  image: alpine
  rules:
    - if: $CI_PIPELINE_SOURCE != "push"
  script:
    - echo $ENV

build:
  stage: build
  image: alpine
  rules:
    - if: $CI_PIPELINE_SOURCE == "push"
  script:
    - apk add --no-cache curl
    - |
      echo "variables:
        ENV:
          description: Selection of an environments
          value: foo
          options:
            - foo
            - bar
            - ${CI_COMMIT_REF_NAME}
      " > envs.yml 
    - cat envs.yml
    - >-
      curl --fail-with-body 
      --header "JOB-TOKEN: ${CI_JOB_TOKEN}"
      --upload-file envs.yml
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/envs/${CI_COMMIT_REF_NAME}/envs.yml"

Example Project

https://gitlab.com/begeeraerts/example-project-for-issue-497644

What is the current bug behavior?

$CI_COMMIT_REF_NAME should display a consistant value, whenever used in include of a manual pipeline that preloads or anywhere else.
For now, it is refs/heads/<branch_name> or refs/tags/<tag_name>.

In the exemple project, this actually means that the drop down variable ENV is not displayed to the end user:

Screenshot_2024-10-03_at_22.52.33

What is the expected correct behavior?

$CI_COMMIT_REF_NAME in include should only have <branch_name> or <tag_name> as value.

In the example project, this is what happens in the workaround branch, the variable is loaded from the external resource and does display properly:

Screenshot_2024-10-03_at_22.54.36

Relevant logs and/or screenshots

Output of checks

This bug happens on GitLab.com

Current workaround

As a current workaround, we could aditionnaly push to refs/{heads,tags}, but that feels like a band-aid fix and it means using the same data twice (once for the pipeline pre-load and once for the pipeline run).

    ## For branches: 
    - >-
      curl --fail-with-body 
      --header "JOB-TOKEN: ${CI_JOB_TOKEN}"
      --upload-file envs.yml
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/envs/refs/heads/${CI_COMMIT_REF_NAME}/envs.yml"
    
    ## For tags: 
    - >-
      curl --fail-with-body 
      --header "JOB-TOKEN: ${CI_JOB_TOKEN}"
      --upload-file envs.yml
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/envs/refs/tags/${CI_COMMIT_REF_NAME}/envs.yml"

Possible fixes

Edited by 🤖 GitLab Bot 🤖