-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·161 lines (139 loc) · 5.44 KB
/
script.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
import {
execSpawn,
existFolder,
makeOutput,
parseJsonFile,
readFile,
removeCharactersAndSpaces,
writeFileJSON,
} from "./src/util/common.js";
import { changeColor, messages } from "./src/util/message-log.js";
import { publishOrUpdate } from "./src/publish/index.js";
/**
* environments
*/
// INPUT
const {
INPUT_APPLICATIONNAME,
INPUT_AZIONPERSONALTOKEN,
INPUT_FUNCTIONARGSFILEPATH,
INPUT_BUILDPRESET,
INPUT_BUILDMODE,
INPUT_BUILDENTRY,
INPUT_BUILDSTATICFOLDER,
INPUT_EDGEMODULEACCELERATION,
INPUT_SCRIPTENV,
} = process.env;
// ENV GITHUB
const { GITHUB_WORKSPACE, GITHUB_REPOSITORY } = process.env;
/**
* constants
*/
const BASE_URL_AZION_API = INPUT_SCRIPTENV === "stage" ? "stage-api-origin.azion.net" : "api-origin.azionapi.net";
const VULCAN_COMMAND = "npx --yes [email protected]";
const FUNCTION_WORK_PATH_DEFAULT = ".edge/worker.js";
const BUILD_ENTRY_DEFAULT = (INPUT_BUILDPRESET?.toLocaleLowerCase() === "typescript") ? "./main.ts" : "./main.js";
const STORAGE_PATH_DEFAULT = ".edge/storage";
const VERSION_ENV_PATH_DEFAULT = ".edge/.env";
/**
* main function where you run the script
* @returns {void}
*/
const main = async () => {
// create initial log
messages.title(changeColor("red", "JAMStack Azion Deployment"));
messages.textOnly("Build and Deploy applications on the Edge with Azion");
messages.textOnly(`Preset · ${INPUT_BUILDPRESET}`);
messages.textOnly(`Environment · ${INPUT_SCRIPTENV}`);
// check script env
if(!["production", "stage"].includes(INPUT_SCRIPTENV)){
throw new Error('Invalid environment. Please set ENV to either production or stage.')
}
let APPLICATION_NAME_VALID = removeCharactersAndSpaces(INPUT_APPLICATIONNAME || "");
if (!INPUT_APPLICATIONNAME) {
const [_, REPO_NAME] = GITHUB_REPOSITORY?.split("/");
APPLICATION_NAME_VALID = REPO_NAME;
}
// init repo or load repo
messages.init.title("INIT SCRIPT");
messages.init.await("initialize repository");
const azionConfigPath = `azion/azion.json`;
const sourceCodePath = GITHUB_WORKSPACE;
messages.init.complete("initialize repository");
// // install libs if not exist
await existFolder(`${sourceCodePath}/node_modules`).catch(async (err) => {
messages.prebuild.title("INSTALL DEPENDENCIES");
messages.prebuild.await("This process may take a few minutes!");
await execSpawn(sourceCodePath, "yarn");
messages.prebuild.complete("install dependencies");
});
// // build code by vulcan preset
messages.build.title("BUILD CODE BY VULCAN");
messages.build.await(`Running ${VULCAN_COMMAND}`);
messages.build.await("This process may take a few minutes!");
const BUILD_MODE_VALID = INPUT_BUILDMODE || "deliver";
let buildCmd = `${VULCAN_COMMAND} build --preset ${INPUT_BUILDPRESET} --mode ${BUILD_MODE_VALID}`;
if (BUILD_MODE_VALID === "compute") {
const entry = `${INPUT_BUILDENTRY || BUILD_ENTRY_DEFAULT}`;
buildCmd = `${VULCAN_COMMAND} build --preset ${INPUT_BUILDPRESET} --mode ${BUILD_MODE_VALID} --entry ${entry}`;
}
await execSpawn(sourceCodePath, buildCmd);
messages.build.complete("building code");
// publish
messages.deploy.title("DEPLOY ON EDGE");
const workerFunctionPath = `${sourceCodePath}/${FUNCTION_WORK_PATH_DEFAULT}`;
const workerArgsPath = `${INPUT_FUNCTIONARGSFILEPATH}`;
const versionBuildPath = `${sourceCodePath}/${VERSION_ENV_PATH_DEFAULT}`;
// create args to function
const ARGS_FUNCTION = await readFile(`${sourceCodePath}/${workerArgsPath}`).catch((err) => messages.prebuild.info("Fail load args file"));
const ARGS_FUNCTION_VALID = ARGS_FUNCTION || "{}";
await writeFileJSON(`${sourceCodePath}/${workerArgsPath}`, parseJsonFile(ARGS_FUNCTION_VALID));
// enable modules
const EDGE_MODULE_ACCELERATION_VALID = !!INPUT_EDGEMODULEACCELERATION;
// publish or update
const staticFolder = INPUT_BUILDSTATICFOLDER ? `${sourceCodePath}/${INPUT_BUILDSTATICFOLDER}` : `${sourceCodePath}/${STORAGE_PATH_DEFAULT}`;
const inputSourceCode = {
path: sourceCodePath,
configPath: azionConfigPath,
functionPath: workerFunctionPath,
functionArgsPath: workerArgsPath,
versionBuildPath: versionBuildPath,
info: { application: { name: APPLICATION_NAME_VALID } },
buildPreset: INPUT_BUILDPRESET,
buildMode: INPUT_BUILDMODE,
buildStaticFolder: staticFolder,
};
const resultPublish = await publishOrUpdate(
BASE_URL_AZION_API,
INPUT_AZIONPERSONALTOKEN,
{ acceleration: EDGE_MODULE_ACCELERATION_VALID },
inputSourceCode,
VULCAN_COMMAND
);
messages.deploy.complete("deploy");
messages.deploy.deployed("Edge Application");
messages.title("DEPLOY INFO");
messages.textOnly(`Name: ${resultPublish?.application?.name}`);
messages.textOnly(`Domain: https://${resultPublish?.domain?.url}`);
messages.textOnly(`Domain ID: ${resultPublish?.domain?.id}`);
messages.textOnly(`Edge Application ID: ${resultPublish?.application?.id}`);
messages.textOnly(`Function ID: ${resultPublish?.function?.id}`);
if (resultPublish?.["version-id"]) {
messages.textOnly(`Version ID: ${resultPublish?.["version-id"]}`);
}
// SET OUTPUT
await makeOutput(GITHUB_WORKSPACE, "applicationId", resultPublish?.application?.id);
await makeOutput(GITHUB_WORKSPACE, "domainUrl", resultPublish?.domain?.url);
};
/**
* execute and catch error
*/
main()
.catch((err) => {
messages.error(changeColor("red", err?.message));
process.exit(1);
})
.finally(async () => {
messages.complete("finally script");
});