mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-02 02:42:41 +02:00
Update docusaurus-publish (#250)
* Update publish script to allow configuration via siteConfig.js file, as well as non-CircleCI env vars * Use || instead of |
This commit is contained in:
parent
bb48bb4f0c
commit
31b70f053c
3 changed files with 54 additions and 32 deletions
|
@ -9,34 +9,56 @@
|
|||
|
||||
const shell = require("shelljs");
|
||||
|
||||
const GIT_USER = process.env.GIT_USER;
|
||||
const CIRCLE_BRANCH = process.env.CIRCLE_BRANCH;
|
||||
const CIRCLE_PROJECT_USERNAME = process.env.CIRCLE_PROJECT_USERNAME;
|
||||
const CIRCLE_PROJECT_REPONAME = process.env.CIRCLE_PROJECT_REPONAME;
|
||||
const CI_PULL_REQUEST = process.env.CI_PULL_REQUEST;
|
||||
|
||||
const USE_SSH = process.env.USE_SSH;
|
||||
|
||||
let remoteBranch;
|
||||
if (USE_SSH === "true") {
|
||||
remoteBranch = `git@github.com:${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}.git`;
|
||||
} else {
|
||||
remoteBranch = `https://${GIT_USER}@github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}.git`;
|
||||
}
|
||||
|
||||
// build static html files, then push to gh-pages branch of specified repo
|
||||
|
||||
if (!shell.which("git")) {
|
||||
shell.echo("Sorry, this script requires git");
|
||||
shell.exit(1);
|
||||
}
|
||||
|
||||
if (CI_PULL_REQUEST) {
|
||||
const siteConfig = require(process.cwd() + "/siteConfig.js");
|
||||
const GIT_USER = process.env.GIT_USER;
|
||||
const CURRENT_BRANCH =
|
||||
process.env.CIRCLE_BRANCH || shell.exec("git rev-parse --abbrev-ref HEAD");
|
||||
const ORGANIZATION_NAME =
|
||||
siteConfig.organizationName ||
|
||||
process.env.ORGANIZATION_NAME ||
|
||||
process.env.CIRCLE_PROJECT_USERNAME;
|
||||
const PROJECT_NAME =
|
||||
siteConfig.projectName ||
|
||||
process.env.PROJECT_NAME ||
|
||||
process.env.CIRCLE_PROJECT_REPONAME;
|
||||
const IS_PULL_REQUEST =
|
||||
process.env.CI_PULL_REQUEST || process.env.CIRCLE_PULL_REQUEST;
|
||||
|
||||
const USE_SSH = process.env.USE_SSH;
|
||||
|
||||
if (!ORGANIZATION_NAME) {
|
||||
shell.echo(
|
||||
"Missing project organization name. Did you forget to define 'organizationName' in siteConfig.js? You may also export it via the ORGANIZATION_NAME environment variable."
|
||||
);
|
||||
shell.exit(0);
|
||||
}
|
||||
|
||||
if (!PROJECT_NAME) {
|
||||
shell.echo(
|
||||
"Missing project name. Did you forget to define 'projectName' in siteConfig.js? You may also export it via the PROJECT_NAME environment variable."
|
||||
);
|
||||
shell.exit(0);
|
||||
}
|
||||
|
||||
let remoteBranch;
|
||||
if (USE_SSH === "true") {
|
||||
remoteBranch = `git@github.com:${ORGANIZATION_NAME}/${PROJECT_NAME}.git`;
|
||||
} else {
|
||||
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) {
|
||||
shell.echo("Skipping deploy on a pull request");
|
||||
shell.exit(0);
|
||||
}
|
||||
|
||||
if (CIRCLE_BRANCH === "gh-pages") {
|
||||
if (CURRENT_BRANCH === "gh-pages") {
|
||||
shell.echo("Cannot deploy from a gh-pages branch. Only to it");
|
||||
shell.exit(1);
|
||||
}
|
||||
|
@ -50,14 +72,13 @@ shell.cd(process.cwd());
|
|||
shell.cd("build");
|
||||
|
||||
if (
|
||||
shell.exec(`git clone ${remoteBranch} ${CIRCLE_PROJECT_REPONAME}-gh-pages`)
|
||||
.code !== 0
|
||||
shell.exec(`git clone ${remoteBranch} ${PROJECT_NAME}-gh-pages`).code !== 0
|
||||
) {
|
||||
shell.echo("Error: git clone failed");
|
||||
shell.exit(1);
|
||||
}
|
||||
|
||||
shell.cd(`${CIRCLE_PROJECT_REPONAME}-gh-pages`);
|
||||
shell.cd(`${PROJECT_NAME}-gh-pages`);
|
||||
|
||||
if (shell.exec("git checkout origin/gh-pages").code !== 0) {
|
||||
if (shell.exec("git checkout --orphan gh-pages").code !== 0) {
|
||||
|
@ -79,23 +100,21 @@ shell.exec("git rm -rf .");
|
|||
|
||||
shell.cd("../..");
|
||||
|
||||
shell.cp(
|
||||
"-R",
|
||||
`build/${CIRCLE_PROJECT_REPONAME}/*`,
|
||||
`build/${CIRCLE_PROJECT_REPONAME}-gh-pages/`
|
||||
);
|
||||
shell.cd(`build/${CIRCLE_PROJECT_REPONAME}-gh-pages`);
|
||||
shell.cp("-R", `build/${PROJECT_NAME}/*`, `build/${PROJECT_NAME}-gh-pages/`);
|
||||
shell.cd(`build/${PROJECT_NAME}-gh-pages`);
|
||||
|
||||
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 commit -m "Deploy website" -m "Deploy website version based on ${currentCommit}"`);
|
||||
shell.exec(
|
||||
`git commit -m "Deploy website" -m "Deploy website version based on ${currentCommit}"`
|
||||
);
|
||||
if (shell.exec("git push origin gh-pages").code !== 0) {
|
||||
shell.echo("Error: Git push failed");
|
||||
shell.exit(1);
|
||||
} else {
|
||||
shell.echo(
|
||||
`Website is live at: https://${CIRCLE_PROJECT_USERNAME}.github.io/${CIRCLE_PROJECT_REPONAME}/`
|
||||
`Website is live at: https://${ORGANIZATION_NAME}.github.io/${PROJECT_NAME}/`
|
||||
);
|
||||
shell.exit(0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue