forked from enriikke/gatsby-gh-pages-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
106 lines (85 loc) · 4.08 KB
/
index.ts
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
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as github from "@actions/github";
import * as io from "@actions/io";
import * as ioUtil from "@actions/io/lib/io-util";
const DEFAULT_DEPLOY_BRANCH = "master";
async function run(): Promise<void> {
try {
const skipPublish = (core.getInput("skip-publish") || "false").toUpperCase();
const accessToken = core.getInput("access-token");
if (!accessToken && skipPublish !== "TRUE") {
return core.setFailed(
"No personal access token found. Please provide one by setting the `access-token` input for this action, or disable publishing by setting `skip-publish`.",
);
}
const deployBranch = core.getInput("deploy-branch") || DEFAULT_DEPLOY_BRANCH;
const deployRepo = core.getInput("deploy-repo");
const isSameRepo = !deployRepo || deployRepo === github.context.repo.repo;
if (isSameRepo && github.context.ref === `refs/heads/${deployBranch}`) {
console.log(`Triggered by branch used to deploy: ${github.context.ref}.`);
console.log("Nothing to deploy.");
return;
}
const workingDir = core.getInput("working-dir") || ".";
const pkgManager = (await ioUtil.exists(`${workingDir}/yarn.lock`)) ? "yarn" : "npm";
console.log(`Installing your site's dependencies using ${pkgManager}.`);
await exec.exec(`${pkgManager} install`, [], {cwd: workingDir});
console.log("Finished installing dependencies.");
let gatsbyArgs = core.getInput("gatsby-args").split(/\s+/).filter(Boolean);
if (gatsbyArgs.length > 0) {
gatsbyArgs = ["--", ...gatsbyArgs];
}
console.log("Ready to build your Gatsby site!");
console.log(`Building with: ${pkgManager} run build ${gatsbyArgs.join(" ")}`);
await exec.exec(`${pkgManager} run build`, gatsbyArgs, {cwd: workingDir});
console.log("Finished building your site.");
const cnameExists = await ioUtil.exists(`${workingDir}/CNAME`);
if (cnameExists) {
console.log("Copying CNAME over.");
await io.cp(`${workingDir}/CNAME`, `${workingDir}/public/CNAME`, {force: true});
console.log("Finished copying CNAME.");
}
const noJekyllExists = await ioUtil.exists(`${workingDir}/.nojekyll`);
if (noJekyllExists) {
console.log("Copying .nojekyll over.");
await io.cp(`${workingDir}/.nojekyll`, `${workingDir}/public/.nojekyll`, {force: true});
console.log("Finished copying .nojekyll.");
}
if (skipPublish === "TRUE") {
return console.log("Building completed successfully - skipping publish");
}
const repo = `${github.context.repo.owner}/${deployRepo || github.context.repo.repo}`;
const repoURL = `https://${accessToken}@github.com/${repo}.git`;
console.log("Ready to deploy your new shiny site!");
console.log(`Deploying to repo: ${repo} and branch: ${deployBranch}`);
console.log("You can configure the deploy branch by setting the `deploy-branch` input for this action.");
await exec.exec(`git init`, [], {cwd: `${workingDir}/public`});
const gitUserName = core.getInput("git-config-name") || github.context.actor;
const gitEmail = core.getInput("git-config-email") || `${github.context.actor}@users.noreply.github.com`;
await exec.exec(`git config user.name`, [gitUserName], {
cwd: `${workingDir}/public`,
});
await exec.exec(`git config user.email`, [gitEmail], {
cwd: `${workingDir}/public`,
});
await exec.exec(`git add`, ["."], {cwd: `${workingDir}/public`});
const commitMessageInput =
core.getInput("commit-message") || `deployed via Gatsby Publish Action 🎩 for ${github.context.sha}`;
await exec.exec(`git commit`, ["-m", commitMessageInput], {
cwd: `${workingDir}/public`,
});
await exec.exec(`git push`, ["-f", repoURL, `master:${deployBranch}`], {
cwd: `${workingDir}/public`,
});
console.log("Finished deploying your site.");
console.log("Enjoy! ✨");
} catch (err: any) {
core.setFailed(err.message);
}
}
// Don't auto-execute in the test environment
if (process.env["NODE_ENV"] !== "test") {
run();
}
export default run;