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

improvement: make sure artifacts are always fetched #6532

Merged
merged 10 commits into from
Oct 17, 2024
35 changes: 32 additions & 3 deletions core/src/plugins/exec/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { styles } from "../../logger/styles.js"
import { copyArtifacts, execRunCommand } from "./common.js"
import { execRunSpecSchema, execRuntimeOutputsSchema, execStaticOutputsSchema } from "./config.js"
import { execProvider } from "./exec.js"
import { InternalError } from "../../exceptions.js"

export const execRun = execProvider.createActionType({
kind: "Run",
Expand All @@ -32,8 +33,38 @@ export type ExecRun = GardenSdkActionDefinitionActionType<typeof execRun>
execRun.addHandler("run", async ({ artifactsPath, log, action, ctx }) => {
const startedAt = new Date()
const { command, env, artifacts } = action.getSpec()
let runCommandError: unknown | undefined

const commandResult = await execRunCommand({ command, action, ctx, log, env, opts: { reject: false } })
let commandResult: Awaited<ReturnType<typeof execRunCommand>> | undefined
try {
// Execute the run command
commandResult = await execRunCommand({ command, action, ctx, log, env, opts: { reject: false } })
} catch (error) {
// Store error to be thrown at the end after trying to fetch artifacts
runCommandError = error
}

try {
// Try to fetch artifacts
await copyArtifacts(log, artifacts, action.getBuildPath(), artifactsPath)
} catch (error) {
if (runCommandError || !commandResult?.success) {
// If the run command has failed or thrown an error, and the artifact copy has failed as well, we just log the artifact copy error
// since we'll trow the run command error later on.
log.error(`Failed to copy artifacts: ${error}`)
} else {
throw error
}
}

if (runCommandError) {
// The run command failed, so we throw the error
throw runCommandError
} else if (!commandResult) {
throw new InternalError({
message: "CommandResult should not be undefined if there was no error",
})
}

const detail = {
moduleName: action.moduleName(),
Expand Down Expand Up @@ -70,7 +101,5 @@ execRun.addHandler("run", async ({ artifactsPath, log, action, ctx }) => {
)
}

await copyArtifacts(log, artifacts, action.getBuildPath(), artifactsPath)

return result
})
36 changes: 32 additions & 4 deletions core/src/plugins/exec/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { styles } from "../../logger/styles.js"
import { copyArtifacts, execRunCommand } from "./common.js"
import { execRunSpecSchema, execRuntimeOutputsSchema, execStaticOutputsSchema } from "./config.js"
import { execProvider } from "./exec.js"
import { InternalError } from "../../exceptions.js"

export const execTestSpecSchema = execRunSpecSchema

Expand All @@ -34,8 +35,38 @@ export type ExecTest = GardenSdkActionDefinitionActionType<typeof execTest>
execTest.addHandler("run", async ({ log, action, artifactsPath, ctx }) => {
const startedAt = new Date()
const { command, env, artifacts } = action.getSpec()
let runCommandError: unknown | undefined

const commandResult = await execRunCommand({ command, action, ctx, log, env, opts: { reject: false } })
let commandResult: Awaited<ReturnType<typeof execRunCommand>> | undefined
try {
// Execute the test command
commandResult = await execRunCommand({ command, action, ctx, log, env, opts: { reject: false } })
} catch (error) {
// Store error to be thrown at the end after trying to fetch artifacts
runCommandError = error
}

try {
// Try to fetch artifacts
await copyArtifacts(log, artifacts, action.getBuildPath(), artifactsPath)
} catch (error) {
if (runCommandError || !commandResult?.success) {
// If the test command has failed or thrown an error, and the artifact copy has failed as well, we just log the artifact copy error
// since we'll trow the test command error later on.
log.error(`Failed to copy artifacts: ${error}`)
} else {
throw error
}
}

if (runCommandError) {
// The test command failed, so we throw the error
throw runCommandError
} else if (!commandResult) {
throw new InternalError({
message: "CommandResult should not be undefined if there was no error",
})
}

const detail = {
moduleName: action.moduleName(),
Expand Down Expand Up @@ -71,8 +102,5 @@ execTest.addHandler("run", async ({ log, action, artifactsPath, ctx }) => {
})
)
}

await copyArtifacts(log, artifacts, action.getBuildPath(), artifactsPath)

return result
})