Skip to content
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

chore(Add red printing for fatal errors) #242

Merged
merged 24 commits into from
Feb 12, 2025
Merged
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
101 changes: 45 additions & 56 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export async function buildPiperInnerSource (version: string, wdfGithubEnterpris
})
const wd = cwd()

const repositoryPath = join(path, fs.readdirSync(path).find((name: string) => {
return name.includes(repository)
}) ?? '')
const repositoryPath = join(path, fs.readdirSync(path).find((name: string) => name.includes(repository)) ?? '')
info(`repositoryPath: ${repositoryPath}`)
chdir(repositoryPath)

Expand Down
13 changes: 6 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ export async function downloadDefaultConfig (server: string, apiURL: string, ver
const flags: string[] = []
flags.push(...defaultsPathsArgs)
flags.push('--gitHubTokens', `${getHost(server)}:${token}`)
const piperExec = await executePiper('getDefaults', flags)

let defaultConfigs = JSON.parse(piperExec.output)
const { stdout } = await executePiper('getDefaults', flags)
let defaultConfigs = JSON.parse(stdout)
if (customDefaultsPathsArray.length === 0) {
defaultConfigs = [defaultConfigs]
}
Expand Down Expand Up @@ -268,8 +267,8 @@ export async function downloadStageConfig (actionCfg: ActionConfiguration): Prom
const flags: string[] = ['--useV1']
flags.push('--defaultsFile', stageConfigPath)
flags.push('--gitHubTokens', `${getHost(actionCfg.gitHubEnterpriseServer)}:${actionCfg.gitHubEnterpriseToken}`)
const piperExec = await executePiper('getDefaults', flags)
const config = JSON.parse(piperExec.output)
const { stdout } = await executePiper('getDefaults', flags)
const config = JSON.parse(stdout)
fs.writeFileSync(path.join(CONFIG_DIR, ENTERPRISE_STAGE_CONFIG_FILENAME), config.content)
}

Expand Down Expand Up @@ -352,6 +351,6 @@ export async function readContextConfig (stepName: string, flags: string[]): Pro
getConfigFlags.push('--customConfig', customConfigFlagValue)
}

const piperExec = await executePiper('getConfig', getConfigFlags)
return JSON.parse(piperExec.output)
const { stdout } = await executePiper('getConfig', getConfigFlags)
return JSON.parse(stdout)
}
8 changes: 4 additions & 4 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export async function startContainer (actionCfg: ActionConfiguration, ctxConfig:

let dockerOptionsArray: string[] = []
const dockerOptions = actionCfg.dockerOptions !== '' ? actionCfg.dockerOptions : ctxConfig.dockerOptions
if (dockerOptions !== undefined && Array.isArray(dockerOptions)) {
dockerOptionsArray = dockerOptions.map(option => option.split(' ')).flat()
} else if (dockerOptions !== undefined) {
dockerOptionsArray = dockerOptions.split(' ')
if (dockerOptions !== undefined) {
dockerOptionsArray = Array.isArray(dockerOptions)
? dockerOptions.map(option => option.split(' ')).flat()
: dockerOptionsArray = dockerOptions.split(' ')
}

const dockerRunArgs: string[] = [
Expand Down
89 changes: 37 additions & 52 deletions src/execute.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,55 @@
import { exec, type ExecOptions } from '@actions/exec'
import { type ExecOptions, type ExecOutput, getExecOutput } from '@actions/exec'
import path from 'path'
import { internalActionVariables } from './piper'

export interface piperExecResult {
output: string
error: string
exitCode: number
}
import { debug, error } from '@actions/core'

export async function executePiper (
stepName: string, flags?: string[], ignoreDefaults?: boolean, execOptions?: ExecOptions
): Promise<piperExecResult> {
let piperOutput = ''
stepName: string, flags: string[] = [], ignoreDefaults: boolean = false, execOptions?: ExecOptions
): Promise<ExecOutput> {
if (process.env.GITHUB_JOB !== undefined) flags.push('--stageName', process.env.GITHUB_JOB)

flags = !ignoreDefaults && process.env.defaultsFlags !== undefined
? flags.concat(JSON.parse(process.env.defaultsFlags))
: flags

const piperPath = internalActionVariables.piperBinPath
const containerID = internalActionVariables.dockerContainerID
let piperError = ''
let options = {
listeners: {
stdout: (data: Buffer) => {
piperOutput += data.toString()
stdline: (data: string) => {
if (data.includes('fatal')) {
error(data)
piperError += data
}
},
stderr: (data: Buffer) => {
piperError += data.toString()
errline: (data: string) => {
if (data.includes('fatal')) {
error(data)
piperError += data
}
}
}
}
options = Object.assign({}, options, execOptions)

flags = flags ?? []

const stageName = process.env.GITHUB_JOB
if (stageName !== undefined) {
flags.push('--stageName', stageName)
}

const defaultsFlags = process.env.defaultsFlags
if (ignoreDefaults !== false && defaultsFlags !== undefined) {
flags = flags.concat(JSON.parse(defaultsFlags))
}
// Default to Piper
let binaryPath = piperPath
let args: string[] = [stepName, ...flags]

const piperPath = internalActionVariables.piperBinPath
const containerID = internalActionVariables.dockerContainerID
if (containerID === '') {
return await exec(piperPath, [
stepName,
...flags
],
options)
.then(exitCode => {
return { output: piperOutput, error: piperError, exitCode }
})
.catch(err => {
throw new Error(`Piper execution error: ${err as string}: ${piperError}`)
})
}
return await exec('docker', [
'exec',
containerID,
if (containerID !== '') { // Running in a container
debug(`containerID: ${containerID}, running in docker`)
binaryPath = 'docker'
args = [
'exec',
containerID,
`/piper/${path.basename(piperPath)}`,
stepName,
...flags
], options).then(exitCode => {
return {
output: piperOutput,
error: piperError,
exitCode
}
}).catch(err => {
throw new Error(`Piper execution error: ${err as string}: ${piperError}`)
})
]
}

return await getExecOutput(binaryPath, args, options)
.then((execOutput: ExecOutput) => (execOutput))
.catch(err => { throw new Error(`Piper execution error: ${err as string}: ${piperError}`) })
}
2 changes: 1 addition & 1 deletion src/pipelineEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function exportPipelineEnv (exportPipelineEnvironment: boolean): Pr
})

try {
const pipelineEnv = JSON.stringify(JSON.parse((piperExec.output)))
const pipelineEnv = JSON.stringify(JSON.parse((piperExec.stdout)))
setOutput('pipelineEnv', pipelineEnv)
} catch (err) {
throw new Error(`Could not export pipeline environment: ${err as string}`)
Expand Down
Loading
Loading