GitLab CI:两个独立的定时任务

27

请考虑以下 gilab-ci.yml 脚本:

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

我希望能够独立地安排这两个工作的时间,但是要遵循以下规则:

  • build_for_ui_automation每天上午5点运行。
  • independent_job每天下午5点运行。

然而,目前的设置只能触发整个流程,这将按顺序执行两个作业。

如何安排定时任务以仅触发单个作业?

2个回答

21

注意 - 此答案仅使用 GitLab CI 中的除外来操作添加到计划中的作业。然而,截至今天,GitLab 已停止对相同命令的主动维护,并建议我们改用规则。这是链接

我已修改原始答案,使用了规则并测试了其工作。

要扩展@Naor Tedgi的答案,您可以在管道计划中定义一个变量。例如,在build_for_ui_automation的计划中设置SCHEDULE_TYPE = "build_ui",在independent_job的计划中设置SCHEDULE_TYPE = "independent"。然后,您的.gitlab-ci.yml文件可以修改为:

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

请注意,在only部分的语法变化,仅在计划表中执行作业,并且当计划变量匹配时执行。


2
这是我采用的解决方案。 - Richard Topchii
@RichardTopchii,您能否接受它,因为您使用了它? :) - vvvvv
Naor Tedgi是第一个回答我的问题的人,而且他的回答非常有帮助。 - Richard Topchii
@RichardTopchii,如果您现在使用的话,将此标记为答案可能会帮助下一个遇到相同问题的人。 - AlexM

8
在你的 GitLab 项目中,进入 CI/CD -> Schedules 页面,点击“新建计划”按钮,按照需要配置任务并设置时间和间隔。
最后为每个变量添加一个变量。
然后通过在 gitlab.yml 文件中添加这些变量来编辑它们,添加到only部分,如下所示: https://docs.gitlab.com/ee/ci/variables/#environment-variables-expressions

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接