2020-03-28 22:35:26 +08:00
|
|
|
import {info} from '@actions/core'
|
2020-04-30 20:29:24 +08:00
|
|
|
import {rmRF, mkdirP} from '@actions/io'
|
2020-05-15 05:24:32 +08:00
|
|
|
import {ActionInterface, Status} from './constants'
|
2020-03-05 21:19:45 +08:00
|
|
|
import {execute} from './execute'
|
2020-03-02 20:52:38 +08:00
|
|
|
import {
|
|
|
|
hasRequiredParameters,
|
|
|
|
isNullOrUndefined,
|
|
|
|
suppressSensitiveInformation
|
2020-03-05 21:19:45 +08:00
|
|
|
} from './util'
|
2019-11-19 23:06:27 +08:00
|
|
|
|
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> {
|
2019-11-19 23:06:27 +08:00
|
|
|
try {
|
2020-03-05 21:19:45 +08:00
|
|
|
hasRequiredParameters(action)
|
2019-11-19 23:06:27 +08:00
|
|
|
|
2020-03-28 23:33:51 +08:00
|
|
|
info(`Deploying using ${action.tokenType}… 🔑`)
|
|
|
|
info('Configuring git…')
|
2020-01-20 04:33:14 +08:00
|
|
|
|
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-05-18 20:55:52 +08:00
|
|
|
await execute(`git fetch --no-recurse-submodules`, action.workspace)
|
2019-11-19 23:06:27 +08:00
|
|
|
|
2020-03-28 23:33:51 +08:00
|
|
|
info('Git configured… 🔧')
|
2019-11-19 23:06:27 +08:00
|
|
|
} catch (error) {
|
2020-03-02 20:52:38 +08:00
|
|
|
throw new Error(
|
|
|
|
`There was an error initializing the repository: ${suppressSensitiveInformation(
|
|
|
|
error.message,
|
|
|
|
action
|
|
|
|
)} ❌`
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2019-11-19 23:06:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
/* Switches to the base branch. */
|
|
|
|
export async function switchToBaseBranch(
|
2020-03-07 11:36:56 +08:00
|
|
|
action: ActionInterface
|
2020-03-02 20:52:38 +08:00
|
|
|
): Promise<void> {
|
|
|
|
try {
|
2020-03-05 21:19:45 +08:00
|
|
|
hasRequiredParameters(action)
|
2020-03-02 20:52:38 +08:00
|
|
|
|
|
|
|
await execute(
|
|
|
|
`git checkout --progress --force ${
|
|
|
|
action.baseBranch ? action.baseBranch : action.defaultBranch
|
|
|
|
}`,
|
|
|
|
action.workspace
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +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
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
}
|
2019-12-22 05:51:39 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 20:52:38 +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> {
|
2019-11-19 23:06:27 +08:00
|
|
|
try {
|
2020-03-05 21:19:45 +08:00
|
|
|
hasRequiredParameters(action)
|
2020-01-20 04:33:14 +08:00
|
|
|
|
2020-03-28 23:33:51 +08:00
|
|
|
info(`Creating the ${action.branch} branch…`)
|
2020-03-02 20:52:38 +08:00
|
|
|
|
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)
|
2019-11-19 23:06:27 +08:00
|
|
|
await execute(
|
2020-03-28 23:33:51 +08:00
|
|
|
`git commit --allow-empty -m "Initial ${action.branch} commit"`,
|
2020-03-02 20:52:38 +08:00
|
|
|
action.workspace
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
await execute(
|
2020-03-31 21:40:27 +08:00
|
|
|
`git push --force ${action.repositoryPath} ${action.branch}`,
|
2020-03-02 20:52:38 +08:00
|
|
|
action.workspace
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
|
|
|
await execute(`git fetch`, action.workspace)
|
2020-03-02 20:52:38 +08:00
|
|
|
|
2020-03-28 23:33:51 +08:00
|
|
|
info(`Created the ${action.branch} branch… 🔧`)
|
2019-11-19 23:06:27 +08:00
|
|
|
} catch (error) {
|
2020-03-02 20:52:38 +08:00
|
|
|
throw new Error(
|
|
|
|
`There was an error creating the deployment branch: ${suppressSensitiveInformation(
|
|
|
|
error.message,
|
|
|
|
action
|
|
|
|
)} ❌`
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2019-11-19 23:06:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
/* Runs the necessary steps to make the deployment. */
|
2020-05-15 05:24:32 +08:00
|
|
|
export async function deploy(action: ActionInterface): Promise<Status> {
|
2020-03-05 21:19:45 +08:00
|
|
|
const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder'
|
|
|
|
const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch'
|
2020-03-31 08:44:09 +08:00
|
|
|
|
2020-03-28 23:33:51 +08:00
|
|
|
info('Starting to commit changes…')
|
2019-11-19 23:06:27 +08:00
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
try {
|
2020-03-05 21:19:45 +08:00
|
|
|
hasRequiredParameters(action)
|
2020-03-02 20:52:38 +08:00
|
|
|
|
2020-04-04 20:23:43 +08:00
|
|
|
const commitMessage = !isNullOrUndefined(action.commitMessage)
|
|
|
|
? (action.commitMessage as string)
|
|
|
|
: `Deploying to ${action.branch} from ${action.baseBranch} ${
|
|
|
|
process.env.GITHUB_SHA ? `@ ${process.env.GITHUB_SHA}` : ''
|
|
|
|
} 🚀`
|
2020-03-31 08:44:09 +08:00
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
/*
|
|
|
|
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
|
|
|
)
|
2020-03-02 20:52:38 +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
|
|
|
}
|
|
|
|
|
2020-03-02 20:52:38 +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)
|
2020-03-02 20:52:38 +08:00
|
|
|
await execute(
|
|
|
|
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
|
|
|
|
action.workspace
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +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 = ''
|
2020-03-02 20:52:38 +08:00
|
|
|
if (action.clean && action.cleanExclude) {
|
|
|
|
try {
|
|
|
|
const excludedItems =
|
2020-03-05 21:19:45 +08:00
|
|
|
typeof action.cleanExclude === 'string'
|
2020-03-02 20:52:38 +08:00
|
|
|
? 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} `
|
|
|
|
}
|
2020-03-02 20:52:38 +08:00
|
|
|
} 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. ❌'
|
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:29:24 +08:00
|
|
|
if (action.targetFolder) {
|
|
|
|
info(`Creating target folder if it doesn't already exist… 📌`)
|
|
|
|
await mkdirP(`${temporaryDeploymentDirectory}/${action.targetFolder}`)
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
/*
|
|
|
|
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
|
|
|
: ''
|
2020-03-02 20:52:38 +08:00
|
|
|
} --exclude .ssh --exclude .git --exclude .github ${
|
|
|
|
action.folder === action.root
|
|
|
|
? `--exclude ${temporaryDeploymentDirectory}`
|
2020-03-05 21:19:45 +08:00
|
|
|
: ''
|
2020-03-02 20:52:38 +08:00
|
|
|
}`,
|
|
|
|
action.workspace
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2019-11-20 07:07:27 +08:00
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
const hasFilesToCommit = await execute(
|
|
|
|
`git status --porcelain`,
|
|
|
|
`${action.workspace}/${temporaryDeploymentDirectory}`
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
|
|
|
|
if (!hasFilesToCommit && !action.isTest) {
|
2020-05-15 05:24:32 +08:00
|
|
|
return Status.SKIPPED
|
2020-03-02 20:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commits to GitHub.
|
|
|
|
await execute(
|
|
|
|
`git add --all .`,
|
|
|
|
`${action.workspace}/${temporaryDeploymentDirectory}`
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
await execute(
|
|
|
|
`git checkout -b ${temporaryDeploymentBranch}`,
|
|
|
|
`${action.workspace}/${temporaryDeploymentDirectory}`
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
await execute(
|
2020-03-31 08:44:09 +08:00
|
|
|
`git commit -m "${commitMessage}" --quiet`,
|
2020-03-02 20:52:38 +08:00
|
|
|
`${action.workspace}/${temporaryDeploymentDirectory}`
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-03-02 20:52:38 +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-02 20:52:38 +08:00
|
|
|
|
2020-03-28 23:33:51 +08:00
|
|
|
info(`Changes committed to the ${action.branch} branch… 📦`)
|
2020-03-02 20:52:38 +08:00
|
|
|
|
|
|
|
// Cleans up temporary files/folders and restores the git state.
|
2020-03-28 23:33:51 +08:00
|
|
|
info('Running post deployment cleanup jobs…')
|
2020-03-31 08:44:09 +08:00
|
|
|
|
|
|
|
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… 🚿')
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:52:38 +08:00
|
|
|
await execute(
|
|
|
|
`git checkout --progress --force ${action.defaultBranch}`,
|
|
|
|
action.workspace
|
2020-03-05 21:19:45 +08:00
|
|
|
)
|
2020-05-15 05:24:32 +08:00
|
|
|
|
|
|
|
return Status.SUCCESS
|
2020-03-02 20:52:38 +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
|
|
|
)
|
2020-03-02 20:52:38 +08:00
|
|
|
} finally {
|
2020-04-12 01:57:56 +08:00
|
|
|
// Ensures the deployment directory is safely removed after each deployment.
|
2020-05-24 22:57:49 +08:00
|
|
|
await execute(
|
|
|
|
`git worktree remove ${temporaryDeploymentDirectory}`,
|
|
|
|
action.workspace
|
|
|
|
)
|
2020-04-12 01:57:56 +08:00
|
|
|
await rmRF(temporaryDeploymentDirectory)
|
2020-03-02 20:52:38 +08:00
|
|
|
}
|
2019-11-19 23:06:27 +08:00
|
|
|
}
|