-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
88 lines (81 loc) · 3.93 KB
/
helpers.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
const core = require('@actions/core');
const execute = require('./execute');
const prepare = require('./prepare');
function obtainLastPathSection(path) {
var pathArray = path.split("/");
var lastIndex = pathArray.length - 1;
return pathArray[lastIndex];
};
module.exports.obtainLastPathSection = obtainLastPathSection;
function lastFourLines(multiLineString) {
var multiLineStringArr = multiLineString.split("\n")
for(var cnt=0;cnt<multiLineStringArr.length;cnt++){ // indent
if (`${multiLineStringArr[cnt].trim()}` !== "") {
multiLineStringArr[cnt] = `STDOUT: ${multiLineStringArr[cnt]}`
}
}
var lastFourLines = multiLineStringArr.slice(multiLineStringArr.length - 4)
return `${lastFourLines.join("\n").trim()}`
}
module.exports.lastFourLines = lastFourLines;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports.sleep = sleep;
async function getVMLabel(ankaCustomVMLabel) {
if (process.env[`${process.env['GITHUB_ACTION']}_vmLabel`] !== undefined) { // return the existing VM's label
return process.env[`${process.env['GITHUB_ACTION']}_vmLabel`]
}
if (typeof(ankaCustomVMLabel) === "undefined" || ankaCustomVMLabel.length === 0) {
var vmLabel = `github-actions-${process.env['GITHUB_REPOSITORY']}-${process.env['GITHUB_RUN_NUMBER']}-${process.env['GITHUB_JOB']}-${process.env['GITHUB_ACTION']}`
} else {
// Needs a random number to prevent collision (we use GITHUB_RUN_NUMBER and GITHUB_ACTION for this if user doesn't specify a label); might be removed in later versions
var vmLabel = `${ankaCustomVMLabel}-${Math.floor(Math.random() * 9999) + 1}`
}
core.exportVariable(`${process.env['GITHUB_ACTION']}_vmLabel`, vmLabel);
return vmLabel
}
module.exports.getVMLabel = getVMLabel;
async function getArtifactArchiveName(artifactArchiveFileName) {
if (typeof(artifactArchiveFileName) === "undefined" || artifactArchiveFileName.length === 0) {
return `artifact`
} else {
return artifactArchiveFileName
}
}
module.exports.getArtifactArchiveName = getArtifactArchiveName;
async function mergeOptions(hostCommandOptions,options) {
if (options === undefined) {
var options = {}
}
// Make sure the host-command-options the user sends in are valid
if (typeof(hostCommandOptions) === 'object') { // When calling this function to add hostCommandOptions (like artifact uploading does), hostCommandOptions will become an object and need to be stringified again
hostCommandOptions = JSON.stringify(hostCommandOptions)
}
if (hostCommandOptions && typeof(hostCommandOptions) === 'string') {
try { // turn string into object
var userHostCommandOptions = JSON.parse(hostCommandOptions.replace(/(\w+:)|(\w+ :)/g, function(s) {
return '"' + s.substring(0, s.length-1) + '":';
}));
for (var key of Object.keys(userHostCommandOptions)) {
options[`${key}`] = userHostCommandOptions[key]
}
} catch(error) {
throw new Error(`host-command-options must be a js object (do not use single quotes to wrap values!) or JSON ({ "cwd": "./pathOnHostToRunAnkaCommands" }) inside of a string!\nSupported options: https://github.com/actions/toolkit/blob/master/packages/exec/src/interfaces.ts\n${error.stack}`);
}
}
return options
}
module.exports.mergeOptions = mergeOptions;
async function cleanup(ankaCustomVMLabel,hostCommandOptions,ankaVmTemplateName,lockFileLocation) {
try {
if (process.env[`${process.env['GITHUB_ACTION']}_cloneCreated`] === 'true') { // Prevent the delete if the VM was never created
await execute.hostCommands(`anka delete --yes ${await getVMLabel(ankaCustomVMLabel)}`,await mergeOptions(hostCommandOptions,{ silent: false }),execute.STD)
core.exportVariable(`${process.env['GITHUB_ACTION']}_cloneCreated`, false);
}
await prepare.deleteLockFile(ankaVmTemplateName,lockFileLocation)
} catch (error) {
throw new Error(`cleanup failed:\n${error.stack}`);
}
}
module.exports.cleanup = cleanup;