如何在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
- Go to your project in GitLab → CI/CD → Schedules.
- 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.
- Key:
- Save the schedule.
- 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
- Key:
- Save the schedule.
Quick Notes:
- If you need fresh build artifacts for your daily test run, remove the
neverrule forbuild_jobwhenSCHEDULE_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
相关产品推荐
相关产品推荐

