Skip to content

Commit

Permalink
feat: Add destination_dir option (#403)
Browse files Browse the repository at this point in the history
Related to #324 #390
  • Loading branch information
peaceiris authored Jul 21, 2020
1 parent 0cb61e9 commit f30118c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 20 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ All Actions runners: Linux (Ubuntu), macOS, and Windows are supported.
- [⭐️ `personal_token`](#%EF%B8%8F-personal_token)
- [⭐️ `publish_branch`](#%EF%B8%8F-publish_branch)
- [⭐️ `publish_dir`](#%EF%B8%8F-publish_dir)
- [⭐️ `destination_dir`](#%EF%B8%8F-destination_dir)
- [⭐️ CNAME](#%EF%B8%8F-cname)
- [⭐️ Enable Built-in Jekyll](#%EF%B8%8F-enable-built-in-jekyll)
- [⭐️ Allow empty commits](#%EF%B8%8F-allow-empty-commits)
Expand Down Expand Up @@ -255,7 +256,7 @@ A target branch to deploy to GitHub Pages. The default is `gh-pages`.

### ⭐️ `publish_dir`

A target directory to deploy to GitHub Pages. The default is `public`.
A source directory to deploy to GitHub Pages. The default is `public`.

```yaml
- name: Deploy
Expand All @@ -265,6 +266,18 @@ A target directory to deploy to GitHub Pages. The default is `public`.
publish_dir: ./out # default: public
```

### ⭐️ `destination_dir`

A destination subdirectory on a publishing branch. The default is empty.

```yaml
- name: Deploy
uses: peaceiris/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
destination_dir: subdir
```

### ⭐️ CNAME

To add `CNAME` file, we can set the `cname` option.
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 @@ -40,6 +40,7 @@ function getInputsLog(authMethod: string, inps: Inputs): string {
[INFO] ${authMethod}: true
[INFO] PublishBranch: ${inps.PublishBranch}
[INFO] PublishDir: ${inps.PublishDir}
[INFO] DestinationDir: ${inps.DestinationDir}
[INFO] ExternalRepository: ${inps.ExternalRepository}
[INFO] AllowEmptyCommit: ${inps.AllowEmptyCommit}
[INFO] KeepFiles: ${inps.KeepFiles}
Expand Down Expand Up @@ -107,6 +108,7 @@ describe('getInputs()', () => {
expect(inps.PersonalToken).toMatch('');
expect(inps.PublishBranch).toMatch('gh-pages');
expect(inps.PublishDir).toMatch('public');
expect(inps.DestinationDir).toMatch('');
expect(inps.ExternalRepository).toMatch('');
expect(inps.AllowEmptyCommit).toBe(false);
expect(inps.KeepFiles).toBe(false);
Expand All @@ -127,6 +129,7 @@ describe('getInputs()', () => {
process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
process.env['INPUT_PUBLISH_BRANCH'] = 'master';
process.env['INPUT_PUBLISH_DIR'] = 'out';
process.env['INPUT_DESTINATION_DIR'] = 'subdir';
process.env['INPUT_EXTERNAL_REPOSITORY'] = 'user/repo';
process.env['INPUT_ALLOW_EMPTY_COMMIT'] = 'true';
process.env['INPUT_KEEP_FILES'] = 'true';
Expand All @@ -147,6 +150,7 @@ describe('getInputs()', () => {
expect(inps.PersonalToken).toMatch('test_personal_token');
expect(inps.PublishBranch).toMatch('master');
expect(inps.PublishDir).toMatch('out');
expect(inps.DestinationDir).toMatch('subdir');
expect(inps.ExternalRepository).toMatch('user/repo');
expect(inps.AllowEmptyCommit).toBe(true);
expect(inps.KeepFiles).toBe(true);
Expand Down
43 changes: 40 additions & 3 deletions __tests__/git-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {getUserName, getUserEmail, setCommitAuthor, getCommitMessage} from '../src/git-utils';
import {getWorkDirName, createWorkDir} from '../src/utils';
import {
setRepo,
getUserName,
getUserEmail,
setCommitAuthor,
getCommitMessage
} from '../src/git-utils';
import {getInputs} from '../src/get-inputs';
import {Inputs} from '../src/interfaces';
import {getWorkDirName, createDir} from '../src/utils';
import {CmdResult} from '../src/interfaces';
import * as exec from '@actions/exec';

Expand All @@ -14,6 +22,35 @@ afterEach(() => {
delete process.env['GITHUB_REPOSITORY'];
});

describe('setRepo()', () => {
test('throw error destination_dir should be a relative path', async () => {
process.env['INPUT_GITHUB_TOKEN'] = 'test_github_token';
process.env['INPUT_PUBLISH_BRANCH'] = 'gh-pages';
process.env['INPUT_PUBLISH_DIR'] = 'public';
process.env['INPUT_DESTINATION_DIR'] = '/subdir';
// process.env['INPUT_EXTERNAL_REPOSITORY'] = 'user/repo';
// process.env['INPUT_ALLOW_EMPTY_COMMIT'] = 'true';
// process.env['INPUT_KEEP_FILES'] = 'true';
// process.env['INPUT_FORCE_ORPHAN'] = 'true';
// process.env['INPUT_USER_NAME'] = 'username';
// process.env['INPUT_USER_EMAIL'] = '[email protected]';
// process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
// process.env['INPUT_FULL_COMMIT_MESSAGE'] = 'feat: Add new feature';
// process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
// process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
// process.env['INPUT_DISABLE_NOJEKYLL'] = 'true';
// process.env['INPUT_CNAME'] = 'github.com';
const inps: Inputs = getInputs();
const remoteURL = 'https://x-access-token:[email protected]/actions/pages.git';
const date = new Date();
const unixTime = date.getTime();
const workDir = await getWorkDirName(`${unixTime}`);
await expect(setRepo(inps, remoteURL, workDir)).rejects.toThrowError(
'destination_dir should be a relative path'
);
});
});

describe('getUserName()', () => {
test('get default git user name', () => {
const userName = '';
Expand Down Expand Up @@ -51,7 +88,7 @@ describe('setCommitAuthor()', () => {
})();

beforeEach(async () => {
await createWorkDir(workDirName);
await createDir(workDirName);
process.chdir(workDirName);
await exec.exec('git', ['init']);
});
Expand Down
10 changes: 5 additions & 5 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import {
getHomeDir,
getWorkDirName,
createWorkDir,
createDir,
addNoJekyll,
addCNAME,
skipOnFork
Expand Down Expand Up @@ -51,11 +51,11 @@ describe('getWorkDirName()', () => {
});
});

describe('createWorkDir()', () => {
test('create work directory', async () => {
describe('createDir()', () => {
test('create a directory', async () => {
const unixTime = await getTime();
const workDirName = await getWorkDirName(`${unixTime}`);
await createWorkDir(workDirName);
await createDir(workDirName);
const test = fs.existsSync(workDirName);
expect(test).toBe(true);
});
Expand All @@ -65,7 +65,7 @@ async function getWorkDir(): Promise<string> {
const unixTime = await getTime();
let workDir = '';
workDir = await getWorkDirName(`${unixTime}`);
await createWorkDir(workDir);
await createDir(workDir);
return workDir;
}

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ inputs:
description: 'Set an input directory for deployment.'
required: false
default: 'public'
destination_dir:
description: 'Set an destination subdirectory for deployment.'
required: false
default: ''
external_repository:
description: 'Set an external repository (owner/repo).'
required: false
Expand Down
2 changes: 2 additions & 0 deletions src/get-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function showInputs(inps: Inputs): void {
[INFO] ${authMethod}: true
[INFO] PublishBranch: ${inps.PublishBranch}
[INFO] PublishDir: ${inps.PublishDir}
[INFO] DestinationDir: ${inps.DestinationDir}
[INFO] ExternalRepository: ${inps.ExternalRepository}
[INFO] AllowEmptyCommit: ${inps.AllowEmptyCommit}
[INFO] KeepFiles: ${inps.KeepFiles}
Expand Down Expand Up @@ -52,6 +53,7 @@ export function getInputs(): Inputs {
PersonalToken: core.getInput('personal_token'),
PublishBranch: core.getInput('publish_branch'),
PublishDir: core.getInput('publish_dir'),
DestinationDir: core.getInput('destination_dir'),
ExternalRepository: core.getInput('external_repository'),
AllowEmptyCommit: (core.getInput('allow_empty_commit') || 'false').toUpperCase() === 'TRUE',
KeepFiles: (core.getInput('keep_files') || 'false').toUpperCase() === 'TRUE',
Expand Down
28 changes: 20 additions & 8 deletions src/git-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import * as io from '@actions/io';
import path from 'path';
import fs from 'fs';
import {Inputs, CmdResult} from './interfaces';
import {createWorkDir} from './utils';
import {createDir} from './utils';

export async function createBranchForce(branch: string): Promise<void> {
await exec.exec('git', ['init']);
await exec.exec('git', ['checkout', '--orphan', branch]);
return;
}

export async function copyAssets(publishDir: string, workDir: string): Promise<void> {
export async function copyAssets(publishDir: string, destDir: string): Promise<void> {
const copyOpts = {recursive: true, force: true};
const files = fs.readdirSync(publishDir);
core.debug(`${files}`);
Expand All @@ -21,7 +21,7 @@ export async function copyAssets(publishDir: string, workDir: string): Promise<v
continue;
}
const filePath = path.join(publishDir, file);
await io.cp(filePath, `${workDir}/`, copyOpts);
await io.cp(filePath, `${destDir}/`, copyOpts);
core.info(`[INFO] copy ${file}`);
}

Expand All @@ -33,12 +33,24 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
? inps.PublishDir
: path.join(`${process.env.GITHUB_WORKSPACE}`, inps.PublishDir);

if (path.isAbsolute(inps.DestinationDir)) {
throw new Error('destination_dir should be a relative path');
}
const destDir = ((): string => {
if (inps.DestinationDir === '') {
return workDir;
} else {
return path.join(workDir, inps.DestinationDir);
}
})();

core.info(`[INFO] ForceOrphan: ${inps.ForceOrphan}`);
if (inps.ForceOrphan) {
await createWorkDir(workDir);
await createDir(destDir);
process.chdir(workDir);
await createBranchForce(inps.PublishBranch);
await copyAssets(publishDir, workDir);
process.chdir(destDir);
await copyAssets(publishDir, destDir);
return;
}

Expand Down Expand Up @@ -68,18 +80,18 @@ export async function setRepo(inps: Inputs, remoteURL: string, workDir: string):
await exec.exec('git', ['rm', '-r', '--ignore-unmatch', '*']);
}

await copyAssets(publishDir, workDir);
await copyAssets(publishDir, destDir);
return;
} else {
throw new Error(`Failed to clone remote branch ${inps.PublishBranch}`);
}
} catch (e) {
core.info(`[INFO] first deployment, create new branch ${inps.PublishBranch}`);
core.info(e.message);
await createWorkDir(workDir);
await createDir(destDir);
process.chdir(workDir);
await createBranchForce(inps.PublishBranch);
await copyAssets(publishDir, workDir);
await copyAssets(publishDir, destDir);
return;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Inputs {
readonly PersonalToken: string;
readonly PublishBranch: string;
readonly PublishDir: string;
readonly DestinationDir: string;
readonly ExternalRepository: string;
readonly AllowEmptyCommit: boolean;
readonly KeepFiles: boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export async function getWorkDirName(unixTime: string): Promise<string> {
return workDirName;
}

export async function createWorkDir(workDirName: string): Promise<void> {
await io.mkdirP(workDirName);
core.debug(`Created: ${workDirName}`);
export async function createDir(dirPath: string): Promise<void> {
await io.mkdirP(dirPath);
core.debug(`Created directory ${dirPath}`);
return;
}

Expand Down

0 comments on commit f30118c

Please sign in to comment.