Skip to content

Commit

Permalink
fix: add safe directory commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesIves committed Sep 27, 2024
1 parent bfd213f commit 31fa027
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/execute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import {exec} from '@actions/exec'
import buffer from 'buffer'

/**
* The output of a command.
*/
type ExecuteOutput = {
/**
* The standard output of the command.
*/
stdout: string
/**
* The standard error of the command.
*/
stderr: string
}

Expand All @@ -21,7 +30,7 @@ export async function execute(
cmd: string,
cwd: string,
silent: boolean,
ignoreReturnCode = false
ignoreReturnCode: boolean = false
): Promise<ExecuteOutput> {
output.stdout = ''
output.stderr = ''
Expand All @@ -37,6 +46,9 @@ export async function execute(
return Promise.resolve(output)
}

/**
* Writes the output of a command to the stdout buffer.
*/
export function stdout(data: Buffer | string): void {
const dataString = data.toString().trim()
if (
Expand All @@ -47,6 +59,9 @@ export function stdout(data: Buffer | string): void {
}
}

/**
* Writes the output of a command to the stderr buffer.
*/
export function stderr(data: Buffer | string): void {
const dataString = data.toString().trim()
if (
Expand Down
18 changes: 18 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ export async function init(action: ActionInterface): Promise<void | Error> {
info(`Deploying using ${action.tokenType}… 🔑`)
info('Configuring git…')

/**
* Add safe directory to the global git config.
*/
try {
await execute(
`git config --global safe.directory '*'`,
action.workspace,
action.silent
)
} catch {
info('Unable to set workflow file tree as a safe directory…')
}

/**
* Ensure that the workspace is a safe directory, this is somewhat redundant as the action
* will always set the workspace as a safe directory, but this is a fallback in case the action
* fails to do so.
*/
try {
await execute(
`git config --global --add safe.directory "${action.workspace}"`,
Expand Down

0 comments on commit 31fa027

Please sign in to comment.