gitlabgitlab-cigitlab-ci-runnergitlab-api

How to trigger a GitLab pipeline for a tagged commit on branch master?


I want to trigger pipelines once I push tagged commits to the branch master. Although, I'm not sure if it works, since I have had no success so far.

.gitlab-ci.yml:

variables:
  JEKYLL_ENV: production
  LC_ALL: C.UTF-8

stages:
  - publish
  - release
  # - deploy

pages:
  stage: publish
  image: ruby
  rules:
    - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG != ""
      when: always
  before_script:
    - gem install bundler
    - bundle install
  script:
    - bundle exec jekyll build -d public
  artifacts:
    paths:
    - public

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG != ""
      when: always
  script:
    - echo 'running release job'
  release:
    name: 'Release $CI_COMMIT_TAG'
    description: 'Created using gitlab-ci.yml. $EXTRA_DESCRIPTION'
    tag_name: '$CI_COMMIT_TAG' 
    ref: '$CI_COMMIT_TAG'
  artifacts:
    paths:
    - public
    expire_in: 1 day

What I have tried:

`- if: '$CI_COMMIT_TAG && $CI_COMMIT_BRANCH == "master"'`

Although, this last rule doesn't trigger pipelines at all.


Solution

  • You can check if the commit has a tag

    rules:
      - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG != ""
        when: manual
    

    or if your tag follows a convention like tag-1, tag-2, use a regex pattern

    rules:
      - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG =~ /tag\-[0-9]/
        when: manual