1

I have a GitLab CI job that I want to only run for tags or for two special branches (develop, main).

only:
  - develop
  - main
  - tags

This job has a dynamic variable that I set using the rules feature:

variables:
  IMAGE_TAG: $CI_COMMIT_TAG # defaults to the pushed commit tag name
rules:
  - if: $CI_COMMIT_REF_NAME == 'develop'
    variables:
      IMAGE_TAG: 'develop'
  - if: $CI_COMMIT_REF_NAME == 'main'
    variables:
      IMAGE_TAG: 'main'

The linter gives me this error though:

jobs:build config key may not be used with rules: only

I looked around and it looks like you can't use rules together with only.

If I remove the only directive, then this job will also run on any branch. How do I adjust my script so that I can set the variable dynamically but also limit the job to only run on tags and certain branches?

dokgu
  • 4,957
  • 3
  • 39
  • 77

1 Answers1

2

The equivalent rules: to this would be:

rules:
  - if: $CI_COMMIT_BRANCH == "develop"
    # ... insert `variables:` here if you want...
  - if: $CI_COMMIT_BRANCH == "main"
    # ...
  - if: $CI_COMMIT_TAG

In this case, you also don't need to set your IMAGE_TAG variable conditionally. You can just use $CI_COMMIT_REF_NAME which will be equal to $CI_COMMIT_BRANCH for branch pipelines or $CI_COMMIT_TAG for tag pipelines.

sytech
  • 29,298
  • 3
  • 45
  • 86