0

I just want to run pipelines when tagged from main branch. I tried using workflow but it doesn't work.
This is my .gitlab-ci.yml file.

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "develop"'
      variables:
        CHART_GIT_URL: $CHART_DEV_URL
        CHART_VALUES_FILE: "values-dev.yaml"
        DOCKER_IMAGE_TAG: "dev-$CI_COMMIT_SHORT_SHA"
    - if: $CI_COMMIT_TAG && $CI_COMMIT_BRANCH == "main"
      variables:
        CHART_GIT_URL: $CHART_PROD_URL
        CHART_VALUES_FILE: "values-prod.yaml"
        DOCKER_IMAGE_TAG: "v$CI_COMMIT_TAG"

stages:
  - build and push
  - deploy

package Docker image:
  stage: build and push
  before_script:
    - docker login $DOCKER_REGISTRY -u $DOCKER_REGISTRY_USER -p $DOCKER_REGISTRY_PASSWD
  script:
    - docker build -t $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG .
    - docker push $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
  rules:
    - if: '$CI_COMMIT_BRANCH == "develop"'
    - if: $CI_COMMIT_TAG && $CI_COMMIT_BRANCH == "main"

Thanks for the help!

quoc9x
  • 1,423
  • 2
  • 9
  • 26

1 Answers1

3

tagged from main branch

Unfortunately, this is not possible. Git tags are only associated with commits, not branches. Therefore, you cannot create a condition for a tag to be created "from" a branch because that's not how tags work. Also consider that a tagged ref can exist on many branches, or even no branch at all.

This is also the reason why the predefined variables CI_COMMIT_TAG and CI_COMMIT_BRANCH will never be present together. If a pipeline is associated with a tag, it cannot be associated with a branch and vice versa.

The best you might be able to do is to run only on tags, then check if the tagged ref exists in main in the job itself. Unfortunately this is not possible to do with rules:.

sytech
  • 29,298
  • 3
  • 45
  • 86
  • `The best you might be able to do is to run only on tags, then check if the tagged ref exists in main in the job itself.` - Can you give me an example of this? – quoc9x Apr 29 '22 at 04:21