Gitlab CI/CD running pipeline only on tag push and only if specific files changed

Viewed 722

I have a monorepository with a lot of things that needs to be deployed seperately.

Now i wonder if it is possible to run a task only if a tag is pushed and if there are changes in a certain directory.

I've tried the following:

.components:
  only:
    changes:
      - source/packages/components/**/*
  except:
    - branches
.components:
  only:
    refs:
      - tags
    changes:
      - source/packages/components/**/*

.components:
  rules:
    - if: $CI_COMMIT_TAG
    changes:
      - source/packages/components/**/*

.components:
  rules:
    - if: $CI_COMMIT_TAG
      changes:
        - source/packages/components/**/*
      when: always
    - when: never

This is one of the .gitlab-ci.yml files i use. Each of then looks the same except of a few things.

.components:
  rules:
    - if: '$CI_COMMIT_TAG'
      changes:
        - source/packages/components/**/*

build_componets:
  stage: build
  extends: .components
  needs: []
  script:
    - echo "Compiling the components code..."

test_componets:
  stage: test
  extends: .components
  needs: ["build_componets"]
  script:
    - echo "Compiling the components code..."

deploy_componets:
  stage: deploy
  extends: .components
  needs: ["test_componets"]
  script:
    - echo "Compiling the components code..."

Currently behavior: Every pipeline runs... regardless of this configurations but only when I'm pushing a tag.

All of those files are imported to a main file at the project root.

stages:
  - build
  - test
  - deploy

include:
  - source/packages/business-logic/.gitlab-ci.yml
  - source/packages/components/.gitlab-ci.yml
  - source/applications/native/.gitlab-ci.yml
  - source/services/user-service/.gitlab-ci.yml

And the merged YAML looks like this.

---
".business-logic":
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
build_business_logic:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
  stage: build
  extends: ".business-logic"
  needs: []
  script:
  - yarn install
  - yarn workspace @zeou/business-logic build
test_business_logic:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
  stage: test
  extends: ".business-logic"
  needs:
  - build_business_logic
  script:
  - echo "Compiling the business-logics code..."
deploy_business_logic:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/business-logic/**/*
    when: always
  - when: never
  stage: deploy
  extends: ".business-logic"
  needs:
  - test_business_logic
  script:
  - echo "Compiling the business-logics code..."
".components":
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
build_components:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
  stage: build
  extends: ".components"
  needs: []
  script:
  - echo "Compiling the components code..."
test_components:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
  stage: test
  extends: ".components"
  needs:
  - build_components
  script:
  - echo "Compiling the components code..."
deploy_components:
  rules:
  - if: "$CI_COMMIT_TAG"
    changes:
    - source/packages/components/**/*
    when: always
  - when: never
  stage: deploy
  extends: ".components"
  script:
  - echo "Compiling the components code..."
stages:
- ".pre"
- build
- test
- deploy
- ".post"
before_script: 

Can someone tell me what i'm doing wrong ?

Thanks 100 times :D

1 Answers

Use rules: in this case to combine a changes: clause with an if: rule to check if there is a tag.

myjob:
  rules:
    - if: "$CI_COMMIT_TAG"
      changes:
        - source/services/user-service/**/*
   # ...
Related