首页 > 解决方案 > 不同阶段的 Gitlab CI 执行时间

问题描述

我敢打赌这只是我缺少的一些愚蠢的事情,但是我应该如何创建我的 gitlab-ci.yml 文件并安排每个晚上执行几个阶段(每晚测试)和一个阶段在周末执行一次(每周测试集)?

目前我有这样的设置:我想每晚执行的所有阶段都是相似的,只是名称改变了。

stage1:
  stage: stage1
  only:
  - schedules

  script:
  - my execute script
  
  allow_failure: true
  
  artifacts:
   when: always
   paths:
    - stuff here
   reports:
     junit: filename here

我还有一个每晚执行的时间表,它会在晚上 18:00 触发执行。

然后我有我想在星期六上午 10 点每周执行一次的阶段:

stagex:
  stage: stagex
  only:
  - schedules
    - cron 0 10 * * 6

  script:
  - my execute script
  
  allow_failure: true
  
  artifacts:
   when: always
   paths:
    - stuff here
   reports:
     junit: filename here

上周六没有开火。UI 端没有单独的时间表。我应该在那里吗?有关如何使其工作的任何指示?

此外,我想要的是能够从一个按钮执行阶段 x(每周),因此需要时间表。如果 gitlab 通过其他方式启动它,它可以保持不活动状态。我尝试将变量设置为阶段并将其放在 UI 端,但我没有让它以这种方式工作。

肯定有一些我想念的东西......

标签: continuous-integrationgitlabgitlab-ci

解决方案


如果我理解你的目标,你想

  • 仅在计划时(每天)执行 stage1
  • 仅在计划(每周)或手动管道运行时执行 stagex。

以下应该做到这一点。您将需要设置每日和每周的时间表。对于每周计划,您需要将 SCHEDULED_JOB 定义为“stagex”;设置好时间表后,您可以按播放键手动运行作业。

variables:
  SCHEDULED_JOB: "stage1"

stage1:
  stage: stage1
  rules:
   - if: '$CI_PIPELINE_SOURCE == "schedule" && $SCHEDULED_JOB == $CI_JOB_NAME'

  script:
  - my execute script
  
  allow_failure: true
  
  artifacts:
   when: always
   paths:
    - stuff here
   reports:
     junit: filename here
     
    
stagex:
  stage: stagex
    - if: '$CI_PIPELINE_SOURCE == "schedule" && '$SCHEDULED_JOB == $CI_JOB_NAME'
  script:
  - my execute script
  
  allow_failure: true
  
  artifacts:
   when: always
   paths:
    - stuff here
   reports:
     junit: filename here

推荐阅读