Dynamically pass runner tags to job

I have gitlab runners on ec2s in 3 different AWS accounts. Each looking for tags based on there account name. I want a given CICD job to execute on a runner in the account based on their branch name

So lets say my 3 AWS accounts are: dev, stage and prod
I have runners on ec2s in each account looking for jobs with the tag over their account name(dev or stage or prod)

I would like feature branches to run on the dev runners, development branch to run on the stage runners and master/main branch to run on the prod runners.

So far I have only been able to get this to work by making 3 identical copies of the job definition. Seems like I should be able to dynamically set the runner tag based on branch somehow. I played around with workflow but could not get that to work

serverlessapp-dev:
stage: terraform
except:
refs:
- development
- master
tags:
- dev
image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
variables:
TF_ROOT: ${CI_PROJECT_DIR}/tf/
script:
- gitlab-terraform init
- gitlab-terraform apply
serverlessapp-stage:
stage: terraform
only:
refs:
- development
tags:
- stage
image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
variables:
TF_ROOT: ${CI_PROJECT_DIR}/tf/
script:
- gitlab-terraform init
- gitlab-terraform apply
serverlessapp-prod:
stage: terraform
only:
refs:
- master
tags:
- prod
image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
variables:
TF_ROOT: ${CI_PROJECT_DIR}/tf/
script:
- gitlab-terraform init
- gitlab-terraform apply

This seems cumbersome. I really want some way to dynamically set the tags: value. Like maybe a rule. Is that possible?

serverlessapp:
stage: terraform
tags:
rules:
- if: β€˜$CI_COMMIT_BRANCH == β€œmain”’
- prod
- if
image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
variables:
TF_ROOT: ${CI_PROJECT_DIR}/tf/
script:
- gitlab-terraform init
- gitlab-terraform apply

1 Like