Sah
Sah

Reputation: 1067

GitLab-CI: run job only when all conditions are met

In GitLab-CI document, I read the following contents:

In this example, job will run only for refs that are tagged, or if a build is explicitly requested via an API trigger or a Pipeline Schedule:

job:
  # use special keywords
  only:
    - tags
    - triggers
    - schedules

I noticed the document uses or instead of and, which means the job is run when either one condition is met. But what if I want to configure a job to only run when all conditions are met, for instance, in a Pipeline Schedule and on master branch?

Upvotes: 6

Views: 5519

Answers (2)

binhdt2611
binhdt2611

Reputation: 17

Updated - 2023

But what if I want to configure a job to only run when all conditions are met, for instance, in a Pipeline Schedule and on master branch?

I would suggest that we should use rules instead of only in GitLab pipelines. For example, this should work for running a pipeline on master/main when it is scheduled.

job:
  script:
    - echo "Hello"
  rules:
    - if: $CI_PIPELINE_SOURCE == 'schedule' && $CI_COMMIT_REF_SLUG == $CI_DEFAULT_BRANCH

Upvotes: 0

Stefan van Gastel
Stefan van Gastel

Reputation: 4478

If your specific question is how do I only run a pipeline on master when it was scheduled this should work:

job:
  only:
    - master
  except:
    - triggers
    - pushes
    - external
    - api
    - web

In this example you exclude all except for the schedules 'trigger' and only run for the master branch.

Upvotes: 7

Related Questions