-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathImageDetector.ts
More file actions
36 lines (33 loc) · 1.4 KB
/
ImageDetector.ts
File metadata and controls
36 lines (33 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as exec from 'actions-exec-listener'
import * as core from '@actions/core'
export class ImageDetector {
async getExistingImages(): Promise<string[]> {
const existingSet = new Set<string>([])
const ids = (await exec.exec(`docker image ls -q`, [], { silent: true, listeners: { stderr: console.warn }})).stdoutStr.split(`\n`).filter(id => id !== ``)
const repotags = await this.getExistingImageIdentifiers()
core.debug(JSON.stringify({ log: "getExistingImages", ids, repotags }));
([...ids, ...repotags]).forEach(image => existingSet.add(image))
core.debug(JSON.stringify({ existingSet }))
return Array.from(existingSet)
}
async getExistingImageIdentifiers(): Promise<string[]> {
return (
await exec.exec(
`docker`, `image ls --format {{.Repository}}:{{.Tag}}:{{.ID}} --filter dangling=false`.split(' '),
{ silent: true, listeners: { stderr: console.warn }}
)
)
.stdoutStr
.split(`\n`)
.filter(img => img !== ``)
.map(img => {
const [repository, tag, id] = img.split(':')
return (tag == '<none>' ? id : `${repository}:${tag}`)
})
}
async getImagesShouldSave(alreadRegisteredImages: string[]): Promise<string[]> {
const resultSet = new Set(await this.getExistingImages())
alreadRegisteredImages.forEach(image => resultSet.delete(image))
return Array.from(resultSet)
}
}