Skip to content

Commit

Permalink
Merge pull request #97 from dependabot/brrygrdn/fix-container-network…
Browse files Browse the repository at this point in the history
…-cleanup

Fix cleanup of network and containers
  • Loading branch information
Barry Gordon authored and GitHub committed Feb 28, 2022
2 parents 4f8db74 + 40702b6 commit 253310b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
15 changes: 15 additions & 0 deletions __tests__/cleanup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as core from '@actions/core'

import {run} from '../src/cleanup'

describe('run', () => {
beforeEach(async () => {
jest.spyOn(core, 'error').mockImplementation(jest.fn())
})

test('it does not log any errors interacting with Docker by default', async () => {
await run()

expect(core.error).not.toHaveBeenCalled()
})
})
13 changes: 10 additions & 3 deletions dist/cleanup/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/cleanup/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"prepare": "husky install",
"dependabot": "ts-node src/cli.ts",
"fetch-images": "ts-node src/fetch-images.ts",
"cleanup-docker": "ts-node src/cleanup.ts",
"update-container-manifest": "ts-node src/update-containers.ts"
},
"repository": {
Expand Down
13 changes: 10 additions & 3 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import * as core from '@actions/core'
import Docker from 'dockerode'

export async function run(): Promise<void> {
// This method performs housekeeping checks to remove Docker artifacts
// which were left behind by old versions of the action or any jobs
// which may have crashed before deleting their own containers or networks
//
// cutoff - a Go duration string to pass to the Docker API's 'until' argument, default '24h'
export async function run(cutoff = '24h'): Promise<void> {
try {
const docker = new Docker()
const untilFilter = JSON.stringify({until: '24h'})
const untilFilter = {until: [cutoff]}
core.info(`Pruning networks older than ${cutoff}`)
await docker.pruneNetworks({filters: untilFilter})
core.info(`Pruning containers older than ${cutoff}`)
await docker.pruneContainers({filters: untilFilter})
} catch (error) {
core.debug(`Error cleaning up: ${error.message}`)
core.error(`Error cleaning up: ${error.message}`)
}
}

Expand Down

0 comments on commit 253310b

Please sign in to comment.