8000 Add preCommitScript option by pikajude · Pull Request #945 · JamesIves/github-pages-deploy-action · GitHub
[go: up one dir, main page]

Skip to content

Add preCommitScript option #945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions __tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {action, Status, TestFlag} from '../src/constants'
import {execute} from '../src/execute'
import {deploy, init} from '../src/git'
import fs from 'fs'
import {exec} from '@actions/exec'

const originalAction = JSON.stringify(action)

Expand All @@ -22,6 +23,10 @@ jest.mock('@actions/core', () => ({
info: jest.fn()
}))

jest.mock('@actions/exec', () => ({
exec: jest.fn()
}))

jest.mock('@actions/io', () => ({
rmRF: jest.fn(),
mkdirP: jest.fn()
Expand Down Expand Up @@ -378,6 +383,26 @@ describe('git', () => {
expect(mkdirP).toBeCalledTimes(1)
})

it('should run the pre-commit script', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: '.',
branch: 'branch',
token: '123',
pusher: {},
clean: true,
commitMessage: 'Hello!',
preCommitScript: 'echo "Hello, world!"',
isTest: TestFlag.NONE
})

await deploy(action)

expect(execute).toBeCalledTimes(10)
expect(exec).toBeCalledTimes(1)
})

it('should stop early if there is nothing to commit', async () => {
Object.assign(action, {
hostname: 'github.com',
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ inputs:
description: 'If you would like to push the contents of the deployment folder into a specific directory on the deployment branch you can specify it here.'
required: false

pre-commit:
description: 'Shell script to run before the new commit is created.'
required: false

commit-message:
description: 'If you need to customize the commit message for an integration you can do so.'
required: false
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface ActionInterface {
isTest: TestFlag
/** The git config name. */
name?: string
/** Shell script to run before creating a new commit containing the target folder */
preCommitScript?: string
/** The repository path, for example JamesIves/github-pages-deploy-action. */
repositoryName?: string
/** The fully qualified repositpory path, this gets auto generated if repositoryName is provided. */
Expand Down Expand Up @@ -132,6 +134,7 @@ export const action: ActionInterface = {
? true
: getInput('ssh-key'),
targetFolder: getInput('target-folder'),
preCommitScript: getInput('pre-commit'),
workspace: process.env.GITHUB_WORKSPACE || ''
}

Expand Down
11 changes: 10 additions & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {info} from '@actions/core'
import {mkdirP, rmRF} from '@actions/io'
import fs from 'fs'
import {ActionInterface, Status, TestFlag} from './constants'
import {execute} from './execute'
import {execute, stdout} from './execute'
import {generateWorktree} from './worktree'
import {
extractErrorMessage,
isNullOrUndefined,
suppressSensitiveInformation
} from './util'
import {exec} from '@actions/exec'

/* Initializes git in the workspace. */
export async function init(action: ActionInterface): Promise<void | Error> {
Expand Down Expand Up @@ -147,6 +148,14 @@ export async function deploy(action: ActionInterface): Promise<Status> {
action.silent
)

if (action.preCommitScript) {
await exec('bash', ['-c', action.preCommitScript], {
listeners: {stdout},
silent: action.silent,
cwd: `${action.workspace}/${temporaryDeploymentDirectory}`
})
}

if (action.singleCommit) {
await execute(
`git add --all .`,
Expand Down
0