generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
67 lines (54 loc) · 2.01 KB
/
index.js
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
const core = require('@actions/core');
const httpClient = require('@actions/http-client');
async function checkRunner({ token, owner, repo, primaryRunnerLabels, fallbackRunner }) {
const http = new httpClient.HttpClient('http-client');
const headers = {
'Authorization': `Bearer ${token}`,
};
const response = await http.getJson(`https://api.github.com/repos/${owner}/${repo}/actions/runners`, headers);
if (response.statusCode !== 200) {
return { error: `Failed to get runners. Status code: ${response.statusCode}` };
}
const runners = response.result.runners || [];
let useRunner = fallbackRunner;
let primaryIsOnline = false;
for (const runner of runners) {
if (runner.status === 'online') {
const runnerLabels = runner.labels.map(label => label.name);
if (primaryRunnerLabels.every(label => runnerLabels.includes(label))) {
primaryIsOnline = true;
useRunner = primaryRunnerLabels.join(',');
break;
}
}
}
// return a JSON string so that it can be parsed using `fromJson`, e.g. fromJson('["self-hosted", "linux"]')
return { useRunner: JSON.stringify(useRunner.split(',')), primaryIsOnline };
}
async function main() {
const githubRepository = process.env.GITHUB_REPOSITORY;
const [owner, repo] = githubRepository.split("/");
try {
const inputs = {
owner,
repo,
token: core.getInput('github-token', { required: true }),
primaryRunnerLabels: core.getInput('primary-runner', { required: true }).split(','),
fallbackRunner: core.getInput('fallback-runner', { required: true }),
};
const { useRunner, primaryIsOnline, error } = await checkRunner(inputs);
if (error) {
core.setFailed(error);
return;
}
core.info(`Primary runner is online: ${primaryIsOnline}`);
core.info(`Using runner: ${useRunner}`);
core.setOutput('use-runner', useRunner);
} catch (error) {
core.setFailed(error.message);
}
}
module.exports = { checkRunner };
if (require.main === module) {
main();
}