From fd9a3ffb6deca84b41dc5a3b43b0db1ca4f3143e Mon Sep 17 00:00:00 2001 From: Gustavo Henke Date: Fri, 15 Jun 2018 09:03:37 +1000 Subject: [PATCH] Cross repo publishing (#764) --- docs/getting-started-publishing.md | 3 ++- lib/publish-gh-pages.js | 43 +++++++++++++++++++----------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/getting-started-publishing.md b/docs/getting-started-publishing.md index 53d0420147..e6eceb54c6 100644 --- a/docs/getting-started-publishing.md +++ b/docs/getting-started-publishing.md @@ -45,7 +45,8 @@ Two of the required parameters are set in the [`siteConfig.js`](api-site-config. | `organizationName` | The GitHub user or organization that owns the repository. In the case of Docusaurus, that would be the "facebook" GitHub organization. | | `projectName` | The name of the GitHub repository for your project. For example, Docusaurus is hosted at https://github.com/facebook/docusaurus, so our project name in this case would be "docusaurus". | -> Docusaurus also supports deploying [user or organization sites](https://help.github.com/articles/user-organization-and-project-pages/#user--organization-pages). These sites will be served from the `master` branch of the repo. So, you will want to have the Docusaurus infra, your docs, etc. in another branch (e.g., maybe call it `source`). To do this, just set `projectName` to "_username_.github.io" (where _username_ is your username or organization name on GitHub) and `organizationName` to "_username_". The publish script will automatically deploy your site to the root of the `master` branch to be served. +> Docusaurus also supports deploying [user or organization sites](https://help.github.com/articles/user-organization-and-project-pages/#user--organization-pages). To do this, just set `projectName` to "_username_.github.io" (where _username_ is your username or organization name on GitHub) and `organizationName` to "_username_". +> For user or org sites, the publish script will deploy these sites to the root of the `master` branch of the _username_.github.io repo. In this case, note that you will want to have the Docusaurus infra, your docs, etc. either in another branch of the _username_.github.io repo (e.g., maybe call it `source`), or in another, separated repo (e.g. in the same as the documented source code). > While we recommend setting the `projectName` and `organizationName` in `siteConfig.js`, you can also use environment variables `ORGANIZATION_NAME` and `PROJECT_NAME`. diff --git a/lib/publish-gh-pages.js b/lib/publish-gh-pages.js index 95b2639419..779513d02c 100755 --- a/lib/publish-gh-pages.js +++ b/lib/publish-gh-pages.js @@ -66,9 +66,15 @@ if (IS_PULL_REQUEST) { shell.exit(0); } +// When we want to do a cross repo publish (#717), we can allow publishing to the same branch. +const currentRepoUrl = shell.exec('git remote get-url origin').stdout.trim(); +const crossRepoPublish = !currentRepoUrl.endsWith( + `${ORGANIZATION_NAME}/${PROJECT_NAME}.git` +); + // build static html files, then push to DEPLOYMENT_BRANCH branch of specified repo -if (CURRENT_BRANCH === DEPLOYMENT_BRANCH) { +if (CURRENT_BRANCH === DEPLOYMENT_BRANCH && !crossRepoPublish) { shell.echo(`Cannot deploy from a ${DEPLOYMENT_BRANCH} branch. Only to it`); shell.exit(1); } @@ -91,20 +97,27 @@ if ( shell.cd(`${PROJECT_NAME}-${DEPLOYMENT_BRANCH}`); -if (shell.exec(`git checkout origin/${DEPLOYMENT_BRANCH}`).code !== 0) { - if (shell.exec(`git checkout --orphan ${DEPLOYMENT_BRANCH}`).code !== 0) { - shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`); - shell.exit(1); - } -} else { - if ( - shell.exec(`git checkout -b ${DEPLOYMENT_BRANCH}`).code + - shell.exec(`git branch --set-upstream-to=origin/${DEPLOYMENT_BRANCH}`) - .code !== - 0 - ) { - shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`); - shell.exit(1); +// If the default branch is the one we're deploying to, then we'll fail to create it. +// This is the case of a cross-repo publish, where we clone a github.io repo with a default master branch. +const defaultBranch = shell + .exec('git rev-parse --abbrev-ref HEAD') + .stdout.trim(); +if (defaultBranch !== DEPLOYMENT_BRANCH) { + if (shell.exec(`git checkout origin/${DEPLOYMENT_BRANCH}`).code !== 0) { + if (shell.exec(`git checkout --orphan ${DEPLOYMENT_BRANCH}`).code !== 0) { + shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`); + shell.exit(1); + } + } else { + if ( + shell.exec(`git checkout -b ${DEPLOYMENT_BRANCH}`).code + + shell.exec(`git branch --set-upstream-to=origin/${DEPLOYMENT_BRANCH}`) + .code !== + 0 + ) { + shell.echo(`Error: Git checkout ${DEPLOYMENT_BRANCH} failed`); + shell.exit(1); + } } }