feat: Add disable_nojekyll and cname options

- Add .nojekyll file by default for only the master and gh-pages branches. When the file already exists, this action does nothing.
- When we set other branches to publish_dir, this action does not add .nojekyll file.

cf. #112

Co-authored-by: Daniel Himmelstein <daniel.himmelstein@gmail.com>
Co-authored-by: Nicolas Vanhoren <nicolas.vanhoren@gmail.com>
This commit is contained in:
peaceiris 2020-02-20 16:10:11 +00:00
parent 0b1dd44709
commit 7c4b591cf6
9 changed files with 243 additions and 23 deletions

View File

@ -21,6 +21,8 @@ afterEach(() => {
delete process.env['INPUT_COMMIT_MESSAGE']; delete process.env['INPUT_COMMIT_MESSAGE'];
delete process.env['INPUT_TAG_NAME']; delete process.env['INPUT_TAG_NAME'];
delete process.env['INPUT_TAG_MESSAGE']; delete process.env['INPUT_TAG_MESSAGE'];
delete process.env['INPUT_DISABLE_NOJEKYLL'];
delete process.env['INPUT_CNAME'];
}); });
describe('getInputs()', () => { describe('getInputs()', () => {
@ -30,15 +32,6 @@ describe('getInputs()', () => {
// process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token'; // process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token';
process.env['INPUT_PUBLISH_BRANCH'] = 'gh-pages'; process.env['INPUT_PUBLISH_BRANCH'] = 'gh-pages';
process.env['INPUT_PUBLISH_DIR'] = 'public'; process.env['INPUT_PUBLISH_DIR'] = 'public';
// 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'] = 'github@github.com';
// process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
// process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
// process.env['INPUT_TAG_MESSAGE'] = 'Deployment v1.2.3';
const inps: Inputs = getInputs(); const inps: Inputs = getInputs();
@ -56,6 +49,8 @@ describe('getInputs()', () => {
expect(inps.CommitMessage).toMatch(''); expect(inps.CommitMessage).toMatch('');
expect(inps.TagName).toMatch(''); expect(inps.TagName).toMatch('');
expect(inps.TagMessage).toMatch(''); expect(inps.TagMessage).toMatch('');
expect(inps.DisableNoJekyll).toBe(false);
expect(inps.CNAME).toMatch('');
}); });
test('get spec inputs', () => { test('get spec inputs', () => {
@ -73,6 +68,8 @@ describe('getInputs()', () => {
process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature'; process.env['INPUT_COMMIT_MESSAGE'] = 'feat: Add new feature';
process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3'; process.env['INPUT_TAG_NAME'] = 'deploy-v1.2.3';
process.env['INPUT_TAG_MESSAGE'] = 'Deployment 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 inps: Inputs = getInputs();
@ -90,5 +87,7 @@ describe('getInputs()', () => {
expect(inps.CommitMessage).toMatch('feat: Add new feature'); expect(inps.CommitMessage).toMatch('feat: Add new feature');
expect(inps.TagName).toMatch('deploy-v1.2.3'); expect(inps.TagName).toMatch('deploy-v1.2.3');
expect(inps.TagMessage).toMatch('Deployment v1.2.3'); expect(inps.TagMessage).toMatch('Deployment v1.2.3');
expect(inps.DisableNoJekyll).toBe(true);
expect(inps.CNAME).toMatch('github.com');
}); });
}); });

160
__tests__/utils.test.ts Normal file
View File

@ -0,0 +1,160 @@
import path from 'path';
import fs from 'fs';
import {
getHomeDir,
getWorkDirName,
createWorkDir,
addNoJekyll,
addCNAME
} from '../src/utils';
beforeEach(() => {
jest.resetModules();
});
// afterEach(() => {
// });
describe('getHomeDir()', () => {
test('get home directory name', async () => {
let test = '';
if (process.platform === 'win32') {
test = 'C:\\Users\\runneradmin';
} else {
test = `${process.env.HOME}`;
}
const homeDir = await getHomeDir();
expect(homeDir).toMatch(test);
});
});
async function getWorkDir(): Promise<string> {
const date = new Date();
const unixTime = date.getTime();
let workDir = '';
workDir = await getWorkDirName(`${unixTime}`);
await createWorkDir(workDir);
return workDir;
}
describe('addNoJekyll()', () => {
test('add .nojekyll gh-pages', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
await addNoJekyll(workDir, false, 'gh-pages');
const test1 = fs.existsSync(filepath);
expect(test1).toBe(true);
fs.unlinkSync(filepath);
});
test('add .nojekyll master', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
await addNoJekyll(workDir, false, 'master');
const test2 = fs.existsSync(filepath);
expect(test2).toBe(true);
fs.unlinkSync(filepath);
});
test('not add .nojekyll disable_nojekyll gh-pages', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
await addNoJekyll(workDir, true, 'gh-pages');
const test3 = fs.existsSync(filepath);
expect(test3).toBe(false);
});
test('not add .nojekyll disable_nojekyll master', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
await addNoJekyll(workDir, true, 'master');
const test4 = fs.existsSync(filepath);
expect(test4).toBe(false);
});
test('not add .nojekyll other-branch', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
await addNoJekyll(workDir, false, 'other-branch');
const test5 = fs.existsSync(filepath);
expect(test5).toBe(false);
});
test('not add .nojekyll disable_nojekyll other-branch', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, '.nojekyll');
await addNoJekyll(workDir, true, 'other-branch');
const test6 = fs.existsSync(filepath);
expect(test6).toBe(false);
});
});
describe('addCNAME()', () => {
test('add CNAME', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, 'CNAME');
await addCNAME(workDir, 'github.com');
const test1 = fs.readFileSync(filepath, 'utf8');
expect(test1).toMatch('github.com');
fs.unlinkSync(filepath);
});
test('do nothing', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, 'CNAME');
await addCNAME(workDir, '');
const test2 = fs.existsSync(filepath);
expect(test2).toBe(false);
});
test('CNAME already exists', async () => {
let workDir = '';
(async (): Promise<void> => {
workDir = await getWorkDir();
})();
const filepath = path.join(workDir, 'CNAME');
await addCNAME(workDir, 'github.io');
await addCNAME(workDir, 'github.com');
const test3 = fs.readFileSync(filepath, 'utf8');
expect(test3).toMatch('github.io');
fs.unlinkSync(filepath);
});
});

