0

need help from GitLab gurus. I have a following pipeline below. I expect "sync_s3:prod" job will run only when i will push new git tag. But gitlab trigger both jobs. Why its behaving like this ? I create $git_commit_tag rule only for one job. Any ideas?

stages:
  - sync:nonprod
  - sync:prod

.sync_s3:
  image:
    name: image
    entrypoint: [""]
  script:
    - aws configure set region eu-west-1
    - aws s3 sync ${FOLDER_ENV} s3://img-${AWS_ENV} --delete

sync_s3:prod:
  stage: sync:prod
  rules:
    - if: $CI_COMMIT_TAG
      changes:
        - prod/*
  extends: .sync_s3
  variables:
    AWS_ENV: prod
    FOLDER_ENV: prod/
  tags:
    - gaming_prod

sync_s3:nonprod:
  stage: sync:nonprod
  rules:
    - changes:
        - pp2/*
  extends: .sync_s3
  variables:
    AWS_ENV: nonprod
    FOLDER_ENV: pp2/
  tags:
    - gaming_nonprod
torek
  • 448,244
  • 59
  • 642
  • 775
Amir Damirov
  • 87
  • 3
  • 14
  • Well, I missunderstood your question. Check out this link: https://stackoverflow.com/questions/42796018/how-to-run-a-gitlab-ci-yml-job-only-on-a-tagged-branch – Baumflaum Nov 25 '21 at 13:32
  • `But gitlab trigger both jobs` When? On tag pipeline or on commit pipeline? – KamilCuk Nov 25 '21 at 13:34
  • @KamilCuk When i do "git push origin newtag" it trigger both jobs. But as you see i wrote this rule only for one job. – Amir Damirov Nov 25 '21 at 13:38
  • So you pushed a tag? You stated that `I expect "sync_s3:prod" job will run only when i will push new git tag`, which is true, your job was run on a tag, seems like your requirement is satisfied. So you want to __not__ run the _other_ job on tag? – KamilCuk Nov 25 '21 at 14:14
  • Yes, exactly i dont want to run other job on tag. What i want to perform fully: - Run non-prod job only when i change files inside "pp2" folder. - Run prod job only when there is new git tag. – Amir Damirov Nov 26 '21 at 10:11

2 Answers2

2

If I understand the question correctly, you do not want to have the sync_s3:nonprod job run if the sync_s3:prod is run. (?)

To achieve this, on the sync_s3:nonprod job you should be able to copy the same rule from sync_s3:prod together with when: never:

stages:
  - sync:nonprod
  - sync:prod

.sync_s3:
  image:
    name: image
    entrypoint: [""]
  script:
    - aws configure set region eu-west-1
    - aws s3 sync ${FOLDER_ENV} s3://img-${AWS_ENV} --delete

sync_s3:prod:
  stage: sync:prod
  rules:
    - if: $CI_COMMIT_TAG
      changes:
        - prod/*
  extends: .sync_s3
  variables:
    AWS_ENV: prod
    FOLDER_ENV: prod/
  tags:
    - gaming_prod

sync_s3:nonprod:
  stage: sync:nonprod
  rules:
    - if: $CI_COMMIT_TAG
      changes:
        - prod/*
      when: never
    - changes:
        - pp2/*
  extends: .sync_s3
  variables:
    AWS_ENV: nonprod
    FOLDER_ENV: pp2/
  tags:
    - gaming_nonprod
slauth
  • 2,667
  • 1
  • 10
  • 18
  • What i want to perform fully: - Run non-prod job only when i change files inside "pp2" folder. - Run prod job only when there is new git tag. – Amir Damirov Nov 26 '21 at 10:12
1

As @slauth already mentions in his answer the rules need to be adjusted per step of the pipeline. I only post this as an answer as an addition to the original answer above. In order to prevent pipeline steps from running when a git-tag is present you need to explicitly set the rule for the corresponding job.

stages:
  - sync:nonprod
  - sync:prod

.sync_s3:
  image:
    name: image
    entrypoint: [""]
  script:
    - aws configure set region eu-west-1
    - aws s3 sync ${FOLDER_ENV} s3://img-${AWS_ENV} --delete

sync_s3:prod:
  stage: sync:prod
  rules:
    - if: $CI_COMMIT_TAG
      changes:
        - prod/*
  extends: .sync_s3
  variables:
    AWS_ENV: prod
    FOLDER_ENV: prod/
  tags:
    - gaming_prod

sync_s3:nonprod:
  stage: sync:nonprod
  rules:
    - changes:
        - pp2/*
    - if: $CI_COMMIT_TAG
      when: never
  extends: .sync_s3
  variables:
    AWS_ENV: nonprod
    FOLDER_ENV: pp2/
  tags:
    - gaming_nonprod

For further clarification:

The following rule will evaluate similar to a logic AND, so this will evaluate to true if there is a $CI_COMMIT_TAG AND there are changes in prod/*. So only when both conditions are met this will be added to the pipeline.

rules:
    - if: $CI_COMMIT_TAG
      changes:
        - prod/*
SPMSE
  • 478
  • 3
  • 14