-
Notifications
You must be signed in to change notification settings - Fork 0
/
staggerWorkflowTimes.php
32 lines (27 loc) · 1.09 KB
/
staggerWorkflowTimes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
$workflowDirectory = __DIR__ . '/.github/workflows';
const WORKFLOW_STAGGER_OFFSET_MINUTES = 5;
$workflowDay = 4;
$workflowHour = 0;
$workflowMinute = 0;
$workflowFiles = scandir($workflowDirectory);
$workflowFiles = array_filter($workflowFiles, fn ($file) => str_ends_with($file, '.yml'));
foreach ($workflowFiles as $workflowFile) {
$workflowMinute += WORKFLOW_STAGGER_OFFSET_MINUTES;
if ($workflowMinute >= 60) {
$workflowMinute = 0;
$workflowHour++;
}
if ($workflowHour >= 24) {
$workflowHour = 0;
$workflowDay++;
}
if ($workflowDay >= 6) {
$workflowDay = 0;
}
$workflowContents = file_get_contents($workflowDirectory . '/' . $workflowFile);
$workflowContents = preg_replace('/- cron: "\d+ \d+ \* \* \d+"/', '- cron: "' . $workflowMinute . ' ' . $workflowHour . ' * * ' . $workflowDay . '"', $workflowContents, 1, $count); if ($count !== 1) {
throw new \RuntimeException('Failed to replace cron in ' . $workflowFile);
}
file_put_contents($workflowDirectory . '/' . $workflowFile, $workflowContents);
}