Amir Damirov
Amir Damirov

Reputation: 97

GITLAB CI pipeline, run job only with git tag

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

Upvotes: 0

Views: 6197

Answers (2)

SPMSE
SPMSE

Reputation: 628

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/*

Upvotes: 1

slauth
slauth

Reputation: 3178

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

Upvotes: 2

Related Questions