Single Commit (#226)

* Wipe History #1

* singleCommit

* Integration tests

* Update README.md
This commit is contained in:
James Ives 2020-03-30 20:44:09 -04:00 committed by GitHub
parent aabaacd944
commit 2f96cedf7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 77 additions and 17 deletions

View File

@ -126,7 +126,7 @@ jobs:
BASE_BRANCH: dev
TARGET_FOLDER: montezuma4
# Deploys using the CLEAN option.
# Deploys using the CLEAN and SINGLE_COMMIT option.
integration-clean:
needs: [integration-checkout-v1, integration-checkout-v2, integration-container, integration-ssh, integration-env]
runs-on: ubuntu-latest
@ -144,8 +144,9 @@ jobs:
FOLDER: integration
BASE_BRANCH: dev
CLEAN: true
SINGLE_COMMIT: true
# Deploys to a branch that doesn't exist.
# Deploys to a branch that doesn't exist with SINGLE_COMMIT toggled.
integration-branch-creation:
runs-on: ubuntu-latest
steps:
@ -162,6 +163,7 @@ jobs:
FOLDER: integration
BASE_BRANCH: dev
CLEAN: true
SINGLE_COMMIT: true
- name: Cleanup Generated Branch
uses: dawidd6/action-delete-branch@v2.0.1

View File

@ -127,7 +127,7 @@ jobs:
BASE_BRANCH: dev
TARGET_FOLDER: montezuma4
# Deploys using the CLEAN option.
# Deploys using the CLEAN option with SINGLE_COMMIT toggled.
integration-clean:
needs: [integration-checkout-v1, integration-checkout-v2, integration-container, integration-ssh, integration-env]
runs-on: ubuntu-latest
@ -145,8 +145,9 @@ jobs:
FOLDER: integration
BASE_BRANCH: dev
CLEAN: true
SINGLE_COMMIT: true
# Deploys to a branch that doesn't exist.
# Deploys to a branch that doesn't exist with SINGLE_COMMIT.
integration-branch-creation:
runs-on: ubuntu-latest
steps:
@ -163,6 +164,7 @@ jobs:
FOLDER: integration
BASE_BRANCH: dev
CLEAN: true
SINGLE_COMMIT: true
- name: Cleanup Generated Branch
uses: dawidd6/action-delete-branch@v2.0.1

View File

@ -146,6 +146,7 @@ In addition to the deployment options you must also configure the following.
| `COMMIT_MESSAGE` | If you need to customize the commit message for an integration you can do so. | `with` | **No** |
| `CLEAN` | If your project generates hashed files on build you can use this option to automatically delete them from the deployment branch with each deploy. This option can be toggled on by setting it to `true`. | `with` | **No** |
| `CLEAN_EXCLUDE` | If you need to use `CLEAN` but you'd like to preserve certain files or folders you can use this option. This should be formatted as an array but stored as a string. For example: `'["filename.js", "folder"]'` | `with` | **No** |
| `SINGLE_COMMIT` | This option can be toggled to `true` if you'd prefer to have a single commit on the deployment branch instead of maintaining the full history. Using this option will also cause any existing history to be wiped from the deployment branch. | `with` | **No** |
| `WORKSPACE` | This should point to where your project lives on the virtual machine. The GitHub Actions environment will set this for you. It is only neccersary to set this variable if you're using the node module. | `with` | **No** |
With the action correctly configured you should see the workflow trigger the deployment under the configured conditions.
@ -303,7 +304,7 @@ If you use a [container](https://help.github.com/en/actions/automating-your-work
### Additional Build Files 📁
This action maintains the full git history of the deployment branch. Therefore if you're using a custom domain and require a `CNAME` file, or if you require the use of a `.nojekyll` file, you can safely commit these files directly into deployment branch without them being overridden after each deployment.
If you're using a custom domain and require a `CNAME` file, or if you require the use of a `.nojekyll` file, you can safely commit these files directly into deployment branch without them being overridden after each deployment.
---

View File

@ -319,6 +319,24 @@ describe('git', () => {
expect(execute).toBeCalledTimes(12)
})
it('should execute commands with single commit toggled', async () => {
Object.assign(action, {
folder: 'build',
branch: 'branch',
gitHubToken: '123',
singleCommit: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
await deploy(action)
// Includes the call to generateBranch
expect(execute).toBeCalledTimes(18)
})
it('should execute commands with clean options, ommits sha commit message', async () => {
process.env.GITHUB_SHA = ''
Object.assign(action, {

View File

@ -63,3 +63,7 @@ inputs:
WORKSPACE:
description: "This should point to where your project lives on the virtual machine. The GitHub Actions environment will set this for you. It is only neccersary to set this variable if you're using the node module."
required: false
SINGLE_COMMIT:
description: "This option can be used if you'd prefer to have a single commit on the deployment branch instead of maintaining the full history."
required: false

View File

@ -36,6 +36,8 @@ export interface ActionInterface {
repositoryPath?: string
/** The root directory where your project lives. */
root?: string
/** Wipes the commit history from the deployment branch in favor of a single commit. */
singleCommit?: boolean | null
/** Set to true if you're using an ssh client in your build step. */
ssh?: boolean | null
/** If you'd like to push the contents of the deployment folder into a specific directory on the deployment branch you can specify it here. */
@ -61,9 +63,6 @@ export const action: ActionInterface = {
isTest: process.env.UNIT_TEST
? process.env.UNIT_TEST.toLowerCase() === 'true'
: false,
ssh: !isNullOrUndefined(getInput('SSH'))
? getInput('SSH').toLowerCase() === 'true'
: false,
email: !isNullOrUndefined(getInput('GIT_CONFIG_EMAIL'))
? getInput('GIT_CONFIG_EMAIL')
: pusher && pusher.email
@ -85,6 +84,12 @@ export const action: ActionInterface = {
? repository.full_name
: process.env.GITHUB_REPOSITORY,
root: '.',
singleCommit: !isNullOrUndefined(getInput('SINGLE_COMMIT'))
? getInput('SINGLE_COMMIT').toLowerCase() === 'true'
: false,
ssh: !isNullOrUndefined(getInput('SSH'))
? getInput('SSH').toLowerCase() === 'true'
: false,
targetFolder: getInput('TARGET_FOLDER'),
workspace: process.env.GITHUB_WORKSPACE || ''
}

View File

@ -1,4 +1,4 @@
import {debug, isDebug} from '@actions/core'
import {isDebug} from '@actions/core'
import {exec} from '@actions/exec'
let output: string
@ -21,7 +21,7 @@ export async function execute(cmd: string, cwd: string): Promise<any> {
}
})
return Promise.resolve(debug(output))
return Promise.resolve(output)
}
export function stdout(data: any): string | void {

View File

@ -94,11 +94,18 @@ export async function generateBranch(action: ActionInterface): Promise<void> {
export async function deploy(action: ActionInterface): Promise<void> {
const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder'
const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch'
info('Starting to commit changes…')
try {
hasRequiredParameters(action)
const commitMessage = `${
!isNullOrUndefined(action.commitMessage)
? action.commitMessage
: `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.
@ -180,13 +187,7 @@ export async function deploy(action: ActionInterface): Promise<void> {
`${action.workspace}/${temporaryDeploymentDirectory}`
)
await execute(
`git commit -m "${
!isNullOrUndefined(action.commitMessage)
? action.commitMessage
: `Deploying to ${action.branch} from ${action.baseBranch}`
} ${
process.env.GITHUB_SHA ? `@ ${process.env.GITHUB_SHA}` : ''
} 🚀" --quiet`,
`git commit -m "${commitMessage}" --quiet`,
`${action.workspace}/${temporaryDeploymentDirectory}`
)
await execute(
@ -198,6 +199,33 @@ export async function deploy(action: ActionInterface): Promise<void> {
// Cleans up temporary files/folders and restores the git state.
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