diff --git a/packages/docker/src/dockerCommands/container.ts b/packages/docker/src/dockerCommands/container.ts index ed061ff..c1da345 100644 --- a/packages/docker/src/dockerCommands/container.ts +++ b/packages/docker/src/dockerCommands/container.ts @@ -196,6 +196,18 @@ export async function containerNetworkRemove(network: string): Promise { await runDockerCommand(dockerArgs) } +export async function containerNetworkPrune(): Promise { + const dockerArgs = [ + 'network', + 'prune', + '--force', + '--filter', + `label=${getRunnerLabel()}` + ] + + await runDockerCommand(dockerArgs) +} + export async function containerPrune(): Promise { const dockerPSArgs: string[] = [ 'ps', diff --git a/packages/docker/src/hooks/cleanup-job.ts b/packages/docker/src/hooks/cleanup-job.ts index f855db3..acb7d40 100644 --- a/packages/docker/src/hooks/cleanup-job.ts +++ b/packages/docker/src/hooks/cleanup-job.ts @@ -1,21 +1,9 @@ import { - containerRemove, - containerNetworkRemove + containerNetworkPrune, + containerPrune } from '../dockerCommands/container' -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export async function cleanupJob(args, state, responseFile): Promise { - const containerIds: string[] = [] - if (state?.container) { - containerIds.push(state.container) - } - if (state?.services) { - containerIds.push(state.services) - } - if (containerIds.length > 0) { - await containerRemove(containerIds) - } - if (state.network) { - await containerNetworkRemove(state.network) - } +export async function cleanupJob(): Promise { + await containerPrune() + await containerNetworkPrune() } diff --git a/packages/docker/src/index.ts b/packages/docker/src/index.ts index 4cb91b7..3419c4f 100644 --- a/packages/docker/src/index.ts +++ b/packages/docker/src/index.ts @@ -30,7 +30,7 @@ async function run(): Promise { await prepareJob(args as PrepareJobArgs, responseFile) return exit(0) case Command.CleanupJob: - await cleanupJob(null, state, null) + await cleanupJob() return exit(0) case Command.RunScriptStep: await runScriptStep(args as RunScriptStepArgs, state) diff --git a/packages/docker/tests/cleanup-job-test.ts b/packages/docker/tests/cleanup-job-test.ts index 16c37e7..648718c 100644 --- a/packages/docker/tests/cleanup-job-test.ts +++ b/packages/docker/tests/cleanup-job-test.ts @@ -46,13 +46,6 @@ describe('cleanup job', () => { }) it('should cleanup successfully', async () => { - const prepareJobOutputContent = fs.readFileSync( - prepareJobOutputPath, - 'utf-8' - ) - const parsedPrepareJobOutput = JSON.parse(prepareJobOutputContent) - await expect( - cleanupJob(prepareJobDefinition.args, parsedPrepareJobOutput.state, null) - ).resolves.not.toThrow() + await expect(cleanupJob()).resolves.not.toThrow() }) }) diff --git a/packages/docker/tests/e2e-test.ts b/packages/docker/tests/e2e-test.ts index 00c26a2..ce4b721 100644 --- a/packages/docker/tests/e2e-test.ts +++ b/packages/docker/tests/e2e-test.ts @@ -82,7 +82,7 @@ describe('e2e', () => { await expect( runContainerStep(runContainerStepDefinition.args, resp.state) ).resolves.not.toThrow() - await expect(cleanupJob(resp, resp.state, null)).resolves.not.toThrow() + await expect(cleanupJob()).resolves.not.toThrow() }) it('should prepare job, then run script step, then run container step with Dockerfile then cleanup', async () => { @@ -111,6 +111,6 @@ ENTRYPOINT [ "tail", "-f", "/dev/null" ] await expect( runContainerStep(containerStepDataCopy.args, resp.state) ).resolves.not.toThrow() - await expect(cleanupJob(resp, resp.state, null)).resolves.not.toThrow() + await expect(cleanupJob()).resolves.not.toThrow() }) })