Skip to content

Commit 8912f4f

Browse files
authored
Stash / Remote Changes (JamesIves#435)
* Preservation Changes * Update git.ts * Makes adjustments
1 parent b8efd66 commit 8912f4f

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ In addition to the deployment options you must also configure the following.
151151
| `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** |
152152
| `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** |
153153
| `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** |
154+
| `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** |
154155
| `SILENT` | Silences the action output preventing it from displaying git messages. | `with` | **No** |
155156
| `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** |
156157

__tests__/git.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,26 @@ describe('git', () => {
241241

242242
expect(execute).toBeCalledTimes(6)
243243
})
244+
245+
it('should stash changes if preserve is true', async () => {
246+
Object.assign(action, {
247+
silent: false,
248+
repositoryPath: 'JamesIves/github-pages-deploy-action',
249+
accessToken: '123',
250+
branch: 'branch',
251+
folder: '.',
252+
preserve: true,
253+
isTest: true,
254+
pusher: {
255+
name: 'asd',
256+
email: 'as@cat'
257+
}
258+
})
259+
260+
await init(action)
261+
262+
expect(execute).toBeCalledTimes(7)
263+
})
244264
})
245265

246266
describe('generateBranch', () => {
@@ -364,6 +384,29 @@ describe('git', () => {
364384
expect(response).toBe(Status.SUCCESS)
365385
})
366386

387+
it('should execute stash apply commands if preserve is true', async () => {
388+
Object.assign(action, {
389+
silent: false,
390+
folder: 'assets',
391+
branch: 'branch',
392+
gitHubToken: '123',
393+
lfs: true,
394+
preserve: true,
395+
isTest: true,
396+
pusher: {
397+
name: 'asd',
398+
email: 'as@cat'
399+
}
400+
})
401+
402+
const response = await deploy(action)
403+
404+
// Includes the call to generateBranch
405+
expect(execute).toBeCalledTimes(14)
406+
expect(rmRF).toBeCalledTimes(1)
407+
expect(response).toBe(Status.SUCCESS)
408+
})
409+
367410
it('should not ignore CNAME or nojekyll if they exist in the deployment folder', async () => {
368411
Object.assign(action, {
369412
silent: false,

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ inputs:
7777
description: "Silences the action output preventing it from displaying git messages."
7878
required: false
7979

80+
PRESERVE:
81+
description: "Preserves and restores any workspace changes prior to deployment."
82+
required: false
83+
8084
outputs:
8185
DEPLOYMENT_STATUS:
8286
description: 'The status of the deployment that indicates if the run failed or passed. Possible outputs include: success|failed|skipped'

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface ActionInterface {
3232
lfs?: boolean | null
3333
/** The git config name. */
3434
name?: string
35+
/** Determines if the workspace should be stashed/restored prior to comitting. */
36+
preserve?: boolean | null
3537
/** The repository path, for example JamesIves/github-pages-deploy-action. */
3638
repositoryName?: string
3739
/** The fully qualified repositpory path, this gets auto generated if repositoryName is provided. */
@@ -85,6 +87,9 @@ export const action: ActionInterface = {
8587
: process.env.GITHUB_ACTOR
8688
? process.env.GITHUB_ACTOR
8789
: 'GitHub Pages Deploy Action',
90+
preserve: !isNullOrUndefined(getInput('PRESERVE'))
91+
? getInput('PRESERVE').toLowerCase() === 'true'
92+
: false,
8893
repositoryName: !isNullOrUndefined(getInput('REPOSITORY_NAME'))
8994
? getInput('REPOSITORY_NAME')
9095
: repository && repository.full_name

src/git.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,25 @@ export async function init(action: ActionInterface): Promise<void | Error> {
2929
action.silent
3030
)
3131

32-
await execute(`git remote rm origin`, action.workspace, action.silent)
32+
try {
33+
await execute(`git remote rm origin`, action.workspace, action.silent)
34+
} finally {
35+
if (action.isTest) {
36+
info('Attempted to remove origin…')
37+
}
38+
}
39+
3340
await execute(
3441
`git remote add origin ${action.repositoryPath}`,
3542
action.workspace,
3643
action.silent
3744
)
45+
46+
if (action.preserve) {
47+
info(`Stashing workspace changes… ⬆️`)
48+
await execute(`git stash`, action.workspace, action.silent)
49+
}
50+
3851
await execute(
3952
`git fetch --no-recurse-submodules`,
4053
action.workspace,
@@ -165,6 +178,18 @@ export async function deploy(action: ActionInterface): Promise<Status> {
165178
)
166179
}
167180

181+
if (action.preserve) {
182+
info(`Applying stashed workspace changes… ⬆️`)
183+
184+
try {
185+
await execute(`git stash apply`, action.workspace, action.silent)
186+
} finally {
187+
if (action.isTest) {
188+
info('Attempted to apply stash…')
189+
}
190+
}
191+
}
192+
168193
await execute(
169194
`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`,
170195
action.workspace,

0 commit comments

Comments
 (0)