Search code examples
gitlabgitlab-ci

Gitlab scheduled pipeline also run another job not on schedule


I'm new to this Gitlab CI/CD features, and I encountered the following issues.

I have these 2 jobs in my gitlab-ci.yml, the automation test and my deployment job.

automation_test_scheduled:
  stage: test
  script:
    - yarn test:cypress
  only:
    - schedules

deploy_to_staging:
  stage: deploy-staging
  environment: staging
  only:
    - staging

I want to run my automation test automatically on a daily basis and I have created a new pipeline schedule against my staging branch.

pipeline schedule

however, when the scheduler is triggered, it also runs my deployment job, which is not needed because I only want my automation test to run in the scheduled pipeline. pipeline schedule

Does this happen because my deploy_to_staging job has only: - staging rules? if so, how can I set my scheduled pipeline to only run the automation test without triggering another job?


Solution

  • If you wanted to do this with only/except, it would probably be sufficient to add

    except: 
      - schedules
    

    to your deployment job. Though as
    Though notably, the rules based system is preferred at this point.
    This also allows for more expressive and detailed decisions when it comes to running jobs.
    The simplest way to set the rules for the two jobs would be:

    automation_test_scheduled:
      stage: test
      script:
        - yarn test:cypress
      rules:
        - if: $CI_PIPELINE_SOURCE == "schedule"
    
    deploy_to_staging:
      stage: deploy-staging
      environment: staging
      rules:
        - if: $CI_PIPELINE_SOURCE == "schedule"
          when: never
        - if: $CI_COMMIT_REF_SLUG == "staging"
    

    And that might be all you need.
    Though when it comes to rules, a particularly convenient way of handling them is defining some common rules for the configuration, and reusing these through yaml anchors. The following are some reusable definitions for your case:

    .definitions:
      rules:
        - &if-scheduled 
          if: $CI_PIPELINE_SOURCE == "schedule"
        - &not-scheduled
          if: $CI_PIPELINE_SOURCE == "schedule"
          when: never
        - &if-staging
          if: $CI_COMMIT_REF_SLUG == "staging"
    

    And after that you could use them in your jobs like this:

    automation_test_scheduled:
      stage: test
      script:
        - yarn test:cypress
      rules:
        - *if-scheduled
    
    deploy_to_staging:
      stage: deploy-staging
      environment: staging
      rules:
        - *not-scheduled
        - *if-staging
    

    This way of handling the rules makes it a bit easier to have a overview, and to reuse rules, which in large configurations definitely makes sense