generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
138 lines (105 loc) · 3.67 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const core = require("@actions/core");
const SpeedCurve = require("speedcurve");
const log = require("speedcurve/dist/log");
async function run() {
if (core.isDebug()) {
log.setLevel("verbose");
}
core.debug(`Using speedcurve@${require("speedcurve/package.json").version}`);
const apiKey = core.getInput("api_key", { required: true });
const siteId = core.getInput("site_id");
const urlId = core.getInput("url_id");
const replaceOrigin = core.getInput("replace_origin");
const noteParam = core.getInput("note");
const detailParam = core.getInput("detail");
const workflowName = process.env["GITHUB_WORKFLOW"];
const runNumber = process.env["GITHUB_RUN_NUMBER"];
const repositoryName = process.env["GITHUB_REPOSITORY"];
const defaultNote = `Run #${runNumber} of workflow ${workflowName} in ${repositoryName}`;
const deployNote = noteParam || defaultNote;
const deployDetail = detailParam || "";
core.setSecret(apiKey);
let afterDeploy = async () => {};
if (replaceOrigin) {
let replaceOriginUrl;
try {
replaceOriginUrl = new URL(replaceOrigin);
} catch (err) {
core.setFailed(`replace_origin "${replaceOrigin}" could not be parsed`);
process.exit(1);
}
if (!siteId) {
core.setFailed("replace_origin can only be used when site_id is specified");
process.exit(1);
}
let site;
try {
site = await SpeedCurve.sites.get(apiKey, siteId);
} catch (err) {
core.debug(err.message);
core.setFailed(`Failed to fetch site ${siteId}`);
process.exit(1);
}
core.debug(`Updating URLs in site ${siteId} with origin ${replaceOrigin.origin}`);
await Promise.all(
site.urls.map((url) => {
const parsedUrl = new URL(url.url);
const newUrl = parsedUrl.href.replace(parsedUrl.origin, replaceOriginUrl.origin);
core.debug(`Updating URL ${url.urlId} to ${newUrl}`);
return SpeedCurve.urls.update(apiKey, url.urlId, {
url: newUrl,
});
})
);
// Restore the URLs to their original origin after the deploy
afterDeploy = async () => {
await Promise.all(
site.urls.map((url) => {
core.debug(`Restoring URL ${url.urlId} to ${url.url}`);
return SpeedCurve.urls.update(apiKey, url.urlId, {
url: url.url,
});
})
);
};
}
let deployResults;
if (urlId) {
core.info(`Creating deploy for URL ${urlId}`);
deployResults = SpeedCurve.deploys.createForUrls(apiKey, [urlId], {
note: deployNote,
detail: deployDetail,
force: true,
});
if (!deployResults.length || !deployResults[0].success) {
await afterDeploy();
core.setFailed(`Failed to trigger deploy for URL ${urlId}`);
if (deployResults.length && deployResults[0].error) {
core.error(deployResults[0].error);
}
process.exit(1);
}
} else if (siteId) {
core.info(`Creating deploy for site ${siteId}`);
deployResults = await SpeedCurve.deploys.create(apiKey, [siteId], {
note: deployNote,
detail: deployDetail,
force: true,
});
if (!deployResults.length || !deployResults[0].success) {
await afterDeploy();
core.setFailed(`Failed to trigger deploy for site ${siteId}`);
if (deployResults.length && deployResults[0].error) {
core.error(deployResults[0].error);
}
process.exit(1);
}
} else {
core.setFailed("Either a siteId or urlId must be provided");
process.exit(1);
}
await afterDeploy();
core.info(`Deploy ${deployResults[0].deployId} triggered ${deployResults[0].totalTests} tests`);
core.setOutput("deploy_id", deployResults[0].deployId);
}
run();