10

I know that I can't have multiple gitlab-ci.yml files in one repo, but it still seems fairly limited. Say for example I have one set of tests I want to run whenever a change is pushed or on PRs, and another set I want to run every 24 hours. Is there a way to do that, or do I only get one set of tests I have to use whenever I want to run CI?

2 Answers 2

14

Yes, you can use the rules syntax. You can use this in combination with regex for commit message, ci_pipeline_source or any other available CI variables.


job1:
  script: 
    - do something on schedule only
  rules:
    - if: '"$CI_PIPELINE_SOURCE" == "schedule"'
      when: always

job2:
  script: 
    - runs on Merge request pipeline
  rules:
    - if: $CI_MERGE_REQUEST_ID
      when: always
    when: never

job2:
  script: 
    - runs on changes to anything in src directory or Dockerfile
  rules:
    - changes:
        - src/*
        - Dockerfile

Note you can also include multiple CI templates in your gitlab-ci.yml. Additonally, you can include a CI template with "hidden" job templates that you can reference with extends:

# Include ci files 
include:
 - local: '/templates/.scheduled-job-template.yml' # CI template with hidden job (.scheduled_job) that runs on schedule
 - local: '/templates/.lint-and-validate.yml' # contains jobs for linting and validating project

job1:
  extends:
    - .scheduled_job
  rules:
    - if: '"$CI_PIPELINE_SOURCE" == "schedule"'
      when: always

job2:
  script: 
    - runs on Merge request pipeline
  rules:
    - if: $CI_MERGE_REQUEST_ID
      when: always
    when: never

In doing this you can compose the jobs/pipelines you want in its own yml file and then define the jobs using those templates in the gitlab-ci.yml, which will help keep things maintainable and clear if you are running numerous different pipeline/pipeline configurations from the same project.

0

I guess you can do it via rules keyword. It has multiple condition with environment variables or branch name or schedules. If you add detail about you want to do, i try to

Gitlab CI Rules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.