-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (66 loc) · 2.83 KB
/
index.js
File metadata and controls
87 lines (66 loc) · 2.83 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
const core = require("@actions/core");
const github = require("@actions/github");
const exec = require("@actions/exec");
const fs = require("fs");
// eslint-disable-next-line no-unused-vars
function checkOrCreateFile(appPath, appToken, appID) {
try {
// If file dont exist, it will throw an error
fs.accessSync(`${appPath}/zcli.apps.config.json`, fs.constants.F_OK);
return true;
} catch (error) {
console.log(`File ${appPath}/zcli.apps.config.json does not exist. Trying to create it...`);
const params = JSON.stringify({
parameters: {
token: appToken,
},
app_id: appID,
});
fs.writeFileSync(`${appPath}/zcli.apps.config.json`, params);
console.log(`File ${appPath}/zcli.apps.config.json created.`);
try {
//Check if file was created
fs.accessSync(`${appPath}/zcli.apps.config.json`, fs.constants.F_OK);
console.log(`File ${appPath}/zcli.apps.config.json exists.`);
return true;
} catch (error) {
console.log(`File ${appPath}/zcli.apps.config.json does not exist.`);
return false;
}
}
}
async function run() {
try {
const dateTime = new Date().toLocaleString("pt-BR");
const { ref, eventName } = github.context;
const { repository } = github.context.payload;
const environment = core.getInput("ENVIRONMENT");
const appPath = core.getInput("PATH");
const appToken = core.getInput("TOKEN");
const appID = core.getInput("APP_ID");
if (environment !== "production" && environment !== "staging") {
throw new Error("Environment input must be provided (production or staging).");
}
await exec.exec(`echo 💡 This job started at ${dateTime} and will run on the path: ${appPath}`);
await exec.exec(`echo 🖥️ Job was automatically triggered by ${eventName} event`);
await exec.exec(`echo 🔎 The name of your branch is ${ref} and your repository is ${repository.name}.`);
await exec.exec(`echo 🐧 Setting up the environment...`);
await exec.exec("npm install yarn@1.22.19 --location=global");
await exec.exec("npm install @zendesk/zcli@v1.0.0-beta.32 --location=global --force");
await exec.exec("npm install typescript --location=global");
await exec.exec(`echo 🔎 Building & Validating...`);
await exec.exec("yarn install --frozen-lockfile");
await exec.exec(`yarn build`);
await exec.exec(`echo 🔎 Checking existence of zcli.apps.config.json file...`);
const fileExists = checkOrCreateFile(appPath, appToken, appID);
if (!fileExists) {
throw new Error("File zcli.apps.config.json not found and can't be created.");
}
await exec.exec(`echo 🚀 Updating an existing application...`);
await exec.exec(`zcli apps:update ${appPath}`);
exec.exec(`echo 🎉 Job has been finished`);
} catch (error) {
core.setFailed(error.message);
}
}
run();