2

I wanna trigger pipelines once I push tagged commits to 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.

jurordrained
  • 131
  • 2
  • 9

1 Answers1

1

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
Miguel Trejo
  • 5,913
  • 5
  • 24
  • 49
  • it works for the publish job and thank you for that! although, it won't work for the release one. `ref: '$CI_COMMIT_TAG'` it seems like this variable is undefined, since the logs says it's missing ref value. – jurordrained Mar 17 '21 at 20:46
  • whenever I mention `$CI_COMMIT_TAG` on release job, I get a different error depending on the key. For the ref key, I get error 422: Ref is not specified. – jurordrained Mar 17 '21 at 20:47
  • weird thing is that using this variable like that was working fine, earlier. – jurordrained Mar 17 '21 at 20:48
  • @jurordrained does your release runs after the tag job ? if these jobs are defined in the same pipeline you can share variables between them through an artifact – Miguel Trejo Mar 17 '21 at 23:52
  • I have updated the `.gitlab-ci.yml` code, if you don't mind. – jurordrained Mar 18 '21 at 09:29
  • this is not working on my gitlab: ```- if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG =~ /^[0-9]+(?:.[0-9]+)+-RC(.*)$/``` – Vahid Alimohamadi Jun 18 '22 at 08:04
  • This solution doesn't work since `CI_COMMIT_BRANCH` and `CI_COMMIT_TAG` have mutually exclusive availability in any given pipeline. If `CI_COMMIT_TAG` is present, that means it is a tag pipeline and `CI_COMMIT_BRANCH` is undefined. Conversely, if `CI_COMMIT_BRANCH` is present, `CI_COMMIT_TAG` must not be present. This is because, in terms of git semantic, tags do not belong to any branch, nor do they have any association with branches. Tags only point to a commit SHA which may be present in multiple branches or even no branches at all. – sytech Jul 20 '23 at 17:57