Feature Request: $CI_JOB_NAME_SLUG

Description

I like to save environment variables in files named for the job that generates them:

thisjob:
  stage: mystage
  image: ubuntu
  script:
    - tee "${CI_JOB_NAME}".env </etc/os-release
  artifacts:
    when: always
    reports:
      dotenv: $CI_JOB_NAME.env

I also like to parameterize the image with a parallel:matrix:

thatjob:
  stage: mystage
  image: $IMAGE
  parallel:
    matrix:
      - IMAGE:
          - alpine
          - debian
          - ubuntu
  script:
    - tee "${CI_JOB_NAME}".env </etc/os-release
  artifacts:
    when: always
    reports:
      dotenv: $CI_JOB_NAME.env

This doesn't work very well:

...
Uploading artifacts...
WARNING: thatjob: [alpine].env: no matching files 
ERROR: No files to upload         
...

I'm guessing the reason for this failure is that artifacts:reports:dotenv has some inherent limitation preventing it from using whitespace-containing paths. I'm unable to find any documentation or extant tickets that specifically mention this limitation.

Here's a ticket that talks about general lack of documentation for artifacts:reports:dotenv : gitlab#246777

Proposal

To https://docs.gitlab.com/ee/ci/variables/predefined_variables.html , add a new predefined variable, CI_JOB_NAME_SLUG, that expands to CI_JOB_NAME with the following modifications:

  • each contiguous run of non-alphanumeric ASCII characters is replaced by -
  • truncate to 63 characters,
  • remove any leading -
  • remove any trailing -
  • convert all characters to lowercase

The resulting string would be safe to use in URLs, host names, and domain names.

Here's how I would implement it in shell:

CI_JOB_NAME_SLUG="$(
    echo "${CI_JOB_NAME}" \
        | sed -E \
        -e 's/[^[:alnum:]]+/-/g' \
        -e 's/^(.{0,63}).*/\1/' \
        -e 's/^-//' \
        -e 's/-$//' \
        -e 's/.*/\L&/'
)"
Edited by Neil Roza