From 8912f4fdb3401df34200010116d4de0394db02e8 Mon Sep 17 00:00:00 2001 From: James Ives Date: Sun, 27 Sep 2020 14:37:43 -0400 Subject: [PATCH] Stash / Remote Changes (#435) * Preservation Changes * Update git.ts * Makes adjustments --- README.md | 1 + __tests__/git.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ action.yml | 4 ++++ src/constants.ts | 5 +++++ src/git.ts | 27 ++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 978fb844..b25465c1 100644 --- a/README.md +++ b/README.md @@ -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** | | `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** | +| `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** | | `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** | diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index f0eec7e2..0f2a0a41 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -241,6 +241,26 @@ describe('git', () => { 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', () => { @@ -364,6 +384,29 @@ describe('git', () => { 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 () => { Object.assign(action, { silent: false, diff --git a/action.yml b/action.yml index 373a0624..e2d30ae9 100644 --- a/action.yml +++ b/action.yml @@ -77,6 +77,10 @@ inputs: description: "Silences the action output preventing it from displaying git messages." required: false + PRESERVE: + description: "Preserves and restores any workspace changes prior to deployment." + required: false + outputs: DEPLOYMENT_STATUS: description: 'The status of the deployment that indicates if the run failed or passed. Possible outputs include: success|failed|skipped' diff --git a/src/constants.ts b/src/constants.ts index 2ee97de5..46eaaf5d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -32,6 +32,8 @@ export interface ActionInterface { lfs?: boolean | null /** The git config name. */ 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. */ repositoryName?: string /** 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 : 'GitHub Pages Deploy Action', + preserve: !isNullOrUndefined(getInput('PRESERVE')) + ? getInput('PRESERVE').toLowerCase() === 'true' + : false, repositoryName: !isNullOrUndefined(getInput('REPOSITORY_NAME')) ? getInput('REPOSITORY_NAME') : repository && repository.full_name diff --git a/src/git.ts b/src/git.ts index a8c13ac5..05853bac 100644 --- a/src/git.ts +++ b/src/git.ts @@ -29,12 +29,25 @@ export async function init(action: ActionInterface): Promise { 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( `git remote add origin ${action.repositoryPath}`, action.workspace, action.silent ) + + if (action.preserve) { + info(`Stashing workspace changes… ⬆️`) + await execute(`git stash`, action.workspace, action.silent) + } + await execute( `git fetch --no-recurse-submodules`, action.workspace, @@ -165,6 +178,18 @@ export async function deploy(action: ActionInterface): Promise { ) } + 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( `git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`, action.workspace,