How to use variable for yaml array? (e.g. $CI_RUNNER_TAGS as job tags)

Problem to solve

I am trying to use the value of $CI_RUNNER_TAGS as tags of another job, but I can’t figure out how to convert its string value for the job’s tags. The value of $CI_RUNNER_TAGS seems to be a string such as ["tag1", "tag2"]. When I store the value of $CI_RUNNER_TAGS in an environment variable via dotenv:, I can use its value in another job, e.g. print it, but when I try to use it for tags:, it fails with:

Error: jobs:build:tags config should be an array of strings

Do you have any idea how to achieve that?

Steps to reproduce

Taken from Allow for variable substitution in CI tag names (Dynamic Tags) (#35742) · Issues · GitLab.org / GitLab · GitLab with adaptions:

# contents of .gitlab-ci.yml

stages:
  - runner_selection
  - pipeline_creation

runner_selection:
  stage: runner_selection
  tags:
    - ANY
  script:
    # $CI_RUNNER_TAGS looks like '["tag1", "tag2"]'
    - echo "SELECTED_RUNNER=$CI_RUNNER_TAGS" >> runner_selection.env
  artifacts:
    reports:
      dotenv: runner_selection.env

pipeline_creation:
  stage: pipeline_creation
  variables:
    SELECTED_RUNNER_TAG: $SELECTED_RUNNER # e.g. '["tag1", "tag2"]'
  trigger:
    include: .gitlab-ci-child.yml


# contents of .gitlab-ci-child.yml
stages:
  - build

build:
  stage: build
  tags: $SELECTED_RUNNER_TAG # FAILS with: jobs:build:tags config should be an array of strings
  script:
    - echo "build"

Another attempt using spec:inputs with type array:

# contents of .gitlab-ci.yml

stages:
  - runner_selection
  - pipeline_creation

runner_selection:
  stage: runner_selection
  tags:
    - ANY
  script:
    # $CI_RUNNER_TAGS looks like '["tag1", "tag2"]'
    - echo "SELECTED_RUNNER=$CI_RUNNER_TAGS" >> runner_selection.env
  artifacts:
    reports:
      dotenv: runner_selection.env

pipeline_creation:
  stage: pipeline_creation
  trigger:
    include: 
      - local: .gitlab-ci-child.yml
        inputs:
          tags: $SELECTED_RUNNER # FAILS with: `tags` input: provided value is not an array

# contents of .gitlab-ci-child.yml
spec:
  inputs:
    tags:
      type: array
---
stages:
  - build

build:
  stage: build
  tags: $[[ inputs.tags ]]
  script:
    - echo "build"

Another attempt using spec:inputs with type string:

# contents of .gitlab-ci.yml

stages:
  - runner_selection
  - pipeline_creation

runner_selection:
  stage: runner_selection
  tags:
    - ANY
  script:
    # $CI_RUNNER_TAGS looks like '["tag1", "tag2"]'
    - echo "SELECTED_RUNNER=$CI_RUNNER_TAGS" >> runner_selection.env
  artifacts:
    reports:
      dotenv: runner_selection.env

pipeline_creation:
  stage: pipeline_creation
  trigger:
    include: 
      - local: .gitlab-ci-child.yml
        inputs:
          tags: $SELECTED_RUNNER

# contents of .gitlab-ci-child.yml
spec:
  inputs:
    tags:
      type: string
---
stages:
  - build

build:
  stage: build
  tags: $[[ inputs.tags ]] # FAILS with: jobs:build:tags config should be an array of strings
  script:
    - echo "build"

Versions

GitLab Enterprise Edition v17.0.8-ee

Helpful resources

1 Like