GitLab CI: Two independent scheduled jobs
Asked Answered
T

2

28

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?

Teaspoon answered 20/6, 2019 at 13:18 Comment(0)
I
8

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

Irishirishism answered 20/6, 2019 at 13:30 Comment(0)
B
21

Note - This answer used, only-except of GitLab CI to manipulate what job gets added to the schedule. However, as of today, GitLab has stopped active maintenance of the same commands and suggests we use rules instead. Here's the link.

I have modified the original answer to use rules and tested the working.

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
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "build_ui"'
      when: always
      variables:
        SCHEDULE_TYPE: "build_ui"
        ANOTHER_VARIABLE: "dummy"
      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
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "independent"'
      when: always
      variables:
        SCHEDULE_TYPE: "independent"
        ANOTHER_VARIABLE: "dummy123"
      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.

Bibliographer answered 17/11, 2020 at 15:55 Comment(4)
This was the solution I went with.Teaspoon
@RichardTopchii would you mind accepting it, since you used it? :)Cholon
Naor Tedgi was the 1st one and with a huge margin, when I actually needed that answer...Teaspoon
@RichardTopchii, it may help the next person who has the same problem to mark this as the answer though if that's what you use now.Bibliographer
I
8

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

Irishirishism answered 20/6, 2019 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.