Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add cleanup_work_dir option (#697, #686) #795

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -106,6 +106,7 @@ Note that the `GITHUB_TOKEN` that is created by the runner might not inherently
- [⭐️ Set Git username and email](#%EF%B8%8F-set-git-username-and-email)
- [⭐️ Set custom commit message](#%EF%B8%8F-set-custom-commit-message)
- [⭐️ Create Git tag](#%EF%B8%8F-create-git-tag)
- [⭐️ Clean up a working directory `cleanup_work_dir`](#%EF%B8%8F-clean-up-a-working-directory-cleanup_work_dir)
- [Tips and FAQ](#tips-and-faq)
- [⭐️ Create SSH Deploy Key](#%EF%B8%8F-create-ssh-deploy-key)
- [⭐️ First Deployment with `GITHUB_TOKEN`](#%EF%B8%8F-first-deployment-with-github_token)
Expand Down Expand Up @@ -529,6 +530,19 @@ deploy-v1.2.3 # Tag on the gh-pages branch
v1.2.3 # Tag on the main branch
```

### ⭐️ Clean up a working directory `cleanup_work_dir`
We can set the `cleanup_work_dir: true` option.
This allows you to delete a working directory (`$HOME/actions_github_pages_{unixTime}`).

```yaml
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
cleanup_work_dir: true
```

<div align="right">
<a href="#table-of-contents">Back to TOC ☝️</a>
</div>
Expand Down
6 changes: 5 additions & 1 deletion __tests__/get-inputs.test.ts
Expand Up @@ -56,6 +56,7 @@ function getInputsLog(authMethod: string, inps: Inputs): string {
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
[INFO] CNAME: ${inps.CNAME}
[INFO] ExcludeAssets ${inps.ExcludeAssets}
[INFO] CleanupWorkDir: ${inps.CleanupWorkDir}
`;
}

Expand Down Expand Up @@ -125,6 +126,7 @@ describe('getInputs()', () => {
expect(inps.DisableNoJekyll).toBe(false);
expect(inps.CNAME).toMatch('');
expect(inps.ExcludeAssets).toMatch('.github');
expect(inps.CleanupWorkDir).toBe(false);
});

test('get spec inputs', () => {
Expand All @@ -147,6 +149,7 @@ describe('getInputs()', () => {
process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
process.env['INPUT_CNAME'] = 'github.com';
process.env['INPUT_EXCLUDE_ASSETS'] = '.github';
process.env['INPUT_CLEANUP_WORK_DIR'] = 'false';

const inps: Inputs = getInputs();

Expand All @@ -169,6 +172,7 @@ describe('getInputs()', () => {
expect(inps.DisableNoJekyll).toBe(true);
expect(inps.CNAME).toMatch('github.com');
expect(inps.ExcludeAssets).toMatch('.github');
expect(inps.CleanupWorkDir).toBe(false);
});

test('get spec inputs enable_jekyll', () => {
Expand All @@ -184,6 +188,6 @@ describe('getInputs()', () => {

expect(() => {
getInputs();
}).toThrowError('Use either of enable_jekyll or disable_nojekyll');
}).toThrow('Use either of enable_jekyll or disable_nojekyll');
eholic marked this conversation as resolved.
Show resolved Hide resolved
});
});
12 changes: 12 additions & 0 deletions __tests__/utils.test.ts
Expand Up @@ -4,6 +4,7 @@ import {
getHomeDir,
getWorkDirName,
createDir,
deleteDir,
addNoJekyll,
addCNAME,
skipOnFork
Expand Down Expand Up @@ -61,6 +62,17 @@ describe('createDir()', () => {
});
});

describe('deleteDir()', () => {
test('delete a directory', async () => {
const unixTime = await getTime();
const workDirName = await getWorkDirName(`${unixTime}`);
await createDir(workDirName);
await deleteDir(workDirName);
const test = fs.existsSync(workDirName);
expect(test).toBe(false);
});
});

async function getWorkDir(): Promise<string> {
const unixTime = await getTime();
let workDir = '';
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Expand Up @@ -77,3 +77,7 @@ inputs:
description: 'Set files or directories to exclude from a publish directory.'
required: false
default: '.github'
cleanup_work_dir:
description: 'Clean up a working directory.'
required: false
default: 'false'
4 changes: 3 additions & 1 deletion src/get-inputs.ts
Expand Up @@ -29,6 +29,7 @@ export function showInputs(inps: Inputs): void {
[INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll}
[INFO] CNAME: ${inps.CNAME}
[INFO] ExcludeAssets ${inps.ExcludeAssets}
[INFO] CleanupWorkDir: ${inps.CleanupWorkDir}
`);
}

Expand Down Expand Up @@ -67,7 +68,8 @@ export function getInputs(): Inputs {
TagMessage: core.getInput('tag_message'),
DisableNoJekyll: useBuiltinJekyll,
CNAME: core.getInput('cname'),
ExcludeAssets: core.getInput('exclude_assets')
ExcludeAssets: core.getInput('exclude_assets'),
CleanupWorkDir: isBoolean(core.getInput('cleanup_work_dir'))
};

return inps;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Expand Up @@ -18,6 +18,7 @@ export interface Inputs {
readonly DisableNoJekyll: boolean;
readonly CNAME: string;
readonly ExcludeAssets: string;
readonly CleanupWorkDir: boolean;
}

export interface CmdResult {
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Expand Up @@ -6,7 +6,7 @@ import {Inputs} from './interfaces';
import {showInputs, getInputs} from './get-inputs';
import {setTokens} from './set-tokens';
import {setRepo, setCommitAuthor, getCommitMessage, commit, push, pushTag} from './git-utils';
import {getWorkDirName, addNoJekyll, addCNAME, skipOnFork} from './utils';
import {getWorkDirName, addNoJekyll, addCNAME, skipOnFork, deleteDir} from './utils';

export async function run(): Promise<void> {
try {
Expand Down Expand Up @@ -83,6 +83,12 @@ export async function run(): Promise<void> {
await pushTag(inps.TagName, inps.TagMessage);
core.endGroup();

if (inps.CleanupWorkDir) {
core.startGroup('Clean up working directory');
await deleteDir(workDir);
core.endGroup();
}

core.info('[INFO] Action successfully completed');

return;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Expand Up @@ -29,6 +29,14 @@ export async function createDir(dirPath: string): Promise<void> {
return;
}

export async function deleteDir(dirPath: string): Promise<void> {
if (fs.existsSync(dirPath)) {
await io.rmRF(dirPath);
core.debug(`Deleted directory ${dirPath}`);
return;
}
}

export async function addNoJekyll(workDir: string, DisableNoJekyll: boolean): Promise<void> {
if (DisableNoJekyll) {
return;
Expand Down