-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.js
More file actions
110 lines (92 loc) · 3.37 KB
/
execute.js
File metadata and controls
110 lines (92 loc) · 3.37 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const core = require('@actions/core');
const github = require('@actions/github');
const {DefaultArtifactClient} = require('@actions/artifact');
async function waitForSeconds(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
}
async function waitForWorkflowRun(octokit, owner, repo, run_id) {
for (;;) {
const run = await octokit.rest.actions.getWorkflowRun({
owner: owner,
repo: repo,
run_id: run_id,
});
if (run.data.status === "completed") break;
console.log(`Waiting for https://github.com/${owner}/${repo}/actions/runs/${run_id}`);
await waitForSeconds(10);
}
}
async function waitForWorkflow(octokit, owner, repo, workflow, branch, event, date) {
for (;;) {
const runs = await octokit.rest.actions.listWorkflowRuns({
owner: owner,
repo: repo,
workflow_id: workflow,
branch: branch,
event: event,
created: `>=${date}`,
});
if (runs.data.total_count) return runs.data.workflow_runs[0].id;
console.log(`Waiting for https://github.com/${owner}/${repo}/actions`);
await waitForSeconds(5);
}
}
async function run() {
try {
const token = core.getInput('token', {required: false});
const download = core.getInput('download', {required: false});
let run_ids = core.getInput('run_ids', {required: false});
const octokit = github.getOctokit(token);
if (run_ids === "{}") {
const date = new Date().toISOString();
const repository = core.getInput('repository', {required: false});
const workflow = core.getInput('workflow', {required: false});
const branch = core.getInput('branch', {required: false});
const inputs = JSON.parse(core.getInput('inputs', {required: false}));
const wait = core.getInput('wait', {required: false});
const [owner, repo] = repository.split('/');
const ret = await octokit.rest.actions.createWorkflowDispatch({
owner: owner,
repo: repo,
workflow_id: workflow,
ref: branch,
inputs: inputs,
});
const run_id = await waitForWorkflow(octokit, owner, repo, workflow, branch, "workflow_dispatch", date);
console.log(`Dispatched https://github.com/${repository}/actions/runs/${run_id}`);
core.setOutput("run_id", run_id);
if (wait === "false") return;
run_ids = {repository: run_id};
} else {
run_ids = JSON.parse(run_ids);
}
const artifactClient = new DefaultArtifactClient();
for (const [repository, run_id] of Object.entries(run_ids)) {
const [owner, repo] = repository.split('/');
await waitForWorkflowRun(octokit, owner, repo, run_id);
if (download === "false") continue;
const artifacts = await octokit.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id,
});
console.log(`Downloading ${artifacts.data.total_count} artifacts`);
for (let i = 0; i < artifacts.data.total_count; i++) {
const artifact = artifacts.data.artifacts[i];
await artifactClient.downloadArtifact(artifact.id, {
path: artifact.name,
findBy: {
token: token,
repositoryOwner: owner,
repositoryName: repo,
workflowRunId: run_id,
}
});
}
}
core.setOutput("run_ids", JSON.stringify(run_ids));
} catch (error) {
core.setFailed(error.message);
}
}
run();