Skip to content

Commit

Permalink
Merge pull request #22 from satackey/fix_layer_cache_not_found
Browse files Browse the repository at this point in the history
Fix layer cache not found (#19)
  • Loading branch information
satackey authored Aug 15, 2020
2 parents a4aecb7 + 50cf92b commit 3b93fec
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 136 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ jobs:
script: |
return {
inspect_image: [
'test_project_node',
'amazon/aws-cli',
'test_project_scratch',
'hello-world',
],
include: [
{
inspect_image: 'test_project_node',
inspect_image: 'test_project_scratch',
prepare_command: 'docker-compose -f test_project/docker-compose.yml -p test_project pull',
build_command: 'docker-compose -f test_project/docker-compose.yml -p test_project build',
}, {
inspect_image: 'amazon/aws-cli',
inspect_image: 'hello-world',
prepare_command: ':',
build_command: 'docker pull amazon/aws-cli',
build_command: 'docker pull hello-world',
},
],
}
Expand Down Expand Up @@ -91,7 +91,6 @@ jobs:
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
with:
key: docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}
restore-keys: docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-

- run: ${{ matrix.build_command }}

Expand Down Expand Up @@ -119,7 +118,7 @@ jobs:
- uses: ./action-dlc
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
with:
key: docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}
key: never-restored-docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}
restore-keys: docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-
skip-save: 'true'

Expand Down
7 changes: 5 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { LayerCache } from './src/LayerCache'
import { ImageDetector } from './src/ImageDetector'

const main = async () => {
// const repotag = core.getInput(`repotag`, { required: true })
const primaryKey = core.getInput(`key`, { required: true })
const restoreKeys = core.getInput(`restore-keys`, { required: false }).split(`\n`).filter(key => key !== ``)

core.saveState(`already-existing-images`, JSON.stringify(await new ImageDetector().getExistingImages()))
const imageDetector = new ImageDetector()

const alreadyExistingImages = await imageDetector.getExistingImages()

const layerCache = new LayerCache([])
layerCache.concurrency = parseInt(core.getInput(`concurrency`, { required: true }), 10)
const restoredKey = await layerCache.restore(primaryKey, restoreKeys)
await layerCache.cleanUp()

core.saveState(`restored-key`, JSON.stringify(restoredKey !== undefined ? restoredKey : ''))
core.saveState(`already-existing-images`, JSON.stringify(alreadyExistingImages))
core.saveState(`restored-images`, JSON.stringify(await imageDetector.getImagesShouldSave(alreadyExistingImages)))
}

main().catch(e => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"dependencies": {
"@actions/cache": "^1.0.2",
"@actions/core": "^1.2.4",
"@types/recursive-readdir": "^2.2.0",
"actions-exec-listener": "^0.0.2",
"crypto": "^1.0.1",
"native-promise-pool": "^3.13.0",
"recursive-readdir": "^2.2.2",
"string-format": "^2.0.0",
"typescript-is": "^0.16.3"
},
Expand Down
27 changes: 12 additions & 15 deletions post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as core from '@actions/core'
import exec from 'actions-exec-listener'

import { LayerCache } from './src/LayerCache'
import { ImageDetector } from './src/ImageDetector'
Expand All @@ -12,26 +11,24 @@ const main = async () => {
}

const primaryKey = core.getInput('key', { required: true })
const restoredKey = JSON.parse(core.getState(`restored-key`)) as string

const rawAlreadyExistingImages = core.getState(`already-existing-images`)
assertType<string>(rawAlreadyExistingImages)
const alreadyExistingImages = JSON.parse(rawAlreadyExistingImages)
const restoredKey = JSON.parse(core.getState(`restored-key`))
const alreadyExistingImages = JSON.parse(core.getState(`already-existing-images`))
const restoredImages = JSON.parse(core.getState(`restored-images`))

assertType<string>(restoredKey)
assertType<string[]>(alreadyExistingImages)
assertType<string[]>(restoredImages)

const imageDetector = new ImageDetector()
imageDetector.registerAlreadyExistedImages(alreadyExistingImages)
await imageDetector.getExistingImages()
core.debug(JSON.stringify({ imageIdsToSave: imageDetector.getImagesShouldSave() }))
const layerCache = new LayerCache(imageDetector.getImagesShouldSave())
layerCache.concurrency = parseInt(core.getInput(`concurrency`, { required: true }), 10)

layerCache.unformattedOrigianlKey = primaryKey
core.debug(JSON.stringify({ restoredKey, formattedOriginalCacheKey: layerCache.getFormattedOriginalCacheKey()}))
if (restoredKey !== `` && restoredKey === layerCache.getFormattedOriginalCacheKey()) {
core.info(`Key ${restoredKey} already exists, skip storing.`)
if (await imageDetector.checkIfImageHasAdded(restoredImages)) {
core.info(`Key ${restoredKey} already exists, not saving cache.`)
return
}

const layerCache = new LayerCache(await imageDetector.getImagesShouldSave(alreadyExistingImages))
layerCache.concurrency = parseInt(core.getInput(`concurrency`, { required: true }), 10)

await layerCache.store(primaryKey)
await layerCache.cleanUp()
}
Expand Down
24 changes: 12 additions & 12 deletions src/ImageDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import exec from 'actions-exec-listener'
import * as core from '@actions/core'

export class ImageDetector {
alreadyExistedImages: Set<string> = new Set([])
existingImages: Set<string> = new Set([])
registerAlreadyExistedImages(images: string[]) {
images.forEach(image => this.alreadyExistedImages.add(image))
}

async getExistingImages(): Promise<string[]> {
const existingSet = new Set<string>([])
const ids = (await exec.exec(`docker image ls -q`, [], { silent: true })).stdoutStr.split(`\n`).filter(id => id !== ``)
const repotags = (await exec.exec(`sh -c "docker image ls --format '{{ .Repository }}:{{ .Tag }}' --filter 'dangling=false'"`, [], { silent: true })).stdoutStr.split(`\n`).filter(id => id !== ``);
core.debug(JSON.stringify({ log: "getExistingImages", ids, repotags }));
([...ids, ...repotags]).forEach(image => this.existingImages.add(image))
core.debug(JSON.stringify({ existingImages: this.existingImages }))
return Array.from(this.existingImages)
([...ids, ...repotags]).forEach(image => existingSet.add(image))
core.debug(JSON.stringify({ existingSet }))
return Array.from(existingSet)
}

getImagesShouldSave(): string[] {
const resultSet = new Set(this.existingImages.values())
this.alreadyExistedImages.forEach(image => resultSet.delete(image))
async getImagesShouldSave(alreadRegisteredImages: string[]): Promise<string[]> {
const resultSet = new Set(await this.getExistingImages())
alreadRegisteredImages.forEach(image => resultSet.delete(image))
return Array.from(resultSet)
}

async checkIfImageHasAdded(restoredImages: string[]): Promise<boolean> {
const existing = await this.getExistingImages()
return JSON.stringify(restoredImages) === JSON.stringify(existing)
}
}
Loading

0 comments on commit 3b93fec

Please sign in to comment.