Stash / Remote Changes (#435)

* Preservation Changes

* Update git.ts

* Makes adjustments
This commit is contained in:
James Ives 2020-09-27 14:37:43 -04:00 committed by GitHub
parent b8efd660d9
commit 8912f4fdb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 1 deletions

View File

@ -151,6 +151,7 @@ In addition to the deployment options you must also configure the following.
| `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** | | `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** | | `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** |
| `LFS` | If toggled all files will be migrated from [Git LFS](https://git-lfs.github.com/) so they can be comitted to the deployment branch. | `with` | **No** | | `LFS` | If toggled all files will be migrated from [Git LFS](https://git-lfs.github.com/) so they can be comitted to the deployment branch. | `with` | **No** |
| `PRESERVE` | Preserves and restores the workspace prior to deployment. This option is useful if you're modifying files in the worktree that aren't comitted to Git. | `with` | **No** |
| `SILENT` | Silences the action output preventing it from displaying git messages. | `with` | **No** | | `SILENT` | Silences the action output preventing it from displaying git messages. | `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 necessary to set this variable if you're using the node module. | `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 necessary to set this variable if you're using the node module. | `with` | **No** |

View File

@ -241,6 +241,26 @@ describe('git', () => {
expect(execute).toBeCalledTimes(6) expect(execute).toBeCalledTimes(6)
}) })
it('should stash changes if preserve is true', async () => {
Object.assign(action, {
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
accessToken: '123',
branch: 'branch',
folder: '.',
preserve: true,
isTest: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
await init(action)
expect(execute).toBeCalledTimes(7)
})
}) })
describe('generateBranch', () => { describe('generateBranch', () => {
@ -364,6 +384,29 @@ describe('git', () => {
expect(response).toBe(Status.SUCCESS) expect(response).toBe(Status.SUCCESS)
}) })
it('should execute stash apply commands if preserve is true', async () => {
Object.assign(action, {
silent: false,
folder: 'assets',
branch: 'branch',
gitHubToken: '123',
lfs: true,
preserve: true,
isTest: true,
pusher: {
name: 'asd',
email: 'as@cat'
}
})
const response = await deploy(action)
// Includes the call to generateBranch
expect(execute).toBeCalledTimes(14)
expect(rmRF).toBeCalledTimes(1)
expect(response).toBe(Status.SUCCESS)
})
it('should not ignore CNAME or nojekyll if they exist in the deployment folder', async () => { it('should not ignore CNAME or nojekyll if they exist in the deployment folder', async () => {
Object.assign(action, { Object.assign(action, {
silent: false, silent: false,

View File

@ -77,6 +77,10 @@ inputs:
description: "Silences the action output preventing it from displaying git messages." description: "Silences the action output preventing it from displaying git messages."
required: false required: false
PRESERVE:
description: "Preserves and restores any workspace changes prior to deployment."
required: false
outputs: outputs:
DEPLOYMENT_STATUS: DEPLOYMENT_STATUS:
description: 'The status of the deployment that indicates if the run failed or passed. Possible outputs include: success|failed|skipped' description: 'The status of the deployment that indicates if the run failed or passed. Possible outputs include: success|failed|skipped'

View File

@ -32,6 +32,8 @@ export interface ActionInterface {
lfs?: boolean | null lfs?: boolean | null
/** The git config name. */ /** The git config name. */
name?: string name?: string
/** Determines if the workspace should be stashed/restored prior to comitting. */
preserve?: boolean | null
/** The repository path, for example JamesIves/github-pages-deploy-action. */ /** The repository path, for example JamesIves/github-pages-deploy-action. */
repositoryName?: string repositoryName?: string
/** The fully qualified repositpory path, this gets auto generated if repositoryName is provided. */ /** The fully qualified repositpory path, this gets auto generated if repositoryName is provided. */
@ -85,6 +87,9 @@ export const action: ActionInterface = {
: process.env.GITHUB_ACTOR : process.env.GITHUB_ACTOR
? process.env.GITHUB_ACTOR ? process.env.GITHUB_ACTOR
: 'GitHub Pages Deploy Action', : 'GitHub Pages Deploy Action',
preserve: !isNullOrUndefined(getInput('PRESERVE'))
? getInput('PRESERVE').toLowerCase() === 'true'
: false,
repositoryName: !isNullOrUndefined(getInput('REPOSITORY_NAME')) repositoryName: !isNullOrUndefined(getInput('REPOSITORY_NAME'))
? getInput('REPOSITORY_NAME') ? getInput('REPOSITORY_NAME')
: repository && repository.full_name : repository && repository.full_name

View File

@ -29,12 +29,25 @@ export async function init(action: ActionInterface): Promise<void | Error> {
action.silent action.silent
) )
await execute(`git remote rm origin`, action.workspace, action.silent) try {
await execute(`git remote rm origin`, action.workspace, action.silent)
} finally {
if (action.isTest) {
info('Attempted to remove origin…')
}
}
await execute( await execute(
`git remote add origin ${action.repositoryPath}`, `git remote add origin ${action.repositoryPath}`,
action.workspace, action.workspace,
action.silent action.silent
) )
if (action.preserve) {
info(`Stashing workspace changes… ⬆️`)
await execute(`git stash`, action.workspace, action.silent)
}
await execute( await execute(
`git fetch --no-recurse-submodules`, `git fetch --no-recurse-submodules`,
action.workspace, action.workspace,
@ -165,6 +178,18 @@ export async function deploy(action: ActionInterface): Promise<Status> {
) )
} }
if (action.preserve) {
info(`Applying stashed workspace changes… ⬆️`)
try {
await execute(`git stash apply`, action.workspace, action.silent)
} finally {
if (action.isTest) {
info('Attempted to apply stash…')
}
}
}
await execute( await execute(
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`, `git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
action.workspace, action.workspace,