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
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.
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.
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? 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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With