-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
95 lines (83 loc) · 3.31 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
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
const ioUtil = require('@actions/io/lib/io-util');
async function run() {
try {
const accessToken = core.getInput('access-token');
if (!accessToken) {
core.setFailed(
'No personal access token found. Please provide one by setting the `access-token` input for this action.',
);
return;
}
const sourceDirectory = core.getInput('source-directory');
if (!sourceDirectory) {
core.setFailed(
'No deploy directory specified. Please provide one by setting the `source-directory` input for this action.',
);
return;
}
const deployBranch = core.getInput('deploy-branch');
if (!deployBranch) deployBranch = 'gh-pages';
if (github.context.ref === `refs/heads/${deployBranch}`) {
console.log(`Triggered by branch used to deploy: ${github.context.ref}.`);
console.log('Nothing to deploy.');
return;
}
const autoInstallInput = core.getInput('auto-install');
const autoInstall = autoInstallInput === 'true' || autoInstallInput === 'yes';
if (autoInstall) {
const pkgManager = (await ioUtil.exists('./yarn.lock')) ? 'yarn' : 'npm';
console.log(`Installing your site's dependencies using ${pkgManager}.`);
await exec.exec(`${pkgManager} install`);
console.log('Finished installing dependencies.');
} else {
console.log('Skipping dependency installation (auto-install is disabled).');
}
const buildCommand = core.getInput('build-command');
if (!buildCommand) {
core.setFailed(
'No build command specified. Please provide one by setting the `build-command` input for this action.',
);
return;
}
console.log(`Building your site with command: ${buildCommand}`);
await exec.exec(buildCommand);
console.log('Finished building your site.');
const customDomain = core.getInput('custom-domain');
if (customDomain) {
exec.exec(`echo "${customDomain}" >> "./${sourceDirectory}/CNAME"`);
console.log('Wrote CNAME to deploy directory for custom domain.');
}
const repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
const repoURL = `https://${accessToken}@github.com/${repo}.git`;
console.log(`Deploying to repo: ${repo} and branch: ${deployBranch}`);
const sizeCallOptions = {};
let sizeOut = '';
sizeCallOptions.listeners = {
stdout: (data) => {
sizeOut += data.toString();
},
};
await exec.exec(`du`, ['-sh', sourceDirectory], sizeCallOptions);
console.log('deploy size:', sizeOut.split('\t')[0]);
const cwd = `./${sourceDirectory}`;
await exec.exec(`git init`, [], { cwd });
await exec.exec(`git config user.name`, [github.context.actor], { cwd });
await exec.exec(`git config user.email`, [`${github.context.actor}@users.noreply.github.com`], {
cwd,
});
await exec.exec(`git add`, ['.'], { cwd });
await exec.exec(
`git commit`,
['-m', `deployed via Github Pages Deploy Action 🚀 for ${github.context.sha}`],
{ cwd },
);
await exec.exec(`git push`, ['-f', repoURL, `HEAD:${deployBranch}`], { cwd });
console.log('Finished deploying your site.');
} catch (error) {
core.setFailed(error.message);
}
}
run();