-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
94 lines (79 loc) · 3.35 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
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
const core = require('@actions/core');
const github = require('@actions/github');
const { BlobServiceClient } = require("@azure/storage-blob");
const { DefaultAzureCredential } = require('@azure/identity');
const { v4: uuidv4 } = require('uuid');
async function runWorkflowAsync() {
const accountName = core.getInput('azure-storage-account-name');
if (!accountName) {
core.setFailed('Azure Storage accountName not found');
return;
}
const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
new DefaultAzureCredential()
);
const containerName = core.getInput('azure-storage-container-name');
const containerClient = blobServiceClient.getContainerClient(containerName);
const clientWorkflowId = uuidv4();
const blobName = `new/${clientWorkflowId}.json`;
const failedBlobPrefix = `failed/${clientWorkflowId}`;
const succeededBlobPrefix = `succeeded/${clientWorkflowId}`;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const data = {
"WorkflowUrl": core.getInput("workflow-path"),
"WorkflowInputsUrl": core.getInput("workflow-inputs-path"),
"WorkflowOptionsUrl": core.getInput("workflow-options-path"),
"WorkflowDependenciesUrl": core.getInput("workflow-dependencies-path")
};
const jsonData = JSON.stringify(data);
const startTime = Date.now();
try {
await blockBlobClient.upload(jsonData, jsonData.length);
console.log(`Trigger file created: ${blockBlobClient.url}`);
} catch (error) {
core.setFailed(`Error uploading blob: ${error.message}`);
return;
}
let isDone = false;
// Loop until the workflow is done
while (!isDone) {
try {
for await (const {} of containerClient.listBlobsFlat({ prefix: succeededBlobPrefix })) {
console.log('Workflow succeeded.');
core.setOutput("status", "succeeded");
isDone = true
break;
}
if (isDone) break;
for await (const {} of containerClient.listBlobsFlat({ prefix: failedBlobPrefix })) {
core.setFailed('Workflow failed.');
isDone = true;
break;
}
if (isDone) break;
} catch (error) {
console.log(`Error occurred; will retry in 30s. ${error.message}`);
}
await new Promise(resolve => setTimeout(resolve, 30000)); // wait 30 seconds before checking again
}
const endTime = Date.now();
const duration = (endTime - startTime) / 1000; // Duration in seconds
const hours = Math.floor(duration / 3600); // Calculate hours
const minutes = Math.floor((duration % 3600) / 60); // Calculate minutes
const seconds = Math.floor(duration % 60); // Calculate seconds
console.log(`Workflow completed in ${hours}h ${minutes}m ${seconds}s`);
}
async function run() {
try {
await runWorkflowAsync();
const time = (new Date()).toTimeString();
core.setOutput("time", time);
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2);
console.log(`The event payload: ${payload}`);
} catch (error) {
core.setFailed(error.message);
}
}
run();