Search code examples
gitlabcontinuous-integrationgitlab-cigitlab-ci-runnercicd

Gitlab CI/CD: Schedule only for a particular job and exclude it from the pipeline


Consider I have the following .gitlab-ci.yml, and they are running fine in Pipeline:

image: 

stages:
  - job1
  - job2
  - job3
       
job1:
  dependencies: []
  stage: job1      
  script:

job2:
  dependencies: []
  stage: job2     
  script:

job3:
  dependencies: []
  stage: job3    
  script:
 

Now, what I would like to achieve is:

  • to exclude job3 from the Pipeline
  • and to create a schedule only for this job3

I have read option to configure schedule, using rules, so I edit the job3 as follow:

job3:
  dependencies: []
  stage: job3
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule"
  script:

And I setup a schedule. But unfortunately, it doesn't work out: job3 is still part of the Pipeline, and my schedule runs all the jobs.

What is missing in this configuration? Or is it possible to achieve it.


Solution

  • define rules for every job:

    job1:
      dependencies: []
      stage: job1  
      rules:  
        - if: '$CI_PIPELINE_SOURCE != "schedule"'
          when: always
      script:
    
    job2:
      dependencies: []
      stage: job2  
      rules:  
        - if: '$CI_PIPELINE_SOURCE != "schedule"'
          when: always  
      script:
    
    job3:
      dependencies: []
      stage: job3
      rules:
        - if: '$CI_PIPELINE_SOURCE == "schedule"'
          when: always
      script: