Update deployment script to work on github.io repos as well (#256)

* Update deployment script to work on github.io repos as well

* remember how index of works

* CURRENT_BRANCH
This commit is contained in:
Ricky Vetter 2017-12-04 09:59:49 -08:00 committed by Joel Marcey
parent 76e423fba3
commit 0cead4b6f9

View file

@ -28,8 +28,9 @@ const PROJECT_NAME =
process.env.CIRCLE_PROJECT_REPONAME; process.env.CIRCLE_PROJECT_REPONAME;
const IS_PULL_REQUEST = const IS_PULL_REQUEST =
process.env.CI_PULL_REQUEST || process.env.CIRCLE_PULL_REQUEST; process.env.CI_PULL_REQUEST || process.env.CIRCLE_PULL_REQUEST;
const USE_SSH = process.env.USE_SSH; const USE_SSH = process.env.USE_SSH;
// github.io indicates organization repos that deploy via master. All others use gh-pages.
const DEPLOYMENT_BRANCH = PROJECT_NAME.indexOf(".github.io") !== -1 ? "master" : "gh-pages";
if (!ORGANIZATION_NAME) { if (!ORGANIZATION_NAME) {
shell.echo( shell.echo(
@ -52,14 +53,15 @@ if (USE_SSH === "true") {
remoteBranch = `https://${GIT_USER}@github.com/${ORGANIZATION_NAME}/${PROJECT_NAME}.git`; remoteBranch = `https://${GIT_USER}@github.com/${ORGANIZATION_NAME}/${PROJECT_NAME}.git`;
} }
// build static html files, then push to gh-pages branch of specified repo
if (IS_PULL_REQUEST) { if (IS_PULL_REQUEST) {
shell.echo("Skipping deploy on a pull request"); shell.echo("Skipping deploy on a pull request");
shell.exit(0); shell.exit(0);
} }
if (CURRENT_BRANCH === "gh-pages") { // build static html files, then push to DEPLOYMENT_BRANCH branch of specified repo
shell.echo("Cannot deploy from a gh-pages branch. Only to it");
if (CURRENT_BRANCH === DEPLOYMENT_BRANCH) {
shell.echo(`Cannot deploy from a ${DEPLOYMENT_BRANCH} branch. Only to it`);
shell.exit(1); shell.exit(1);
} }
@ -72,26 +74,27 @@ shell.cd(process.cwd());
shell.cd("build"); shell.cd("build");
if ( if (
shell.exec(`git clone ${remoteBranch} ${PROJECT_NAME}-gh-pages`).code !== 0 shell.exec(`git clone ${remoteBranch} ${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`)
.code !== 0
) { ) {
shell.echo("Error: git clone failed"); shell.echo("Error: git clone failed");
shell.exit(1); shell.exit(1);
} }
shell.cd(`${PROJECT_NAME}-gh-pages`); shell.cd(`${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`);
if (shell.exec("git checkout origin/gh-pages").code !== 0) { if (shell.exec(`git checkout origin/${DEPLOYMENT_BRANCH}`).code !== 0) {
if (shell.exec("git checkout --orphan gh-pages").code !== 0) { if (shell.exec(`git checkout --orphan ${DEPLOYMENT_BRANCH}`).code !== 0) {
shell.echo("Error: Git checkout gh-pages failed"); shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`);
shell.exit(1); shell.exit(1);
} }
} else { } else {
if ( if (
shell.exec("git checkout -b gh-pages").code + shell.exec(`git checkout -b ${DEPLOYMENT_BRANCH}`).code +
shell.exec("git branch --set-upstream-to=origin/gh-pages").code !== shell.exec(`git branch --set-upstream-to=origin/${DEPLOYMENT_BRANCH}`).code !==
0 0
) { ) {
shell.echo("Error: Git checkout gh-pages failed"); shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`);
shell.exit(1); shell.exit(1);
} }
} }
@ -100,16 +103,21 @@ shell.exec("git rm -rf .");
shell.cd("../.."); shell.cd("../..");
shell.cp("-R", `build/${PROJECT_NAME}/*`, `build/${PROJECT_NAME}-gh-pages/`); shell.cp(
shell.cd(`build/${PROJECT_NAME}-gh-pages`); "-R",
`build/${PROJECT_NAME}/*`,
`build/${PROJECT_NAME}-${DEPLOYMENT_BRANCH}/`
);
shell.cd(`build/${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`);
const currentCommit = shell.exec("git rev-parse HEAD").stdout.trim(); const currentCommit = shell.exec("git rev-parse HEAD").stdout.trim();
shell.exec("git add --all"); shell.exec("git add --all");
shell.exec( shell.exec(
`git commit -m "Deploy website" -m "Deploy website version based on ${currentCommit}"` `git commit -m "Deploy website" -m "Deploy website version based on ${currentCommit}"`
); );
if (shell.exec("git push origin gh-pages").code !== 0) { if (shell.exec(`git push origin ${DEPLOYMENT_BRANCH}`).code !== 0) {
shell.echo("Error: Git push failed"); shell.echo("Error: Git push failed");
shell.exit(1); shell.exit(1);
} else { } else {