Search code examples
gitlabgitlab-ci

How to set a dynamic gitlab job tag with a variable?


I am trying to create a dynamic job that can switch between two gitlab runners depending on which tag it is given. I would like to do this with an environmental variable, but it seems this cannot be used. The following job:

runner_test:
  image: alpine
  tags:
    - $MY_RUNNER
  stage: deploy_main
  script:
    - echo foobar
  retry: 2

Results in a paused pipeline with the error: This job is stuck because you don't have any active runners online or available with any of these tags assigned to them: $MY_RUNNER


Solution

  • This is currently not available. There is currently an open issue which is in the backlog requesting this feature: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1809.


    A work around perhaps, using rules and extends:

    .template:
      stage: deploy_main
      script:
        - echo foobar
    
    runner_test_1:
      extends: .template
      tags:
        - runner_1
      rules:
        - if: $RUNNER_TAG == runner_1
        
    runner_test_2:
      extends: .template
      tags:
        - runner_2
      rules:
        - if: $RUNNER_TAG == runner_2
    

    or something to that effect.