How to add values to an existing variable

How do I add, concatenate, or append values to an existing variable?

Summary:

A simple example is that the gitlab-ci variable ‘FOO’ is defined in a template file. Later we want to append value(s) to FOO so the template does a bit more.

Details:

We have a generic gitlab-ci.yml file that defines SAST jobs that we run on all of our projects. At the top of the file we include the generic files and 90% of the time the generic file works fine.

# Include the yml file that defines all of our default SAST jobs to run
include:
    - project: 'Build/DevOps'
      file: 'Gitlab/ci_cd/all.gitlab-ci.yml'

However, sometimes we need to add additional values to one of these jobs. We’ve been trying to use variables but it’s not working.

For example the SAST template we have for static:spell_check is which defines variables that it uses such as:

static:spell_check:
  extends: .static_analysis_template

  variables:
    # System files used to define default codespell configurations
    DEFAULT_DICTIONARY: /etc/codespell/codespell.dict
    CSPELLIGNORE: .cspellignore
    # All types of excluded items
    EXCLUDED_DIRS: ".git/*,INSTALL/*,*/data/*"
    EXCLUDED_FILES: "*.crt,*.pem,*.req,*.key,*.pfx,*.html,*.dcs,*.omp"
    EXCLUDED_SYSTEM_FILES: ".gitignore,${CSPELLIGNORE}"

  script:
    # Install codespell
    - pip install codespell

    # If CSPELLIGNORE exists, then add flag to remove those words from consideration
    - CSPELLIGNORE_FLAG="--ignore-words ${CSPELLIGNORE}"
    - '[ -f ${CSPELLIGNORE} ] || CSPELLIGNORE_FLAG=""'

    - codespell --enable-colors
                --summary
                --context 2
                --skip "${EXCLUDED_DIRS},${EXCLUDED_FILES},${EXCLUDED_SYSTEM_FILES}"
                --dictionary "${DICTIONARY:--}"
                --dictionary "${DEFAULT_DICTIONARY}"
                ${CSPELLIGNORE_FLAG}

The variables of concern are EXCLUDED_DIRS and EXCLUDED_FILES.

Sometimes we need additional directories excluded. We have been trying to do this in the project’s .gitlab-ci.yml file by adding logic to add values to EXCLUDED_DIRS such as the following:

static:spell_check:
    variables:
        EXCLUDED_DIRS: "$EXCLUDED_DIRS,*/submodules"

However, this does not work. I’ve tried ${EXCLUDED_DIRS} and $EXCLUDED_DIRS but it doesn’t work.

It is correctly excluding the submodules folder, but it no longer excludes the original default values.

The syntax above is how it would be done in say CMake or bash or even bat files.

Hi there,

Are you on GitLab SASS? There are a number of issues up about expanding variables in CI configs. This one I think should have resolved your issue, but there are quite a few others…

Hello,

We are not using GitLab SASS. We have a self hosted instance running at 14.9.3-ee.

However, I see from the issue that you shared that we should be able to enable a feature flag in order to activate this.

Thank you for the tip!

1 Like