21

I have following gitlab-ci conf. file:

before_script:
  - echo %CI_BUILD_REF%
  - echo %CI_PROJECT_DIR%

stages:
  - createPBLs
  - build
  - package


create PBLs:
  stage: createPBLs
  script: 
    - xcopy /y /s "%CI_PROJECT_DIR%" "C:\Bauen\"
    - cd "C:\Bauen\"
    - ./run_orcascript.cmd


build:
  stage: build
  script:
  - cd "C:\Bauen\"
  - ./run_pbc.cmd
  except:
  - master

build_master:
  stage: build
  script:
  - cd "C:\Bauen\"
  - ./run_pbcm.cmd
  only:
  - master

package:
  stage: package
  script:
  - cd "C:\Bauen\"
  - ./cpfiles.cmd
  artifacts:
    expire_in: 1 week
    name: "%CI_COMMIT_REF_NAME%"
    paths:
      - GitLab-Build

How can I add the rule that the pipeline will ONLY trigger if a new tag has been added to a branch? The tag should start with "Ticket/ticket_"

Currently he is building for every push.

Hendouz
  • 491
  • 1
  • 7
  • 17

3 Answers3

23

You need to use only syntax:

only:
  - tags

This would trigger for any Tag being pushed. If you want to be a bit more specific you can do:

only:
  - /Ticket\/ticket\_.*/

which would build for any push with the Ticket/ticket_ tag.

Rekovni
  • 6,319
  • 3
  • 39
  • 62
  • Oh, that simple! Need i add this after every stage/ Job? – Hendouz Mar 27 '18 at 14:11
  • @Hendouz Yes, otherwise the other stages would be run on every push. – Rekovni Mar 27 '18 at 14:12
  • 1
    The pipeline runs aswell if i name a Branch as "Ticket/Ticket_xxx" but i want the pipeline only run if it has a tag with it. @Rekovni – Hendouz Mar 27 '18 at 14:41
  • 2
    Hmm, you might need to add `except: - branches` – Rekovni Mar 27 '18 at 14:54
  • I think I was mistaken in my more specific way of doing things, as that is more for any `refs` with `Ticket/ticket_`. Perhaps just use `only: - tags` which would kick off your build with any tags. – Rekovni Mar 27 '18 at 15:19
  • Is there any solution @Rekovni ? i still need a special syntax for tags..for now he triggers the build if there is ANY tag. – Hendouz Jun 05 '18 at 10:51
  • 3
    ⚠️ `only and except are not being actively developed. rules is the preferred keyword to control when to add jobs to pipelines.` [Gitlab 14.9](https://docs.gitlab.com/14.9/ee/ci/yaml/#only--except) – frouo Apr 06 '22 at 09:12
  • any sample for filtering semantic versioning tags, and only trigger when the tag is on `main` or `master` branch? – Bill Jul 16 '22 at 11:34
9

below could be more readable, see only:varibles@gitlab-ci docs with refs:tags

only:
  refs:
    - tags
  variables:
    - $CI_COMMIT_TAG =~ /^[Tt]icket-.*/
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
5

I recommend to use pattern in varibles-expression using commits

Example

build_api:
 stage: build
 script:
  - docker build --pull -t $CONTAINER_TEST_IMAGE .
  - docker push $CONTAINER_TEST_IMAGE
only:
  variables:
   - $CI_COMMIT_MESSAGE =~ /(\[pipeline\]|(merge))/     

Here i am saying that only execute that job when have [pipeline] or merge inside the commit. More info, here in gitlab

Alex Montoya
  • 4,697
  • 1
  • 30
  • 31