Interaction between workflow:rules and rules

How does using workflow:rules at the top level interact with rules defined (or even not defined) in a job? I haven’t been able to find this in the documentation.

For more reference, I want to be able to combine the two so that I can run a single job on schedule.

First I want to (by default) disable all jobs from running on schedule

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never

But I want this single job to run by essentially overriding the workflow:rules -

scheduled_job:
  stage: test
  script:
    - echo "I ran..."
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: always

Clearly, this wont work because the pipeline will not be created in the first place. How can I achieve this functionality without changing ALL my jobs but by only changing workflow:rules and scheduled_job:rules?

3 Likes

You have to specify rules for reach job. Currently the workflow:rules determine if the pipeline is created as a whole, it is not possible to overwrite it with rules. workflow:rules does not function as default rules for all jobs. There is no interaction between those two.

1 Like

Thats what I thought. Wondering if anyone had any hacks. Thanks for your response.

I’m not sure if there are any useful hacks, but the other thing you can do with schedules is to set CI variables for them. For example:

        - if: $RUN_WEEKLY && $CI_PIPELINE_SOURCE == "schedule" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          when: always
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          when: never
        - when: always

runs weekly on scheduled jobs on the default branch (e.g. master or main); never otherwise on the default branch (so it doesn’t run if $RUN_MONTHLY is set, or whatever); but does run on feature branches (for testing).

Sarah