Search code examples
gitlabgitlab-cistring-matchingcicd

How to use 'only' keyword to select which job to run when tag name ends with some specific keyword


Although this seems to be very easy but seems like I am doing something silly somewhere. I am trying to automate my project where I have decided to run either of the jobs where one is for staging and one is for production. I want my deploy-on-staging job to run when my tag released ends with -stage and deploy-on-prod job to run when my tag release ends with -prod. For this I have used only keyword (instead of rules keyword as it was becoming pain for me). But only keyword is not supposed to be working as expected, for me.

The issue I am getting is even after using the only keyword, both of my jobs are still running. Any pointer would be very helpful.

I am just pasting the code of my gitlab-ci.yml which would be useful to you to resolve this issue. Please ping me if you want something else also from the gitlab-ci.yml file.

Here is my gitlab-ci section:

variables:
  TagName: ${CI_COMMIT_TAG}

deploy-on-staging:
  stage: deploy
  # rules:
  #   - if: '$TagName == "*-stage"'
  #   - if: '$TagName == "*-prod"'
  #     when: manual
  image: ubuntu:20.04
  # tags:
  #   - docker-executor
  before_script:
    - apt-get update
  script:
    - echo "It's here in stage"
    - echo "$TagName =~ '/^*-stage/'"
  only:
    - tags
    - "$TagName =~ '/^*-stage/'"


deploy-on-prod:
  stage: deploy
  image: ubuntu:20.04
  # tags:
  #   - docker-executor
  # rules:
  #   - if: '$TagName == "*-prod"'
  # before_script:
  #   - apt-get update
  script:
    - echo "It's here in prod"
  only:
    - tags
    - $TagName =~ '/^*-prod/'

Solution

  • Only/except are deprecated https://docs.gitlab.com/ee/ci/yaml/#only--except

    to achieve that you want with rules, just use this example

      rules:
        - if: $CI_COMMIT_TAG =~ /-stage/i
          when: always
        - if: $CI_COMMIT_TAG =~ /-prod/i
          when: never