You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GitLab CI/CD中按不同时间调度构建与测试任务

Solution

To set up your daily scheduled runs for build and test jobs separately, we'll use schedule-specific variables in GitLab CI/CD to control which job runs for each schedule. Here's how to modify your YAML and configure the schedules:

Modified GitLab CI YAML File

variables:
  MSBUILD_PATH: 'C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe'
  SOLUTION_PATH: 'Source/NewProject.sln'
stages:
  - build
  - test

build_job:
  stage: build
  script:
    - '& "$env:MSBUILD_PATH" "$env:SOLUTION_PATH" /nologo /t:Rebuild /p:Configuration=Debug'
    - pwd
  artifacts:
    paths:
      - 'Output'
  rules:
    # Run on daily build schedule (SCHEDULE_TYPE=build)
    - if: '$SCHEDULE_TYPE == "build"'
      when: always
    # Run on normal pipelines (pushes, merge requests, etc.)
    - if: '$SCHEDULE_TYPE == null'
      when: always
    # Skip on test schedule to save resources
    - if: '$SCHEDULE_TYPE == "test"'
      when: never

test_job:
  stage: test
  script:
    - 'Output\bin\Debug\NewProject.exe'
  rules:
    # Only run on daily test schedule (SCHEDULE_TYPE=test)
    - if: '$SCHEDULE_TYPE == "test"'
      when: always
    # Skip all other pipeline triggers
    - when: never

How to Configure the Schedules in GitLab UI

  1. Go to your project in GitLab → CI/CD → Schedules.
  2. Create the daily build schedule (16:00):
    • Click New schedule.
    • Set the Cron time to 0 16 * * * (this runs daily at 16:00 UTC; adjust your project's timezone in settings if needed).
    • Under Variables, add a new entry:
      • Key: SCHEDULE_TYPE
      • Value: build
      • Leave Protected and Masked unchecked unless you have specific security needs.
    • Save the schedule.
  3. Create the daily test schedule (4:00):
    • Click New schedule again.
    • Set the Cron time to 0 4 * * *.
    • Under Variables, add:
      • Key: SCHEDULE_TYPE
      • Value: test
    • Save the schedule.

Quick Notes:

  • If you need fresh build artifacts for your daily test run, remove the never rule for build_job when SCHEDULE_TYPE == "test"—this will run the build first before testing.
  • GitLab uses UTC by default for cron schedules. You can change your project's timezone in Settings → General → Timezone to match your local time.

内容的提问来源于stack exchange,提问作者codeBoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:17:53