diff --git a/README.md b/README.md index 40d1706b..7c153aed 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ Below you'll find a description of what each option does. | `BASE_BRANCH` | The base branch of your repository which you'd like to checkout prior to deploying. This defaults to `master`. | `with` | **No** | | `TARGET_FOLDER` | 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. | `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", "./folderpath"]'` | `with` | **No** | With the action correctly configured you should see the workflow trigger the deployment under the configured conditions. diff --git a/lib/constants.js b/lib/constants.js index 34cce3d4..c1d49666 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -34,7 +34,8 @@ exports.action = { ? pusher.email : `${process.env.GITHUB_ACTOR || "github-pages-deploy-action"}@users.noreply.github.com`, - clean: core.getInput("CLEAN") + clean: core.getInput("CLEAN"), + cleanExclude: core.getInput("CLEAN_EXCLUDE") }; // Repository path used for commits/pushes. exports.repositoryPath = `https://${exports.action.accessToken || diff --git a/lib/git.js b/lib/git.js index bda7eb38..83a240d0 100644 --- a/lib/git.js +++ b/lib/git.js @@ -88,13 +88,26 @@ function deploy() { yield util_1.execute(`git checkout ${constants_1.action.baseBranch || "master"}`, constants_1.workspace); yield util_1.execute(`git fetch ${constants_1.repositoryPath}`, constants_1.workspace); yield util_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${constants_1.action.branch}`, constants_1.workspace); + // Ensures that items that need to be excluded from the clean job get parsed. + let excludes = ""; + if (constants_1.action.clean && constants_1.action.cleanExclude) { + try { + const excludedItems = JSON.parse(constants_1.action.cleanExclude); + excludedItems.forEach((item) => (excludes += `--exclude ${item} `)); + } + catch (_a) { + console.log("There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details."); + } + } /* Pushes all of the build files into the deployment directory. Allows the user to specify the root if '.' is provided. rysync is used to prevent file duplication. */ yield util_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${constants_1.action.targetFolder ? `${temporaryDeploymentDirectory}/${constants_1.action.targetFolder}` - : temporaryDeploymentDirectory} ${constants_1.action.clean ? `--delete --exclude CNAME --exclude .nojekyll` : ""} --exclude .git --exclude .github ${constants_1.action.build === constants_1.root ? `--exclude ${temporaryDeploymentDirectory}` : ""}`, constants_1.workspace); + : temporaryDeploymentDirectory} ${constants_1.action.clean + ? `--delete ${excludes} --exclude CNAME --exclude .nojekyll` + : ""} --exclude .git --exclude .github ${constants_1.action.build === constants_1.root ? `--exclude ${temporaryDeploymentDirectory}` : ""}`, constants_1.workspace); const hasFilesToCommit = yield util_1.execute(`git status --porcelain`, temporaryDeploymentDirectory); if (!hasFilesToCommit && !constants_1.isTest) { console.log("There is nothing to commit. Exiting..."); diff --git a/src/constants.ts b/src/constants.ts index 5a39e3fc..68a67116 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -31,7 +31,8 @@ export const action = { ? pusher.email : `${process.env.GITHUB_ACTOR || "github-pages-deploy-action"}@users.noreply.github.com`, - clean: core.getInput("CLEAN") + clean: core.getInput("CLEAN"), + cleanExclude: core.getInput("CLEAN_EXCLUDE") }; // Repository path used for commits/pushes. diff --git a/src/git.ts b/src/git.ts index 5ad6c964..cf6fe4f3 100644 --- a/src/git.ts +++ b/src/git.ts @@ -82,6 +82,21 @@ export async function deploy(): Promise { workspace ); + // Ensures that items that need to be excluded from the clean job get parsed. + let excludes = ""; + if (action.clean && action.cleanExclude) { + try { + const excludedItems = JSON.parse(action.cleanExclude); + excludedItems.forEach( + (item: string) => (excludes += `--exclude ${item} `) + ); + } catch { + console.log( + "There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details." + ); + } + } + /* Pushes all of the build files into the deployment directory. Allows the user to specify the root if '.' is provided. @@ -92,7 +107,9 @@ export async function deploy(): Promise { ? `${temporaryDeploymentDirectory}/${action.targetFolder}` : temporaryDeploymentDirectory } ${ - action.clean ? `--delete --exclude CNAME --exclude .nojekyll` : "" + action.clean + ? `--delete ${excludes} --exclude CNAME --exclude .nojekyll` + : "" } --exclude .git --exclude .github ${ action.build === root ? `--exclude ${temporaryDeploymentDirectory}` : "" }`,