Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI: Two independent scheduled jobs

Consider the following gilab-ci.yml script:

stages:
  - build_for_ui_automation
  - independent_job

variables:
  LC_ALL: "en_US.UTF-8"
  LANG: "en_US.UTF-8"

before_script:
  - gem install bundler
  - bundle install

build_for_ui_automation:
  dependencies: []
  stage: build_for_ui_automation
  artifacts:
    paths:
      - fastlane/screenshots
      - fastlane/logs
      - fastlane/test_output
      - fastlane/report.xml
  script:
    - bundle exec fastlane ui_automation
  tags:
    - ios
  only:
    - schedules
  allow_failure: false

# This should be added and trigerred independently from "build_for_ui_automation"
independent_job:
  dependencies: []
  stage: independent_job
  artifacts:
    paths:
      - fastlane/screenshots
      - fastlane/logs
      - fastlane/test_output
      - fastlane/report.xml
  script:
    - bundle exec fastlane independent_job
  tags:
    - ios
  only:
    - schedules
  allow_failure: false

I'd like to be able to schedule these two jobs independently, but following the rules:

  • build_for_ui_automation runs every day at 5 AM
  • independent_job runs every day at 5 PM

However, with the current setup I can only trigger the whole pipeline, which will go through both jobs in sequence.

How can I have a schedule triggering only a single job?

like image 604
Richard Topchii Avatar asked Jun 20 '19 13:06

Richard Topchii


People also ask

How do I run two GitLab jobs parallelly?

To run our tests in parallel as part of our GitLab CI/CD pipeline, we need to tell GitLab to start multiple test jobs at the same time. In each of the test jobs we then run a different subset of our test suite. For our example project we start by adding separate build , test and deploy jobs.

Can you have multiple GitLab CI files?

No, you can't have multiple gitlab-ci files per repository.

Do GitLab jobs run in parallel?

GitLab provides a method to make clones of a job and run them in parallel for faster execution using the parallel: keyword. While parallel jobs may not help in reducing the consumption of CI minutes, they definitely help increase work productivity.

Can GitLab runner run multiple jobs?

One way to allow more jobs to run simultaneously is to simply register more runners. Each installation of GitLab Runner can register multiple distinct runner instances. They operate independently of each other and don't all need to refer to the same coordinating server.


2 Answers

To build off @Naor Tedgi's answer, you can define a variable in your pipeline schedules. For example, set SCHEDULE_TYPE = "build_ui" in the schedule for build_for_ui_automation and SCHEDULE_TYPE = "independent" in the schedule for independent_job. Then your .gitlab-ci.yml file could be modified as:

stages:
  - build_for_ui_automation
  - independent_job

variables:
  LC_ALL: "en_US.UTF-8"
  LANG: "en_US.UTF-8"

before_script:
  - gem install bundler
  - bundle install

build_for_ui_automation:
  dependencies: []
  stage: build_for_ui_automation
  artifacts:
    paths:
      - fastlane/screenshots
      - fastlane/logs
      - fastlane/test_output
      - fastlane/report.xml
  script:
    - bundle exec fastlane ui_automation
  tags:
    - ios
  only:
    refs:
      - schedules
    variables:
      - $SCHEDULE_TYPE == "build_ui"
  allow_failure: false

# This should be added and trigerred independently from "build_for_ui_automation"
independent_job:
  dependencies: []
  stage: independent_job
  artifacts:
    paths:
      - fastlane/screenshots
      - fastlane/logs
      - fastlane/test_output
      - fastlane/report.xml
  script:
    - bundle exec fastlane independent_job
  tags:
    - ios
  only:
    refs:
      - schedules
    variables:
      - $SCHEDULE_TYPE == "independent"
  allow_failure: false

where note the syntax change in the only sections to execute the jobs only during schedules and when the schedule variable is matching.

like image 198
AlexM Avatar answered Oct 08 '22 11:10

AlexM


in gitlab inside your project go to CI/CD -> Schedules press new Schedule button config the tasks as you want set the time and interval

now at the end add a variable for each one of them

now edit your gitlab.yml by adding that variable at the only section

as shown here below

https://docs.gitlab.com/ee/ci/variables/#environment-variables-expressions

like image 20
Naor Tedgi Avatar answered Oct 08 '22 09:10

Naor Tedgi