Skip to content

Commit

Permalink
feat: Add cleanup_work_dir option
Browse files Browse the repository at this point in the history
  • Loading branch information
eholic committed Oct 26, 2022
1 parent 087a759 commit a1c1689
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
4 changes: 4 additions & 0 deletions __tests__/get-inputs.test.ts
Original file line number Diff line number Diff line change
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 Down
12 changes: 12 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit a1c1689

Please sign in to comment.