mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
refactor: optimize clone and checkout in deploy command (#5829)
* refactor: optimize clone and checkout in deploy command * refactor: remove obsolete check for default branch and simplify flow * refactor: skip cloning repository if deployment branch doesn't exist * Refactors * More tip about failure Co-authored-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
parent
ac88d979f1
commit
6c0a193fc8
1 changed files with 21 additions and 41 deletions
|
@ -179,9 +179,9 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
|
||||||
process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com';
|
process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com';
|
||||||
const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort;
|
const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort;
|
||||||
|
|
||||||
let remoteBranch: string;
|
let deploymentRepoURL: string;
|
||||||
if (useSSH) {
|
if (useSSH) {
|
||||||
remoteBranch = buildSshUrl(
|
deploymentRepoURL = buildSshUrl(
|
||||||
githubHost,
|
githubHost,
|
||||||
organizationName,
|
organizationName,
|
||||||
projectName,
|
projectName,
|
||||||
|
@ -190,7 +190,7 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
|
||||||
} else {
|
} else {
|
||||||
const gitPass = process.env.GIT_PASS;
|
const gitPass = process.env.GIT_PASS;
|
||||||
const gitCredentials = gitPass ? `${gitUser!}:${gitPass}` : gitUser!;
|
const gitCredentials = gitPass ? `${gitUser!}:${gitPass}` : gitUser!;
|
||||||
remoteBranch = buildHttpsUrl(
|
deploymentRepoURL = buildHttpsUrl(
|
||||||
gitCredentials,
|
gitCredentials,
|
||||||
githubHost,
|
githubHost,
|
||||||
organizationName,
|
organizationName,
|
||||||
|
@ -200,7 +200,7 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`${chalk.cyan('Remote branch:')} ${obfuscateGitPass(remoteBranch)}`,
|
`${chalk.cyan('Remote repo URL:')} ${obfuscateGitPass(deploymentRepoURL)}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check if this is a cross-repo publish.
|
// Check if this is a cross-repo publish.
|
||||||
|
@ -225,45 +225,24 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
|
||||||
const toPath = await fs.mkdtemp(
|
const toPath = await fs.mkdtemp(
|
||||||
path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`),
|
path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`),
|
||||||
);
|
);
|
||||||
if (
|
|
||||||
shellExecLog(
|
|
||||||
`git clone --depth 1 --no-single-branch ${remoteBranch} ${toPath}`,
|
|
||||||
).code !== 0
|
|
||||||
) {
|
|
||||||
throw new Error(`Running "git clone" command in "${toPath}" failed.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
shell.cd(toPath);
|
shell.cd(toPath);
|
||||||
|
|
||||||
// If the default branch is the one we're deploying to, then we'll fail
|
// Check out deployment branch when cloning repository, and then remove all
|
||||||
// to create it. This is the case of a cross-repo publish, where we clone
|
// the files in the directory. If the 'clone' command fails, assume that
|
||||||
// a github.io repo with a default branch.
|
// the deployment branch doesn't exist, and initialize git in an empty
|
||||||
const defaultBranch = shell
|
// directory, check out a clean deployment branch and add remote.
|
||||||
.exec('git rev-parse --abbrev-ref HEAD')
|
if (
|
||||||
.stdout.trim();
|
shellExecLog(
|
||||||
if (defaultBranch !== deploymentBranch) {
|
`git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} ${toPath}`,
|
||||||
if (shellExecLog(`git checkout origin/${deploymentBranch}`).code !== 0) {
|
).code === 0
|
||||||
if (
|
) {
|
||||||
shellExecLog(`git checkout --orphan ${deploymentBranch}`).code !== 0
|
shellExecLog('git rm -rf .');
|
||||||
) {
|
} else {
|
||||||
throw new Error(
|
shellExecLog('git init');
|
||||||
`Running "git checkout ${deploymentBranch}" command failed.`,
|
shellExecLog(`git checkout -b ${deploymentBranch}`);
|
||||||
);
|
shellExecLog(`git remote add origin ${deploymentRepoURL}`);
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
shellExecLog(`git checkout -b ${deploymentBranch}`).code +
|
|
||||||
shellExecLog(
|
|
||||||
`git branch --set-upstream-to=origin/${deploymentBranch}`,
|
|
||||||
).code !==
|
|
||||||
0
|
|
||||||
) {
|
|
||||||
throw new Error(
|
|
||||||
`Running "git checkout ${deploymentBranch}" command failed.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shellExecLog('git rm -rf .');
|
|
||||||
try {
|
try {
|
||||||
await fs.copy(fromPath, toPath);
|
await fs.copy(fromPath, toPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -271,7 +250,6 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
|
||||||
`Copying build assets from "${fromPath}" to "${toPath}" failed with error "${error}".`,
|
`Copying build assets from "${fromPath}" to "${toPath}" failed with error "${error}".`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
shell.cd(toPath);
|
|
||||||
shellExecLog('git add --all');
|
shellExecLog('git add --all');
|
||||||
|
|
||||||
const commitMessage =
|
const commitMessage =
|
||||||
|
@ -281,7 +259,9 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
|
||||||
if (
|
if (
|
||||||
shellExecLog(`git push --force origin ${deploymentBranch}`).code !== 0
|
shellExecLog(`git push --force origin ${deploymentBranch}`).code !== 0
|
||||||
) {
|
) {
|
||||||
throw new Error('Running "git push" command failed.');
|
throw new Error(
|
||||||
|
'Running "git push" command failed. Does the GitHub user account you are using have push access to the repository?',
|
||||||
|
);
|
||||||
} else if (commitResults.code === 0) {
|
} else if (commitResults.code === 0) {
|
||||||
// The commit might return a non-zero value when site is up to date.
|
// The commit might return a non-zero value when site is up to date.
|
||||||
let websiteURL = '';
|
let websiteURL = '';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue