-
Notifications
You must be signed in to change notification settings - Fork 299
79 lines (69 loc) · 3.04 KB
/
00_cron.yaml
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
name: Build LMS Scheduler
on:
workflow_dispatch:
schedule:
- cron: '40 2 * * *'
jobs:
check:
name: Check whether build is needed
runs-on: ubuntu-22.04
steps:
- uses: actions/github-script@v7
with:
script: |
const repoStatus = await github.request('GET https://lms-community.github.io/lms-server-repository/servers.json');
if (repoStatus.status !== 200) {
return false;
}
// get the oldest timestamp for each version from previous builds
const candidates = [];
Object.keys(repoStatus.data).forEach(version => {
const matches = version.match(/(\d+\.\d+)\.\d+/);
if (matches && matches.length == 2) {
const versionBuilds = repoStatus.data[version];
candidates.push({
v: matches[1],
r: Object.keys(versionBuilds).reduce((accumulator, build) => {
const buildInfo = versionBuilds[build];
accumulator = accumulator || parseInt(buildInfo.revision);
return Math.min(accumulator, parseInt(buildInfo.revision));
}, 0)
})
}
else {
delete repoStatus.data[version];
}
});
// for each version see whether there's a more recent commit than the revision of the previous build
for (let i = 0; i < candidates.length; i++) {
const latestBuildTimestamp = candidates[i].r * 1000;
const commitStatus = await github.rest.repos.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
sha: 'public/' + candidates[i].v,
per_page: 1,
sort: 'created',
order: 'asc',
});
if (commitStatus.status !== 200) {
console.log(JSON.stringify(commitStatus, null, 2));
continue;
}
// see whether there's really been a commit since that timestamp - above "since" would be inclusive
const needsBuild = commitStatus.data.find(commit => new Date(commit.commit.committer.date).getTime() > latestBuildTimestamp);
if (needsBuild) {
console.log(`${candidates[i].v}: needs a build (${new Date(needsBuild.commit.committer.date).getTime()} > ${latestBuildTimestamp})`);
const workflowStatus = await github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: '00_build.yaml',
ref: 'public/' + candidates[i].v,
});
if (workflowStatus.status < 200 || workflowStatus.status > 204) {
console.log(workflowStatus);
}
}
else {
console.log(`${candidates[i].v}: is up to date (${candidates[i].r})`);
}
}