View File

@ -55,3 +55,10 @@ inputs:
tag_message: tag_message:
description: 'Set tag message' description: 'Set tag message'
required: false required: false
disable_nojekyll:
description: 'Disable adding .nojekyll file to master or gh-pages branches'
required: false
default: 'false'
cname:
description: 'Set custom domain'
required: false

View File

@ -21,6 +21,8 @@ function showInputs(inps: Inputs): void {
core.info(`[INFO] CommitMessage: ${inps.CommitMessage}`); core.info(`[INFO] CommitMessage: ${inps.CommitMessage}`);
core.info(`[INFO] TagName: ${inps.TagName}`); core.info(`[INFO] TagName: ${inps.TagName}`);
core.info(`[INFO] TagMessage: ${inps.TagMessage}`); core.info(`[INFO] TagMessage: ${inps.TagMessage}`);
core.info(`[INFO] DisableNoJekyll: ${inps.DisableNoJekyll}`);
core.info(`[INFO] CNAME: ${inps.CNAME}`);
} }
export function getInputs(): Inputs { export function getInputs(): Inputs {
@ -41,7 +43,10 @@ export function getInputs(): Inputs {
UserEmail: core.getInput('user_email'), UserEmail: core.getInput('user_email'),
CommitMessage: core.getInput('commit_message'), CommitMessage: core.getInput('commit_message'),
TagName: core.getInput('tag_name'), TagName: core.getInput('tag_name'),
TagMessage: core.getInput('tag_message') TagMessage: core.getInput('tag_message'),
DisableNoJekyll:
(core.getInput('disable_nojekyll') || 'false').toUpperCase() === 'TRUE',
CNAME: core.getInput('cname')
}; };
showInputs(inps); showInputs(inps);

View File

@ -5,13 +5,7 @@ import * as io from '@actions/io';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
import {Inputs, CmdResult} from './interfaces'; import {Inputs, CmdResult} from './interfaces';
import {getHomeDir} from './utils'; import {createWorkDir} from './utils';
export async function createWorkDir(workDirName: string): Promise<void> {
await io.mkdirP(workDirName);
core.debug(`workDir was created: ${workDirName}`);
return;
}
export async function createBranchForce(branch: string): Promise<void> { export async function createBranchForce(branch: string): Promise<void> {
await exec.exec('git', ['init']); await exec.exec('git', ['init']);
@ -41,9 +35,8 @@ export async function copyAssets(
export async function setRepo( export async function setRepo(
inps: Inputs, inps: Inputs,
remoteURL: string, remoteURL: string,
unixTime: string workDir: string
): Promise<void> { ): Promise<void> {
const workDir = path.join(getHomeDir(), `actions_github_pages_${unixTime}`);
const publishDir = path.join( const publishDir = path.join(
`${process.env.GITHUB_WORKSPACE}`, `${process.env.GITHUB_WORKSPACE}`,
inps.PublishDir inps.PublishDir

View File

@ -13,6 +13,8 @@ export interface Inputs {
readonly CommitMessage: string; readonly CommitMessage: string;
readonly TagName: string; readonly TagName: string;
readonly TagMessage: string; readonly TagMessage: string;
readonly DisableNoJekyll: boolean;
readonly CNAME: string;
} }
export interface CmdResult { export interface CmdResult {

View File

@ -4,6 +4,7 @@ import {Inputs} from './interfaces';
import {getInputs} from './get-inputs'; import {getInputs} from './get-inputs';
import {setTokens} from './set-tokens'; import {setTokens} from './set-tokens';
import * as git from './git-utils'; import * as git from './git-utils';
import {getWorkDirName, addNoJekyll, addCNAME} from './utils';
export async function run(): Promise<void> { export async function run(): Promise<void> {
try { try {
@ -16,12 +17,16 @@ export async function run(): Promise<void> {
const date = new Date(); const date = new Date();
const unixTime = date.getTime(); const unixTime = date.getTime();
await git.setRepo(inps, remoteURL, `${unixTime}`); const workDir = await getWorkDirName(`${unixTime}`);
await git.setRepo(inps, remoteURL, workDir);
await addNoJekyll(workDir, inps.DisableNoJekyll, inps.PublishBranch);
await addCNAME(workDir, inps.CNAME);
try { try {
await exec.exec('git', ['remote', 'rm', 'origin']); await exec.exec('git', ['remote', 'rm', 'origin']);
} catch (e) { } catch (e) {
core.info(`[INFO] e`); core.info(`[INFO] ${e}`);
} }
await exec.exec('git', ['remote', 'add', 'origin', remoteURL]); await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
await exec.exec('git', ['add', '--all']); await exec.exec('git', ['add', '--all']);

View File

@ -22,7 +22,7 @@ export async function setSSHKey(
): Promise<string> { ): Promise<string> {
core.info('[INFO] setup SSH deploy key'); core.info('[INFO] setup SSH deploy key');
const homeDir = getHomeDir(); const homeDir = await getHomeDir();
const sshDir = path.join(homeDir, '.ssh'); const sshDir = path.join(homeDir, '.ssh');
await io.mkdirP(sshDir); await io.mkdirP(sshDir);
await exec.exec('chmod', ['700', sshDir]); await exec.exec('chmod', ['700', sshDir]);

View File

@ -1,6 +1,9 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as io from '@actions/io';
import path from 'path';
import fs from 'fs';
export function getHomeDir(): string { export async function getHomeDir(): Promise<string> {
let homedir = ''; let homedir = '';
if (process.platform === 'win32') { if (process.platform === 'win32') {
@ -13,3 +16,49 @@ export function getHomeDir(): string {
return homedir; return homedir;
} }
export async function getWorkDirName(unixTime: string): Promise<string> {
const homeDir = await getHomeDir();
const workDirName = path.join(homeDir, `actions_github_pages_${unixTime}`);
return workDirName;
}
export async function createWorkDir(workDirName: string): Promise<void> {
await io.mkdirP(workDirName);
core.debug(`Created: ${workDirName}`);
return;
}
export async function addNoJekyll(
workDir: string,
DisableNoJekyll: boolean,
PublishBranch: string
): Promise<void> {
if (DisableNoJekyll) {
return;
}
if (PublishBranch === 'master' || PublishBranch === 'gh-pages') {
const filepath = path.join(workDir, '.nojekyll');
if (fs.existsSync(filepath)) {
return;
}
fs.closeSync(fs.openSync(filepath, 'w'));
core.info(`[INFO] Created ${filepath}`);
}
}
export async function addCNAME(
workDir: string,
content: string
): Promise<void> {
if (content === '') {
return;
}
const filepath = path.join(workDir, 'CNAME');
if (fs.existsSync(filepath)) {
core.warning(`CNAME already exists`);
return;
}
fs.writeFileSync(filepath, content + '\n');
core.info(`[INFO] Created ${filepath}`);
}