This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
146 lines (118 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
139
140
141
142
143
144
145
146
const core = require("@actions/core");
const { execSync, exec } = require("child_process");
const fs = require("fs");
const path = require("path");
const { hasUncaughtExceptionCaptureCallback } = require("process");
var inVars = getInputVars();
catNetrcFile(inVars);
gitCredentials(inVars);
createProcfile(inVars);
loginHeroku();
selectHerokuApp(inVars);
addBuildpacks(inVars);
addConfigs(inVars);
addDynamicFiles(inVars);
deploy(inVars);
function gitCredentials({email}){
execComm(`git config --global user.name "Automated Heroku Deploy"`);
execComm(`git config --global user.email "${email}"`);
}
function deploy({useforce, branch,appdir}){
if (appdir === "") {
execComm(`git push heroku ${branch}:refs/heads/master ${useforce? "--force" : ""}`);
} else {
execComm(
`git push ${useforce? "--force" : ""} heroku \`git subtree split --prefix=${appdir} ${branch}\`:refs/heads/master`
);
}
}
function addBuildpacks({buildpacks}){
if(buildpacks){
execComm("heroku buildpacks:clear");
var buildpacks = JSON.parse(buildpacks);
// Add first buildpack
var buildpack = buildpacks.shift();
execComm(`heroku buildpacks:set ${buildpack}`);
// Add anothers buildpacks
buildpacks.forEach((bp) => {
execComm(`heroku buildpacks:add ${bp}`);
});
console.log("Added buildpacks");
}
}
function addDynamicFiles({ dynamicFiles }){
if(dynamicFiles){
execComm(`git add -A . && git commit -m "Added dynamic files"`);
console.log("Added dynamic files");
}
}
function addConfigs({ app_name, env_file, appdir }){
let configVars = [];
for (let key in process.env) {
if (key.startsWith("HD_")) {
configVars.push(key.substring(3) + "='" + process.env[key] + "'");
}
}
if (env_file) {
const env = fs.readFileSync(path.join(appdir, env_file), "utf8");
const variables = require("dotenv").parse(env);
const newVars = [];
for (let key in variables) {
newVars.push(key + "=" + variables[key]);
}
configVars = [...configVars, ...newVars];
}
if (configVars.length !== 0) {
execComm(`heroku config:set --app=${app_name} ${configVars.join(" ")}`);
}
}
function selectHerokuApp ({ app_name }) {
try {
execComm(`heroku git:remote --app ${app_name}`);
console.log("Added git remote heroku");
} catch (err) {
execComm(`heroku create ${app_name}`);
console.log("Successfully created a new heroku app");
}
}
function loginHeroku(){
execComm("heroku login");
console.log("Successfully logged into heroku");
}
function createProcfile({ procfile, appdir }){
if (procfile) {
fs.writeFileSync(path.join(appdir, "Procfile"), procfile);
execComm(`git add -A ${path.join(appdir, "Procfile")} && git commit -m 'Added Procfile'`);
console.log("Written Procfile with custom configuration");
}
}
function catNetrcFile({email, api_key}){
execComm(`cat >~/.netrc <<EOF
machine api.heroku.com
login ${email}
password ${api_key}
machine git.heroku.com
login ${email}
password ${api_key}
EOF`);
console.log("Created netrc file");
}
function getInputVars(){
let inputVars = {
api_key: core.getInput("heroku_api_key"),
email: core.getInput("heroku_email"),
app_name: core.getInput("heroku_app_name"),
buildpacks: core.getInput("buildpacks"),
branch: core.getInput("branch"),
useforce: core.getInput("useforce"),
appdir: core.getInput("appdir"),
procfile: core.getInput("procfile"),
env_file: core.getInput("env_file"),
dynamicFiles: core.getInput("dynamicFiles"),
};
console.log("Obtained input vars");
return inputVars;
}
function execComm(comm){
return execSync(comm, {stdio: 'inherit'});
}