From 00fde1eb97e54d4fa89f31806ff2815878fafcb6 Mon Sep 17 00:00:00 2001 From: Shohei Ueda <30958501+peaceiris@users.noreply.github.com> Date: Mon, 24 Feb 2020 18:49:55 +0900 Subject: [PATCH] feat: Add disable_nojekyll and cname options (#119) * feat: Add disable_nojekyll and cname options * docs: Add cname and disable_nojekyll * chore: Add vim * chore(release): 3.3.0-0 * ci: Add codecov/codecov-action * refactor: Enhance warning message - Add .nojekyll file by default to only the master and gh-pages branches. When the file already exists, this action does nothing. - When we set other branches to publish_branch, this action does not add .nojekyll file. cf. https://github.com/peaceiris/actions-gh-pages/issues/112#issuecomment-589678269 Close #112 Co-authored-by: Daniel Himmelstein Co-authored-by: Nicolas Vanhoren --- .github/workflows/test.yml | 4 + CHANGELOG.md | 18 ++++ Dockerfile | 3 +- README.md | 32 ++++++ __tests__/get-inputs.test.ts | 17 ++-- __tests__/utils.test.ts | 190 +++++++++++++++++++++++++++++++++++ action.yml | 7 ++ package-lock.json | 2 +- package.json | 2 +- src/get-inputs.ts | 7 +- src/git-utils.ts | 11 +- src/interfaces.ts | 2 + src/main.ts | 9 +- src/set-tokens.ts | 2 +- src/utils.ts | 51 +++++++++- 15 files changed, 331 insertions(+), 26 deletions(-) create mode 100644 __tests__/utils.test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f7cc083e9..d533b3e0a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,3 +62,7 @@ jobs: with: name: coverage path: coverage + + - uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 8beb91252..41f205195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +# [3.3.0-0](https://github.com/peaceiris/actions-gh-pages/compare/v3.2.0...v3.3.0-0) (2020-02-20) + + +### chore + +* Add vim ([b9a6975](https://github.com/peaceiris/actions-gh-pages/commit/b9a697582713e95c1d628172933226631d3825af)) + +### deps + +* bump @types/jest from 25.1.2 to 25.1.3 (#118) ([0b1dd44](https://github.com/peaceiris/actions-gh-pages/commit/0b1dd44709acc2ba27cf43e181767b92e78d1577)), closes [#118](https://github.com/peaceiris/actions-gh-pages/issues/118) +* bump ts-jest from 25.2.0 to 25.2.1 (#117) ([e1bf32a](https://github.com/peaceiris/actions-gh-pages/commit/e1bf32ad68fadb9a32147579d6dc49b51d6ddefb)), closes [#117](https://github.com/peaceiris/actions-gh-pages/issues/117) + +### feat + +* Add disable_nojekyll and cname options ([7c4b591](https://github.com/peaceiris/actions-gh-pages/commit/7c4b591cf65cf4daf0464fba89b32ff4e764b427)), closes [#112](https://github.com/peaceiris/actions-gh-pages/issues/112) + + + # [3.2.0](https://github.com/peaceiris/actions-gh-pages/compare/v3.1.0...v3.2.0) (2020-02-19) diff --git a/Dockerfile b/Dockerfile index 228b55a69..4e81d4eb1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,8 @@ RUN apt-get update && \ libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev autoconf \ ca-certificates \ wget \ - ssh && \ + ssh \ + vim && \ rm -rf /var/lib/apt/lists/* WORKDIR /git diff --git a/README.md b/README.md index 5df2ff55e..bf8c402d8 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,8 @@ Three tokens are supported. - [Options](#options) - [⭐️ `personal_token`](#%EF%B8%8F-personal_token) - [⭐️ `github_token`](#%EF%B8%8F-github_token) + - [⭐️ CNAME](#%EF%B8%8F-cname) + - [⭐️ Disable `.nojekyll`](#%EF%B8%8F-disable-nojekyll) - [⭐️ Allow empty commits](#%EF%B8%8F-allow-empty-commits) - [⭐️ Keeping existing files](#%EF%B8%8F-keeping-existing-files) - [⭐️ Deploy to external repository](#%EF%B8%8F-deploy-to-external-repository) @@ -246,6 +248,36 @@ jobs: + github_token: ${{ secrets.GITHUB_TOKEN }} ``` +### ⭐️ CNAME + +To add `CNAME` file, we can set the `cname` option. + +For more details about `CNAME`, read the official documentation: [Managing a custom domain for your GitHub Pages site - GitHub Help](https://help.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site) + +```yaml +- name: Deploy + uses: peaceiris/actions-gh-pages@v3 + with: + deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} + publish_dir: ./public + cname: github.com +``` + +### ⭐️ Disable `.nojekyll` + +By default, this action adds the `.nojekyll` file to only the `master` and `gh-pages` branches. When the file already exists, this action does nothing. + +To disable this behavior, we can set the `disable_nojekyll` option to `true`. + +```yaml +- name: Deploy + uses: peaceiris/actions-gh-pages@v3 + with: + deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} + publish_dir: ./public + disable_nojekyll: true +``` + ### ⭐️ Allow empty commits By default, a commit will not be generated when no file changes. If you want to allow an empty commit, set the optional parameter `allow_empty_commit` to `true`. diff --git a/__tests__/get-inputs.test.ts b/__tests__/get-inputs.test.ts index eed12efb6..01872ce9a 100644 --- a/__tests__/get-inputs.test.ts +++ b/__tests__/get-inputs.test.ts @@ -21,6 +21,8 @@ afterEach(() => { delete process.env['INPUT_COMMIT_MESSAGE']; delete process.env['INPUT_TAG_NAME']; delete process.env['INPUT_TAG_MESSAGE']; + delete process.env['INPUT_DISABLE_NOJEKYLL']; + delete process.env['INPUT_CNAME']; }); describe('getInputs()', () => { @@ -30,15 +32,6 @@ describe('getInputs()', () => { // process.env['INPUT_PERSONAL_TOKEN'] = 'test_personal_token'; process.env['INPUT_PUBLISH_BRANCH'] = 'gh-pages'; 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(); @@ -56,6 +49,8 @@ describe('getInputs()', () => { expect(inps.CommitMessage).toMatch(''); expect(inps.TagName).toMatch(''); expect(inps.TagMessage).toMatch(''); + expect(inps.DisableNoJekyll).toBe(false); + expect(inps.CNAME).toMatch(''); }); test('get spec inputs', () => { @@ -73,6 +68,8 @@ describe('getInputs()', () => { 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'; + process.env['INPUT_DISABLE_NOJEKYLL'] = 'true'; + process.env['INPUT_CNAME'] = 'github.com'; const inps: Inputs = getInputs(); @@ -90,5 +87,7 @@ describe('getInputs()', () => { expect(inps.CommitMessage).toMatch('feat: Add new feature'); expect(inps.TagName).toMatch('deploy-v1.2.3'); expect(inps.TagMessage).toMatch('Deployment v1.2.3'); + expect(inps.DisableNoJekyll).toBe(true); + expect(inps.CNAME).toMatch('github.com'); }); }); diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts new file mode 100644 index 000000000..ba3f3316d --- /dev/null +++ b/__tests__/utils.test.ts @@ -0,0 +1,190 @@ +import path from 'path'; +import fs from 'fs'; +import { + getHomeDir, + getWorkDirName, + createWorkDir, + addNoJekyll, + addCNAME +} from '../src/utils'; + +beforeEach(() => { + jest.resetModules(); +}); + +// afterEach(() => { + +// }); + +async function getTime(): Promise { + const date = new Date(); + const unixTime = date.getTime(); + return `${unixTime}`; +} + +describe('getHomeDir()', () => { + test('get home directory name', async () => { + let test = ''; + if (process.platform === 'win32') { + test = 'C:\\Users\\runneradmin'; + } else { + test = `${process.env.HOME}`; + } + const expected = await getHomeDir(); + expect(test).toMatch(expected); + }); +}); + +describe('getWorkDirName()', () => { + test('get work directory name', async () => { + let home = ''; + if (process.platform === 'win32') { + home = 'C:\\Users\\runneradmin'; + } else { + home = `${process.env.HOME}`; + } + const unixTime = await getTime(); + const expected = path.join(home, `actions_github_pages_${unixTime}`); + const test = await getWorkDirName(`${unixTime}`); + expect(test).toMatch(expected); + }); +}); + +describe('createWorkDir()', () => { + test('create work directory', async () => { + const unixTime = await getTime(); + const workDirName = await getWorkDirName(`${unixTime}`); + await createWorkDir(workDirName); + const test = fs.existsSync(workDirName); + expect(test).toBe(true); + }); +}); + +async function getWorkDir(): Promise { + const unixTime = await getTime(); + let workDir = ''; + workDir = await getWorkDirName(`${unixTime}`); + await createWorkDir(workDir); + return workDir; +} + +describe('addNoJekyll()', () => { + test('add .nojekyll gh-pages', async () => { + let workDir = ''; + (async (): Promise => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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); + }); +}); diff --git a/action.yml b/action.yml index f25ad8b3a..0081a1c13 100644 --- a/action.yml +++ b/action.yml @@ -55,3 +55,10 @@ inputs: tag_message: description: 'Set tag message' 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 diff --git a/package-lock.json b/package-lock.json index 4cb980d57..d38913e93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "actions-github-pages", - "version": "3.2.0", + "version": "3.3.0-0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ecf254cf0..a65c91a49 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "actions-github-pages", - "version": "3.2.0", + "version": "3.3.0-0", "description": "GitHub Actions for GitHub Pages", "main": "lib/index.js", "engines": { diff --git a/src/get-inputs.ts b/src/get-inputs.ts index d45769abe..032f60589 100644 --- a/src/get-inputs.ts +++ b/src/get-inputs.ts @@ -21,6 +21,8 @@ function showInputs(inps: Inputs): void { core.info(`[INFO] CommitMessage: ${inps.CommitMessage}`); core.info(`[INFO] TagName: ${inps.TagName}`); core.info(`[INFO] TagMessage: ${inps.TagMessage}`); + core.info(`[INFO] DisableNoJekyll: ${inps.DisableNoJekyll}`); + core.info(`[INFO] CNAME: ${inps.CNAME}`); } export function getInputs(): Inputs { @@ -41,7 +43,10 @@ export function getInputs(): Inputs { UserEmail: core.getInput('user_email'), CommitMessage: core.getInput('commit_message'), 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); diff --git a/src/git-utils.ts b/src/git-utils.ts index c18cafa5c..c56eb7ade 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -5,13 +5,7 @@ import * as io from '@actions/io'; import path from 'path'; import fs from 'fs'; import {Inputs, CmdResult} from './interfaces'; -import {getHomeDir} from './utils'; - -export async function createWorkDir(workDirName: string): Promise { - await io.mkdirP(workDirName); - core.debug(`workDir was created: ${workDirName}`); - return; -} +import {createWorkDir} from './utils'; export async function createBranchForce(branch: string): Promise { await exec.exec('git', ['init']); @@ -41,9 +35,8 @@ export async function copyAssets( export async function setRepo( inps: Inputs, remoteURL: string, - unixTime: string + workDir: string ): Promise { - const workDir = path.join(getHomeDir(), `actions_github_pages_${unixTime}`); const publishDir = path.join( `${process.env.GITHUB_WORKSPACE}`, inps.PublishDir diff --git a/src/interfaces.ts b/src/interfaces.ts index c1bb2111b..f61edf8fd 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -13,6 +13,8 @@ export interface Inputs { readonly CommitMessage: string; readonly TagName: string; readonly TagMessage: string; + readonly DisableNoJekyll: boolean; + readonly CNAME: string; } export interface CmdResult { diff --git a/src/main.ts b/src/main.ts index 4fe539887..98bc55449 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import {Inputs} from './interfaces'; import {getInputs} from './get-inputs'; import {setTokens} from './set-tokens'; import * as git from './git-utils'; +import {getWorkDirName, addNoJekyll, addCNAME} from './utils'; export async function run(): Promise { try { @@ -16,12 +17,16 @@ export async function run(): Promise { const date = new Date(); 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 { await exec.exec('git', ['remote', 'rm', 'origin']); } catch (e) { - core.info(`[INFO] e`); + core.info(`[INFO] ${e}`); } await exec.exec('git', ['remote', 'add', 'origin', remoteURL]); await exec.exec('git', ['add', '--all']); diff --git a/src/set-tokens.ts b/src/set-tokens.ts index ec0078112..667f5722d 100644 --- a/src/set-tokens.ts +++ b/src/set-tokens.ts @@ -22,7 +22,7 @@ export async function setSSHKey( ): Promise { core.info('[INFO] setup SSH deploy key'); - const homeDir = getHomeDir(); + const homeDir = await getHomeDir(); const sshDir = path.join(homeDir, '.ssh'); await io.mkdirP(sshDir); await exec.exec('chmod', ['700', sshDir]); diff --git a/src/utils.ts b/src/utils.ts index 0363edcd4..c86147b61 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,9 @@ 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 { let homedir = ''; if (process.platform === 'win32') { @@ -13,3 +16,49 @@ export function getHomeDir(): string { return homedir; } + +export async function getWorkDirName(unixTime: string): Promise { + const homeDir = await getHomeDir(); + const workDirName = path.join(homeDir, `actions_github_pages_${unixTime}`); + return workDirName; +} + +export async function createWorkDir(workDirName: string): Promise { + await io.mkdirP(workDirName); + core.debug(`Created: ${workDirName}`); + return; +} + +export async function addNoJekyll( + workDir: string, + DisableNoJekyll: boolean, + PublishBranch: string +): Promise { + 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 { + if (content === '') { + return; + } + const filepath = path.join(workDir, 'CNAME'); + if (fs.existsSync(filepath)) { + core.warning(`CNAME already exists, skip adding CNAME`); + return; + } + fs.writeFileSync(filepath, content + '\n'); + core.info(`[INFO] Created ${filepath}`); +}