How to trigger a build for specific branch on tag creation (.gitlab-ci.yml config)?

I want to trigger different builds on events connected with branch “dev” for two scenarios:

  1. push commit to dev branch:
  • perform build 1

This is simple. In .gitlab-ci.yml i have:

build1:
  only:    
    - develop
  1. tag only on branch dev:
  • perform build 2

In .gitlab-ci.yml i have:

build2:
  only:
    - develop
    - tags

This triggers a build on every tag creation in repo, even on master branch. This is not what I want from CI. I want to trigger build only if tag is created on dev branch. Is it possible with gitlab CI?

No.

tl;dr;
Tags are created from commits, not branches. There is no reference in git between tag and branch.
Such relation is not tracked in GitLab either so you can’t have rule based on from which branch is tag created, because there is no such thing.

I found a way to solve it.
Use different types of label structures to mark branches from.
I have three branches (dev test master), the script is as follows:

variables:
  # test_tag
  TEST_TAG: beta
stages:
  - develop_pre
  - product_pre
  - test_pre

###########################   development env   ####################################
develop_pre-job: # When I push code on dev branch, it fires
  stage: develop_pre
  script:
    - whoami
  only:
    - dev
  tags:
    - smart_platform_runner

###########################   product env   ####################################
product_pre-job: # When I use a tag that does not have beta, it fires
  stage: product_pre
  script:
    - docker info
    - docker login ${HARBOR_ADDRESS} --username ${HARBOR_LOGIN_USERNAME} --password ${HARBOR_LOGIN_PASSWORD}
    - echo $CI_COMMIT_TAG
  rules:
    - if: '$CI_COMMIT_TAG != null && $TEST_TAG !~ $CI_COMMIT_TAG'
  tags:
    - smart_platform_product_runner
###########################   test env   ####################################
test_pre-job: # When I use a tag with beta, it fires
  stage: test_pre
  script:
    - whoami
    - docker info
    - docker login ${HARBOR_ADDRESS} --username ${HARBOR_LOGIN_USERNAME} --password ${HARBOR_LOGIN_PASSWORD}
    - echo $CI_COMMIT_TAG
  rules:
    - if: '$CI_COMMIT_TAG != null && $TEST_TAG =~ $CI_COMMIT_TAG'
  tags:
    - ftp-server

1 Like