improve deploy script

This commit is contained in:
sebastien 2025-02-28 12:43:46 +01:00
parent 4d5751376a
commit 056373b467

View file

@ -30,15 +30,24 @@ const debugMode = !!process.env.DOCUSAURUS_DEPLOY_DEBUG;
// Log executed commands so that user can figure out mistakes on his own // Log executed commands so that user can figure out mistakes on his own
// for example: https://github.com/facebook/docusaurus/issues/3875 // for example: https://github.com/facebook/docusaurus/issues/3875
function shellExecLog(cmd: string) { function exec(cmd: string, options?: {log?: boolean; failfast?: boolean}) {
const log = options?.log ?? true;
const failfast = options?.failfast ?? false;
try { try {
const result = execa.commandSync(cmd); const result = execa.commandSync(cmd);
logger.info`code=${obfuscateGitPass( if (log || debugMode) {
cmd, logger.info`code=${obfuscateGitPass(
)} subdue=${`code: ${result.exitCode}`}`; cmd,
)} subdue=${`code: ${result.exitCode}`}`;
}
if (debugMode) { if (debugMode) {
console.log(result); console.log(result);
} }
if (failfast && result.exitCode !== 0) {
throw new Error(
`Command returned unexpected exitCode ${result.exitCode}`,
);
}
return result; return result;
} catch (err) { } catch (err) {
throw new Error( throw new Error(
@ -52,7 +61,7 @@ In CWD code=${process.cwd()}`,
} }
function hasGit() { function hasGit() {
return shellExecLog('git --version').exitCode === 0; return exec('git --version').exitCode === 0;
} }
export async function deploy( export async function deploy(
@ -80,14 +89,21 @@ This behavior can have SEO impacts and create relative link issues.
} }
// Source repo is the repo from where the command is invoked // Source repo is the repo from where the command is invoked
// TODO silent const {stdout} = exec('git remote get-url origin', {
const {stdout} = await execa.command('git remote get-url origin'); log: false,
failfast: true,
});
const sourceRepoUrl = stdout.trim(); const sourceRepoUrl = stdout.trim();
// The source branch; defaults to the currently checked out branch // The source branch; defaults to the currently checked out branch
const sourceBranch = const sourceBranch =
process.env.CURRENT_BRANCH ?? process.env.CURRENT_BRANCH ??
execa.command('git rev-parse --abbrev-ref HEAD')?.stdout?.toString().trim(); exec('git rev-parse --abbrev-ref HEAD', {
log: false,
failfast: true,
})
?.stdout?.toString()
.trim();
const gitUser = process.env.GIT_USER; const gitUser = process.env.GIT_USER;
@ -132,7 +148,10 @@ This behavior can have SEO impacts and create relative link issues.
const isPullRequest = const isPullRequest =
process.env.CI_PULL_REQUEST ?? process.env.CIRCLE_PULL_REQUEST; process.env.CI_PULL_REQUEST ?? process.env.CIRCLE_PULL_REQUEST;
if (isPullRequest) { if (isPullRequest) {
await execa.command('echo "Skipping deploy on a pull request."'); exec('echo "Skipping deploy on a pull request."', {
log: false,
failfast: true,
});
process.exit(0); process.exit(0);
} }
@ -197,9 +216,7 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
// Save the commit hash that triggers publish-gh-pages before checking // Save the commit hash that triggers publish-gh-pages before checking
// out to deployment branch. // out to deployment branch.
const currentCommit = shellExecLog('git rev-parse HEAD') const currentCommit = exec('git rev-parse HEAD')?.stdout?.toString().trim();
?.stdout?.toString()
.trim();
const runDeploy = async (outputDirectory: string) => { const runDeploy = async (outputDirectory: string) => {
const targetDirectory = cliOptions.targetDir ?? '.'; const targetDirectory = cliOptions.targetDir ?? '.';
@ -207,22 +224,22 @@ 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}`),
); );
await execa.command(`cd ${toPath}`); exec(`cd ${toPath}`, {failfast: true});
// Clones the repo into the temp folder and checks out the target branch. // Clones the repo into the temp folder and checks out the target branch.
// If the branch doesn't exist, it creates a new one based on the // If the branch doesn't exist, it creates a new one based on the
// repository default branch. // repository default branch.
if ( if (
shellExecLog( exec(
`git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} "${toPath}"`, `git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} "${toPath}"`,
).exitCode !== 0 ).exitCode !== 0
) { ) {
shellExecLog(`git clone --depth 1 ${deploymentRepoURL} "${toPath}"`); exec(`git clone --depth 1 ${deploymentRepoURL} "${toPath}"`);
shellExecLog(`git checkout -b ${deploymentBranch}`); exec(`git checkout -b ${deploymentBranch}`);
} }
// Clear out any existing contents in the target directory // Clear out any existing contents in the target directory
shellExecLog(`git rm -rf ${targetDirectory}`); exec(`git rm -rf ${targetDirectory}`, {log: false, failfast: true});
const targetPath = path.join(toPath, targetDirectory); const targetPath = path.join(toPath, targetDirectory);
try { try {
@ -231,25 +248,23 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
logger.error`Copying build assets from path=${fromPath} to path=${targetPath} failed.`; logger.error`Copying build assets from path=${fromPath} to path=${targetPath} failed.`;
throw err; throw err;
} }
shellExecLog('git add --all'); exec('git add --all');
const gitUserName = process.env.GIT_USER_NAME; const gitUserName = process.env.GIT_USER_NAME;
if (gitUserName) { if (gitUserName) {
shellExecLog(`git config user.name "${gitUserName}"`); exec(`git config user.name "${gitUserName}"`, {failfast: true});
} }
const gitUserEmail = process.env.GIT_USER_EMAIL; const gitUserEmail = process.env.GIT_USER_EMAIL;
if (gitUserEmail) { if (gitUserEmail) {
shellExecLog(`git config user.email "${gitUserEmail}"`); exec(`git config user.email "${gitUserEmail}"`, {failfast: true});
} }
const commitMessage = const commitMessage =
process.env.CUSTOM_COMMIT_MESSAGE ?? process.env.CUSTOM_COMMIT_MESSAGE ??
`Deploy website - based on ${currentCommit}`; `Deploy website - based on ${currentCommit}`;
const commitResults = shellExecLog(`git commit -m "${commitMessage}"`); const commitResults = exec(`git commit -m "${commitMessage}"`);
if ( if (exec(`git push --force origin ${deploymentBranch}`).exitCode !== 0) {
shellExecLog(`git push --force origin ${deploymentBranch}`).exitCode !== 0
) {
throw new Error( throw new Error(
'Running "git push" command failed. Does the GitHub user account you are using have push access to the repository?', 'Running "git push" command failed. Does the GitHub user account you are using have push access to the repository?',
); );
@ -265,7 +280,7 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
websiteURL = `https://${githubHost}/pages/${organizationName}/${projectName}/`; websiteURL = `https://${githubHost}/pages/${organizationName}/${projectName}/`;
} }
try { try {
await execa.command(`echo "Website is live at ${websiteURL}."`); exec(`echo "Website is live at ${websiteURL}."`, {failfast: true});
process.exit(0); process.exit(0);
} catch (err) { } catch (err) {
throw new Error(`Failed to execute command: ${err}`); throw new Error(`Failed to execute command: ${err}`);