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

249 lines
7.5 KiB
TypeScript
Raw Normal View History

2020-03-28 22:35:26 +08:00
import {info} from '@actions/core'
2020-03-07 11:36:56 +08:00
import {ActionInterface} 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-03-05 21:19:45 +08:00
await execute(`git init`, action.workspace)
await execute(`git config user.name "${action.name}"`, action.workspace)
await execute(`git config user.email "${action.email}"`, action.workspace)
2020-03-29 00:38:18 +08:00
await execute(`git remote rm origin`, action.workspace)
await execute(
`git remote add origin ${action.repositoryPath}`,
action.workspace
)
2020-03-05 21:19:45 +08:00
await execute(`git fetch`, action.workspace)
2020-03-28 23:33:51 +08:00
info('Git configured… 🔧')
} catch (error) {
throw new Error(
`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
}`,
action.workspace
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)
await execute(`git checkout --orphan ${action.branch}`, action.workspace)
await execute(`git reset --hard`, action.workspace)
await execute(
2020-03-28 23:33:51 +08:00
`git commit --allow-empty -m "Initial ${action.branch} commit"`,
action.workspace
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}`,
action.workspace
2020-03-05 21:19:45 +08:00
)
await execute(`git fetch`, action.workspace)
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. */
2020-03-07 11:36:56 +08:00
export async function deploy(action: ActionInterface): Promise<void> {
2020-03-05 21:19:45 +08:00
const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder'
const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch'
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`,
action.workspace
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)
await execute(`git fetch ${action.repositoryPath}`, action.workspace)
await execute(
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
action.workspace
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. ❌'
)
}
}
/*
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(
`rsync -q -av --progress ${action.folder}/. ${
action.targetFolder
? `${temporaryDeploymentDirectory}/${action.targetFolder}`
: temporaryDeploymentDirectory
} ${
action.clean
? `--delete ${excludes} --exclude CNAME --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
: ''
}`,
action.workspace
2020-03-05 21:19:45 +08:00
)
const hasFilesToCommit = await execute(
`git status --porcelain`,
`${action.workspace}/${temporaryDeploymentDirectory}`
2020-03-05 21:19:45 +08:00
)
if (!hasFilesToCommit && !action.isTest) {
2020-03-28 23:33:51 +08:00
info('There is nothing to commit. Exiting early… 📭')
2020-03-05 21:19:45 +08:00
return
}
// Commits to GitHub.
await execute(
`git add --all .`,
`${action.workspace}/${temporaryDeploymentDirectory}`
2020-03-05 21:19:45 +08:00
)
await execute(
`git checkout -b ${temporaryDeploymentBranch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`
2020-03-05 21:19:45 +08:00
)
await execute(
`git commit -m "${commitMessage}" --quiet`,
`${action.workspace}/${temporaryDeploymentDirectory}`
2020-03-05 21:19:45 +08:00
)
await execute(
`git push --force ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`
2020-03-05 21:19:45 +08:00
)
2020-03-28 23:33:51 +08:00
info(`Changes committed to the ${action.branch} branch… 📦`)
// Cleans up temporary files/folders and restores the git state.
2020-03-28 23:33:51 +08:00
info('Running post deployment cleanup jobs…')
if (action.singleCommit) {
await execute(`git fetch ${action.repositoryPath}`, action.workspace)
await execute(
`git checkout --orphan ${action.branch}-temp`,
`${action.workspace}/${temporaryDeploymentDirectory}`
)
await execute(
`git add --all .`,
`${action.workspace}/${temporaryDeploymentDirectory}`
)
await execute(
`git commit -m "${commitMessage}" --quiet`,
`${action.workspace}/${temporaryDeploymentDirectory}`
)
await execute(
`git branch -M ${action.branch}-temp ${action.branch}`,
`${action.workspace}/${temporaryDeploymentDirectory}`
)
await execute(
`git push origin ${action.branch} --force`,
`${action.workspace}/${temporaryDeploymentDirectory}`
)
info('Cleared git history… 🚿')
}
await execute(
`git checkout --progress --force ${action.defaultBranch}`,
action.workspace
2020-03-05 21:19:45 +08:00
)
} catch (error) {
throw new Error(
`The deploy step encountered an error: ${suppressSensitiveInformation(
error.message,
action
)} `
2020-03-05 21:19:45 +08:00
)
} finally {
// Ensures the deployment directory is safely removed.
await execute(
`chmod u+w -R ${temporaryDeploymentDirectory}`,
action.workspace
)
2020-03-05 21:19:45 +08:00
await execute(`rm -rf ${temporaryDeploymentDirectory}`, action.workspace)
}
}