2022-06-02 15:53:11 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2022-06-15 03:41:49 +02:00
|
|
|
import * as fs from 'fs'
|
2023-12-15 13:01:04 +01:00
|
|
|
import * as core from '@actions/core'
|
2022-06-02 15:53:11 -04:00
|
|
|
import { RunScriptStepArgs } from 'hooklib'
|
2025-10-02 16:23:07 +02:00
|
|
|
import { execCpFromPod, execCpToPod, execPodStep } from '../k8s'
|
|
|
|
|
import { writeRunScript, sleep, listDirAllCommand } from '../k8s/utils'
|
2022-06-15 03:41:49 +02:00
|
|
|
import { JOB_CONTAINER_NAME } from './constants'
|
2025-10-02 16:23:07 +02:00
|
|
|
import { dirname } from 'path'
|
2022-06-02 15:53:11 -04:00
|
|
|
|
|
|
|
|
export async function runScriptStep(
|
|
|
|
|
args: RunScriptStepArgs,
|
2025-10-02 16:23:07 +02:00
|
|
|
state
|
2022-06-02 15:53:11 -04:00
|
|
|
): Promise<void> {
|
2025-10-02 16:23:07 +02:00
|
|
|
// Write the entrypoint first. This will be later coppied to the workflow pod
|
2022-06-15 03:41:49 +02:00
|
|
|
const { entryPoint, entryPointArgs, environmentVariables } = args
|
2025-10-02 16:23:07 +02:00
|
|
|
const { containerPath, runnerPath } = writeRunScript(
|
2022-06-15 03:41:49 +02:00
|
|
|
args.workingDirectory,
|
|
|
|
|
entryPoint,
|
|
|
|
|
entryPointArgs,
|
|
|
|
|
args.prependPath,
|
|
|
|
|
environmentVariables
|
2022-06-02 15:53:11 -04:00
|
|
|
)
|
|
|
|
|
|
2025-10-02 16:23:07 +02:00
|
|
|
const workdir = dirname(process.env.RUNNER_WORKSPACE as string)
|
|
|
|
|
const containerTemp = '/__w/_temp'
|
|
|
|
|
const runnerTemp = `${workdir}/_temp`
|
|
|
|
|
await execCpToPod(state.jobPod, runnerTemp, containerTemp)
|
|
|
|
|
|
|
|
|
|
// Execute the entrypoint script
|
2022-06-15 03:41:49 +02:00
|
|
|
args.entryPoint = 'sh'
|
|
|
|
|
args.entryPointArgs = ['-e', containerPath]
|
|
|
|
|
try {
|
|
|
|
|
await execPodStep(
|
|
|
|
|
[args.entryPoint, ...args.entryPointArgs],
|
|
|
|
|
state.jobPod,
|
|
|
|
|
JOB_CONTAINER_NAME
|
|
|
|
|
)
|
|
|
|
|
} catch (err) {
|
2023-12-15 13:01:04 +01:00
|
|
|
core.debug(`execPodStep failed: ${JSON.stringify(err)}`)
|
|
|
|
|
const message = (err as any)?.response?.body?.message || err
|
|
|
|
|
throw new Error(`failed to run script step: ${message}`)
|
2022-06-15 03:41:49 +02:00
|
|
|
} finally {
|
2025-10-02 16:23:07 +02:00
|
|
|
try {
|
|
|
|
|
fs.rmSync(runnerPath, { force: true })
|
|
|
|
|
} catch (removeErr) {
|
|
|
|
|
core.debug(`Failed to remove file ${runnerPath}: ${removeErr}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
core.debug(
|
|
|
|
|
`Copying from job pod '${state.jobPod}' ${containerTemp} to ${runnerTemp}`
|
|
|
|
|
)
|
|
|
|
|
await execCpFromPod(state.jobPod, containerTemp, workdir)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.warning('Failed to copy _temp from pod')
|
2022-06-02 15:53:11 -04:00
|
|
|
}
|
|
|
|
|
}
|