diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3fd1e5b..af3967b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -131,7 +131,7 @@ jobs: force_orphan: true user_name: 'github-actions[bot]' user_email: 'github-actions[bot]@users.noreply.github.com' - # commit_message: ${{ github.event.head_commit.message }} + full_commit_message: ${{ github.event.head_commit.message }} - name: Deploy if: | diff --git a/README.md b/README.md index b58844e..7311146 100644 --- a/README.md +++ b/README.md @@ -397,6 +397,18 @@ When we create a commit with a message `docs: Update some post`, a deployment co commit_message: ${{ github.event.head_commit.message }} ``` +To set a full custom commit message without a triggered commit hash, +use the `full_commit_message` option instead of the `commit_message` option. + +```yaml +- name: Deploy + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./public + full_commit_message: ${{ github.event.head_commit.message }} +``` + ### ⭐️ Create Git tag Here is an example workflow. diff --git a/__tests__/get-inputs.test.ts b/__tests__/get-inputs.test.ts index d369200..648bc86 100644 --- a/__tests__/get-inputs.test.ts +++ b/__tests__/get-inputs.test.ts @@ -51,6 +51,7 @@ function getInputsLog(authMethod: string, inps: Inputs): string { [INFO] UserName: ${inps.UserName} [INFO] UserEmail: ${inps.UserEmail} [INFO] CommitMessage: ${inps.CommitMessage} +[INFO] FullCommitMessage: ${inps.FullCommitMessage} [INFO] TagName: ${inps.TagName} [INFO] TagMessage: ${inps.TagMessage} [INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll} @@ -117,6 +118,7 @@ describe('getInputs()', () => { expect(inps.UserName).toMatch(''); expect(inps.UserEmail).toMatch(''); expect(inps.CommitMessage).toMatch(''); + expect(inps.FullCommitMessage).toMatch(''); expect(inps.TagName).toMatch(''); expect(inps.TagMessage).toMatch(''); expect(inps.DisableNoJekyll).toBe(false); @@ -136,6 +138,7 @@ describe('getInputs()', () => { 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_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'; @@ -155,12 +158,19 @@ describe('getInputs()', () => { expect(inps.UserName).toMatch('username'); expect(inps.UserEmail).toMatch('github@github.com'); expect(inps.CommitMessage).toMatch('feat: Add new feature'); + expect(inps.FullCommitMessage).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'); }); + test('get spec inputs enable_jekyll', () => { + process.env['INPUT_ENABLE_JEKYLL'] = 'true'; + const inps: Inputs = getInputs(); + expect(inps.DisableNoJekyll).toBe(true); + }); + test('throw error enable_jekyll or disable_nojekyll', () => { process.env['INPUT_DEPLOY_KEY'] = 'test_deploy_key'; process.env['INPUT_ENABLE_JEKYLL'] = 'true'; diff --git a/__tests__/git-utils.test.ts b/__tests__/git-utils.test.ts index d8b94c4..73b1166 100644 --- a/__tests__/git-utils.test.ts +++ b/__tests__/git-utils.test.ts @@ -1,4 +1,9 @@ -import {getUserName, getUserEmail, setCommitAuthor} from '../src/git-utils'; +import { + getUserName, + getUserEmail, + setCommitAuthor, + getCommitMessage +} from '../src/git-utils'; import {getWorkDirName, createWorkDir} from '../src/utils'; import {CmdResult} from '../src/interfaces'; import * as exec from '@actions/exec'; @@ -114,3 +119,54 @@ describe('setCommitAuthor()', () => { ); }); }); + +describe('getCommitMessage()', () => { + test('get default message', () => { + const test = getCommitMessage('', '', '', 'actions/pages', 'commit_hash'); + expect(test).toMatch('deploy: commit_hash'); + }); + + test('get default message for external repository', () => { + const test = getCommitMessage( + '', + '', + 'actions/actions.github.io', + 'actions/pages', + 'commit_hash' + ); + expect(test).toMatch('deploy: actions/pages@commit_hash'); + }); + + test('get custom message', () => { + const test = getCommitMessage( + 'Custom msg', + '', + '', + 'actions/pages', + 'commit_hash' + ); + expect(test).toMatch('Custom msg commit_hash'); + }); + + test('get custom message for external repository', () => { + const test = getCommitMessage( + 'Custom msg', + '', + 'actions/actions.github.io', + 'actions/pages', + 'commit_hash' + ); + expect(test).toMatch('Custom msg actions/pages@commit_hash'); + }); + + test('get full custom message', () => { + const test = getCommitMessage( + '', + 'Full custom msg', + '', + 'actions/pages', + 'commit_hash' + ); + expect(test).toMatch('Full custom msg'); + }); +}); diff --git a/action.yml b/action.yml index c05618f..74c5afd 100644 --- a/action.yml +++ b/action.yml @@ -47,7 +47,10 @@ inputs: description: 'Set Git user.email' required: false commit_message: - description: 'Set custom commit message' + description: 'Set a custom commit message with a triggered commit hash' + required: false + full_commit_message: + description: 'Set a custom full commit message without a triggered commit hash' required: false tag_name: description: 'Set tag name' diff --git a/src/get-inputs.ts b/src/get-inputs.ts index c43a6c3..7eec4ba 100644 --- a/src/get-inputs.ts +++ b/src/get-inputs.ts @@ -22,6 +22,7 @@ export function showInputs(inps: Inputs): void { [INFO] UserName: ${inps.UserName} [INFO] UserEmail: ${inps.UserEmail} [INFO] CommitMessage: ${inps.CommitMessage} +[INFO] FullCommitMessage: ${inps.FullCommitMessage} [INFO] TagName: ${inps.TagName} [INFO] TagMessage: ${inps.TagMessage} [INFO] EnableJekyll (DisableNoJekyll): ${inps.DisableNoJekyll} @@ -61,6 +62,7 @@ export function getInputs(): Inputs { UserName: core.getInput('user_name'), UserEmail: core.getInput('user_email'), CommitMessage: core.getInput('commit_message'), + FullCommitMessage: core.getInput('full_commit_message'), TagName: core.getInput('tag_name'), TagMessage: core.getInput('tag_message'), DisableNoJekyll: useBuiltinJekyll, diff --git a/src/git-utils.ts b/src/git-utils.ts index 8037be9..3e45873 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -1,6 +1,5 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; -import * as github from '@actions/github'; import * as io from '@actions/io'; import path from 'path'; import fs from 'fs'; @@ -133,26 +132,38 @@ export async function setCommitAuthor( await exec.exec('git', ['config', 'user.email', getUserEmail(userEmail)]); } +export function getCommitMessage( + msg: string, + fullMsg: string, + extRepo: string, + baseRepo: string, + hash: string +): string { + const msgHash = ((): string => { + if (extRepo) { + return `${baseRepo}@${hash}`; + } else { + return hash; + } + })(); + + const subject = ((): string => { + if (fullMsg) { + return fullMsg; + } else if (msg) { + return `${msg} ${msgHash}`; + } else { + return `deploy: ${msgHash}`; + } + })(); + + return subject; +} + export async function commit( allowEmptyCommit: boolean, - externalRepository: string, - message: string + msg: string ): Promise { - let msg = ''; - if (message) { - msg = message; - } else { - msg = 'deploy:'; - } - - const hash = `${process.env.GITHUB_SHA}`; - const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`; - if (externalRepository) { - msg = `${msg} ${baseRepo}@${hash}`; - } else { - msg = `${msg} ${hash}`; - } - try { if (allowEmptyCommit) { await exec.exec('git', ['commit', '--allow-empty', '-m', `${msg}`]); diff --git a/src/interfaces.ts b/src/interfaces.ts index f61edf8..2cbd538 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -11,6 +11,7 @@ export interface Inputs { readonly UserName: string; readonly UserEmail: string; readonly CommitMessage: string; + readonly FullCommitMessage: string; readonly TagName: string; readonly TagMessage: string; readonly DisableNoJekyll: boolean; diff --git a/src/main.ts b/src/main.ts index 1964f95..d136ae0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,18 @@ import {context} from '@actions/github'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; +import * as github from '@actions/github'; import {Inputs} from './interfaces'; import {showInputs, getInputs} from './get-inputs'; import {setTokens} from './set-tokens'; -import {setRepo, setCommitAuthor, commit, push, pushTag} from './git-utils'; +import { + setRepo, + setCommitAuthor, + getCommitMessage, + commit, + push, + pushTag +} from './git-utils'; import {getWorkDirName, addNoJekyll, addCNAME, skipOnFork} from './utils'; export async function run(): Promise { @@ -54,11 +62,16 @@ export async function run(): Promise { await exec.exec('git', ['remote', 'add', 'origin', remoteURL]); await exec.exec('git', ['add', '--all']); await setCommitAuthor(inps.UserName, inps.UserEmail); - await commit( - inps.AllowEmptyCommit, + const hash = `${process.env.GITHUB_SHA}`; + const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`; + const commitMessage = getCommitMessage( + inps.CommitMessage, + inps.FullCommitMessage, inps.ExternalRepository, - inps.CommitMessage + baseRepo, + hash ); + await commit(inps.AllowEmptyCommit, commitMessage); await push(inps.PublishBranch, inps.ForceOrphan); await pushTag(inps.TagName, inps.TagMessage);