github-pages-deploy-action/src/git.ts

318 lines
8.9 KiB
TypeScript
Raw Normal View History

2020-03-28 22:35:26 +08:00
import {info} from '@actions/core'
import {mkdirP, rmRF} from '@actions/io'
import fs from 'fs'
import {ActionInterface, Status} from './constants'
2020-03-05 21:19:45 +08:00
import {execute} from './execute'
import {
hasRequiredParameters,
isNullOrUndefined,
suppressSensitiveInformation
2020-03-05 21:19:45 +08:00
} from './util'
2020-03-02 22:05:39 +08:00
/* Initializes git in the workspace. */
2020-03-07 11:36:56 +08:00
export async function init(action: ActionInterface): Promise<void | Error> {
try {
2020-03-05 21:19:45 +08:00
hasRequiredParameters(action)
2020-03-28 23:33:51 +08:00
info(`Deploying using ${action.tokenType}… 🔑`)
info('Configuring git…')
2020-06-26 20:18:23 +08:00
await execute(`git init`, action.workspace, action.silent)
await execute(
`git config user.name "${action.name}"`,
action.workspace,
action.silent
)
await execute(
`git config user.email "${action.email}"`,
action.workspace,
action.silent
)
2020-07-05 02:38:32 +08:00
2020-06-26 20:18:23 +08:00
await execute(`git remote rm origin`, action.workspace, action.silent)
await execute(
`git remote add origin ${action.repositoryPath}`,
2020-06-27 02:16:54 +08:00
action.workspace,
action.silent
)
2020-06-26 20:18:23 +08:00
await execute(
`git fetch --no-recurse-submodules`,
action.workspace,
action.silent
)
2020-03-28 23:33:51 +08:00
info('Git configured… 🔧')
} catch (error) {
throw new Error(
2020-07-05 02:47:45 +08:00
`There was an error initializing the repository: ${suppressSensitiveInformation(
error.message,
action
)} `
2020-03-05 21:19:45 +08:00
)
}
}
/* Switches to the base branch. */
export async function switchToBaseBranch(
2020-03-07 11:36:56 +08:00
action: ActionInterface
): Promise<void> {
try {
2020-03-05 21:19:45 +08:00
hasRequiredParameters(action)
await execute(
`git checkout --progress --force ${
action.baseBranch ? action.baseBranch : action.defaultBranch
}`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
} catch (error) {
throw new Error(
`There was an error switching to the base branch: ${suppressSensitiveInformation(
error.message,
action
)} `
2020-03-05 21:19:45 +08:00
)
}
}
/* Generates the branch if it doesn't exist on the remote. */
2020-03-07 11:36:56 +08:00
export async function generateBranch(action: ActionInterface): Promise<void> {
try {
2020-03-05 21:19:45 +08:00
hasRequiredParameters(action)
2020-03-28 23:33:51 +08:00
info(`Creating the ${action.branch} branch…`)
2020-03-05 21:19:45 +08:00
await switchToBaseBranch(action)
2020-06-26 20:18:23 +08:00
await execute(
`git checkout --orphan ${action.branch}`,
action.workspace,
action.silent
)
await execute(`git reset --hard`, action.workspace, action.silent)
await execute(
`git commit --no-verify --allow-empty -m "Initial ${action.branch} commit"`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
await execute(
2020-03-31 21:40:27 +08:00
`git push --force ${action.repositoryPath} ${action.branch}`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
2020-06-26 20:18:23 +08:00
await execute(`git fetch`, action.workspace, action.silent)
2020-03-28 23:33:51 +08:00
info(`Created the ${action.branch} branch… 🔧`)
} catch (error) {
throw new Error(
`There was an error creating the deployment branch: ${suppressSensitiveInformation(
error.message,
action
)} `
2020-03-05 21:19:45 +08:00
)
}
}
/* Runs the necessary steps to make the deployment. */
export async function deploy(action: ActionInterface): Promise<Status> {
2020-05-25 00:50:10 +08:00
const temporaryDeploymentDirectory =
'github-pages-deploy-action-temp-deployment-folder'
2020-05-25 00:49:54 +08:00
const temporaryDeploymentBranch = `github-pages-deploy-action/${Math.random()
.toString(36)
.substr(2, 9)}`
2020-03-28 23:33:51 +08:00
info('Starting to commit changes…')
try {
2020-03-05 21:19:45 +08:00
hasRequiredParameters(action)
const commitMessage = !isNullOrUndefined(action.commitMessage)
? (action.commitMessage as string)
: `Deploying to ${action.branch} from ${action.baseBranch} ${
process.env.GITHUB_SHA ? `@ ${process.env.GITHUB_SHA}` : ''
} 🚀`
/*
Checks to see if the remote exists prior to deploying.
If the branch doesn't exist it gets created here as an orphan.
*/
const branchExists = await execute(
`git ls-remote --heads ${action.repositoryPath} ${action.branch} | wc -l`,
2020-06-27 02:10:06 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
if (!branchExists && !action.isTest) {
2020-03-05 21:19:45 +08:00
await generateBranch(action)
2019-12-21 06:36:35 +08:00
}
// Checks out the base branch to begin the deployment process.
2020-03-05 21:19:45 +08:00
await switchToBaseBranch(action)
2020-09-13 05:33:19 +08:00
if (action.lfs) {
// Migrates data from LFS so it can be comitted the "normal" way.
await execute(
`git lfs migrate export --include="*"`,
action.workspace,
action.silent
)
}
2020-06-26 20:18:23 +08:00
await execute(
`git fetch ${action.repositoryPath}`,
action.workspace,
action.silent
)
await execute(
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
// Ensures that items that need to be excluded from the clean job get parsed.
2020-03-05 21:19:45 +08:00
let excludes = ''
if (action.clean && action.cleanExclude) {
try {
const excludedItems =
2020-03-05 21:19:45 +08:00
typeof action.cleanExclude === 'string'
? JSON.parse(action.cleanExclude)
2020-03-05 21:19:45 +08:00
: action.cleanExclude
2020-03-07 11:36:56 +08:00
for (const item of excludedItems) {
excludes += `--exclude ${item} `
}
} catch {
2020-03-28 22:35:26 +08:00
info(
2020-03-05 21:19:45 +08:00
'There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌'
)
}
}
if (action.targetFolder) {
info(`Creating target folder if it doesn't already exist… 📌`)
await mkdirP(`${temporaryDeploymentDirectory}/${action.targetFolder}`)
}
/*
Pushes all of the build files into the deployment directory.
Allows the user to specify the root if '.' is provided.
rsync is used to prevent file duplication. */
await execute(
2020-06-06 21:15:31 +08:00
`rsync -q -av --checksum --progress ${action.folder}/. ${
action.targetFolder
? `${temporaryDeploymentDirectory}/${action.targetFolder}`
: temporaryDeploymentDirectory
} ${
action.clean
? `--delete ${excludes} ${
!fs.existsSync(`${action.folder}/CNAME`) ? '--exclude CNAME' : ''
} ${
!fs.existsSync(`${action.folder}/.nojekyll`)
? '--exclude .nojekyll'
: ''
}`
2020-03-05 21:19:45 +08:00
: ''
} --exclude .ssh --exclude .git --exclude .github ${
action.folder === action.root
? `--exclude ${temporaryDeploymentDirectory}`
2020-03-05 21:19:45 +08:00
: ''
}`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
const hasFilesToCommit = await execute(
`git status --porcelain`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
2020-03-05 21:19:45 +08:00
)
if (!hasFilesToCommit && !action.isTest) {
return Status.SKIPPED
}
// Commits to GitHub.
await execute(
`git add --all .`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
2020-03-05 21:19:45 +08:00
)
await execute(
`git checkout -b ${temporaryDeploymentBranch}`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
2020-03-05 21:19:45 +08:00
)
await execute(
`git commit -m "${commitMessage}" --quiet --no-verify`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
2020-03-05 21:19:45 +08:00
)
await execute(
`git push --force ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
2020-03-05 21:19:45 +08:00
)
2020-03-28 23:33:51 +08:00
info(`Changes committed to the ${action.branch} branch… 📦`)
if (action.singleCommit) {
2020-06-26 20:18:23 +08:00
await execute(
`git fetch ${action.repositoryPath}`,
action.workspace,
action.silent
)
await execute(
`git checkout --orphan ${action.branch}-temp`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`git add --all .`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`git commit -m "${commitMessage}" --quiet --no-verify`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`git branch -M ${action.branch}-temp ${action.branch}`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
await execute(
`git push origin ${action.branch} --force`,
2020-06-26 20:18:23 +08:00
`${action.workspace}/${temporaryDeploymentDirectory}`,
action.silent
)
info('Cleared git history… 🚿')
}
await execute(
`git checkout --progress --force ${action.defaultBranch}`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
2020-03-05 21:19:45 +08:00
)
return Status.SUCCESS
} catch (error) {
throw new Error(
`The deploy step encountered an error: ${suppressSensitiveInformation(
error.message,
action
)} `
2020-03-05 21:19:45 +08:00
)
} finally {
// Cleans up temporary files/folders and restores the git state.
info('Running post deployment cleanup jobs… 🗑️')
await execute(
2020-05-24 23:24:33 +08:00
`git worktree remove ${temporaryDeploymentDirectory} --force`,
2020-06-26 20:18:23 +08:00
action.workspace,
action.silent
)
await rmRF(temporaryDeploymentDirectory)
}
}