Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 791
Alex Cohen Avatar asked Jan 22 '21 18:01

Alex Cohen


People also ask

How do I set variables in Gitlab?

In GitLab, CI/CD variables can be defined by going to Settings » CI/CD » Variables, or by simply defining them in the . gitlab-ci. yml file. Variables are useful for configuring third-party services for different environments, such as testing , staging , production , etc.

How do Gitlab runner tags work?

Creating Gitlab Runner Tags on the Registration Process If you enable that option, the Runner will only pick up projects explicitly using the Runner's tags. If you do not enable that option, this Runner will be configured as “Shared Runner,” so Gitlab will share the Runner for any project that doesn't specify a Tag.

Is it possible to create dynamic job tags in GitLab?

There is a new feature in 14.4 called "Nested variable expansion" which can provide exactly what you want for dynamic runner tags. See documenation docs.gitlab.com/ee/ci/variables/… Dynamic job tags is achievable now (tested on 14.10).

How to set GitLab-CI variables dynamically?

How to set gitlab-ci variables dynamically? There is currently no way in GitLab to pass environment variable between stages or jobs. Current workaround is to use artifacts - basically pass files. We had a similar use case - get Java app version from pom.xml and pass it to various jobs later in the pipeline.

How to run a manual job with additional variables in GitLab?

Introduced in GitLab 12.2. When running manual jobs you can supply additional job specific variables. You can do this from the job page of the manual job you want to run with additional variables. To access this page, click on the name of the manual job in the pipeline view, not the play () button.

Is it possible to create a dynamic job tag?

Dynamic job tags is achievable now (tested on 14.10). Variable interpolation works for job tags, and one way you can dynamically modify CI Variables is with the rules:variables key.


Video Answer


3 Answers

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.

like image 97
Rekovni Avatar answered Oct 14 '22 05:10

Rekovni


Dynamic job tags is achievable now (tested on 14.10). Variable interpolation works for job tags, and one way you can dynamically modify CI Variables is with the rules:variables key.

The following example would result in a default job tag of "staging" for most branches, and an override value of "production" for pipelines running against the project's default branch.

job:
  variables:
    RUNNER_TAG: staging           # default CI variable value
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      variables:
        RUNNER_TAG: production    # override CI variable value when the if: check is true
  tags:
    - $RUNNER_TAG                 # use the dynamic CI variable as a job tag
  script:
    - hello world from runner matching $RUNNER_TAG
like image 43
ashtonium Avatar answered Oct 14 '22 06:10

ashtonium


Since this is not possible on older versions of Gitlab, as a work around for those on older verisons of Gitlab, to extend on the answer given by Rekovni, if you're looking to run environment specific runners:

.Deploy_template:
  stage: Deploy
  image:
    name: busybox/busybox:latest
    entrypoint: [""]
  rules:
    - when: manual
  before_script:
    - echo "Preflight tests"
  script:
    - echo "Start Deployment"
  after_script:
    - echo "Postflight tests"
  allow_failure: false


Deploy_feature:
  extends: .Deploy_template
  tags:
    - docker
    - feature
  rules:
    - if: $BRANCH =~ /feature/

Deploy_develop:
  extends: .Deploy_template
  tags:
    - docker
    - develop
  rules:
    - if: $BRANCH =~ /develop/

Deploy_stable:
  extends: .Deploy_template
  tags:
    - docker
    - stable
  rules:
    - if: $BRANCH =~ /stable/

Then you just repeat this for every job you have.

like image 1
doublespaces Avatar answered Oct 14 '22 06:10

doublespaces