Skip to content

Commit bfabbd4

Browse files
committed
WIP
1 parent 165a53d commit bfabbd4

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

.github/workflows/release.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ jobs:
5454
- uses: ./action-dlc
5555
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
5656
with:
57-
repotag: test_project_node
58-
key: docker-layer-caching-test_project_node-sha:${{ github.sha }}
57+
key: docker-layer-caching-test_project_node-sha:${{ github.sha }}-{hash}
58+
restore-keys: docker-layer-caching-test_project_node-sha:${{ github.sha }}-
5959

6060
- run: docker-compose -f test_project/docker-compose.yml -p test_project build
6161

@@ -81,8 +81,9 @@ jobs:
8181
- uses: ./action-dlc
8282
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
8383
with:
84-
repotag: test_project_node
85-
key: docker-layer-caching-test_project_node-sha:${{ github.sha }}
84+
# repotag: test_project_node
85+
key: docker-layer-caching-test_project_node-sha:${{ github.sha }}-{hash}
86+
restore-keys: docker-layer-caching-test_project_node-sha:${{ github.sha }}-
8687

8788
- name: Show cached image info
8889
run: docker inspect test_project_node
@@ -132,8 +133,8 @@ jobs:
132133
- uses: ./action-dlc
133134
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
134135
with:
135-
repotag: amazon/aws-cli
136-
key: docker-layer-caching-aws-cli-sha:${{ github.sha }}
136+
key: docker-layer-caching-aws-cli-sha:${{ github.sha }}-{hash}
137+
restore-keys: docker-layer-caching-aws-cli-sha:${{ github.sha }}-
137138

138139
- run: docker run --rm amazon/aws-cli --version
139140

@@ -157,8 +158,8 @@ jobs:
157158
- uses: ./action-dlc
158159
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
159160
with:
160-
repotag: amazon/aws-cli
161-
key: docker-layer-caching-aws-cli-sha:${{ github.sha }}
161+
key: docker-layer-caching-aws-cli-sha:${{ github.sha }}-{hash}
162+
restore-keys: docker-layer-caching-aws-cli-sha:${{ github.sha }}-
162163

163164
- name: Show cached image info
164165
run: docker inspect amazon/aws-cli

main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import exec from 'actions-exec-listener'
33
import { LayerCache } from './src/LayerCache'
44

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

1010
core.saveState(`already-existing-image-ids`, (await exec.exec(`docker image ls -q`)).stdoutStr.split(`\n`).filter(id => id !== ``))
1111

12-
const layerCache = new LayerCache([repotag])
12+
const layerCache = new LayerCache([])
1313
const restoredKey = await layerCache.restore(primaryKey, restoreKeys)
1414
await layerCache.cleanUp()
1515

16-
core.saveState(`exists-primary-key`, primaryKey === restoredKey)
16+
core.saveState(`restored-key`, JSON.stringify(restoredKey !== undefined ? restoredKey : ''))
1717
}
1818

1919
main().catch(e => {

post.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ import exec from 'actions-exec-listener'
33

44
import { LayerCache } from './src/LayerCache'
55
const main = async () => {
6-
const repotag = core.getInput('repotag', { required: true })
76
const primaryKey = core.getInput('key', { required: true })
8-
9-
if (JSON.parse(core.getState(`exists-primary-key`)) === true) {
10-
core.info(`Key ${primaryKey} already exists, aborting.`)
11-
return
12-
}
13-
7+
const restoredKey = JSON.parse(core.getState(`restored-key`)) as string
148
const alreadyExistingImageIds = JSON.parse(core.getState(`already-existing-image-ids`)) as string[]
15-
169
const currentImageIds = (await exec.exec(`docker image ls -q`)).stdoutStr.split(`\n`).filter(id => id !== ``)
17-
1810
const imageIdsToSave = new Set([...currentImageIds])
1911
alreadyExistingImageIds.forEach(id => imageIdsToSave.delete(id))
2012

2113
const layerCache = new LayerCache(Array.from(imageIdsToSave))
14+
15+
core.debug(JSON.stringify({ restoredKey, formattedOriginalCacheKey: layerCache.getFormattedOriginalCacheKey()}))
16+
if (restoredKey !== `` && restoredKey === layerCache.getFormattedOriginalCacheKey()) {
17+
core.info(`Key ${restoredKey} already exists, aborting.`)
18+
return
19+
}
2220
await layerCache.store(primaryKey)
2321
await layerCache.cleanUp()
2422
}

src/LayerCache.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import format from 'string-format'
1111
class LayerCache {
1212
// repotag: string
1313
ids: string[]
14-
originalKeyToStore: string = ''
14+
originalKeyThatMayUnformatted: string = ''
1515
// tarFile: string = ''
1616
imagesDir: string = path.resolve(`${process.cwd()}/./.action-docker-layer-caching-docker_images`)
1717
enabledParallel = true
@@ -33,7 +33,7 @@ class LayerCache {
3333
}
3434

3535
async store(key: string) {
36-
this.originalKeyToStore = key
36+
this.originalKeyThatMayUnformatted = key
3737
// this.originalKeyToStore = format(key, {
3838
// hash: this.getIdhashesPathFriendly()
3939
// })
@@ -141,7 +141,7 @@ class LayerCache {
141141
// ---
142142

143143
async restore(key: string, restoreKeys?: string[]) {
144-
this.originalKeyToStore = key
144+
this.originalKeyThatMayUnformatted = key
145145
// const restoreKeysIncludedRootKey = [key, ...(restoreKeys !== undefined ? restoreKeys : [])]
146146
const restoredCacheKey = await this.restoreRoot(restoreKeys)
147147
if (restoredCacheKey === undefined) {
@@ -167,8 +167,8 @@ class LayerCache {
167167
if (restoredCacheKeyMayUndefined === undefined) {
168168
return undefined
169169
}
170-
this.originalKeyToStore = restoredCacheKeyMayUndefined.replace(/-root$/, '')
171-
return this.originalKeyToStore
170+
this.originalKeyThatMayUnformatted = restoredCacheKeyMayUndefined.replace(/-root$/, '')
171+
return this.originalKeyThatMayUnformatted
172172
}
173173

174174
private async restoreLayers() {
@@ -214,15 +214,21 @@ class LayerCache {
214214
}
215215

216216
getRootKey(): string {
217-
return `${this.originalKeyToStore}-root`
217+
return `${this.getFormattedOriginalCacheKey()}-root`
218218
}
219219

220220
genSingleLayerStorePath(id: string) {
221221
return `${this.getLayerCachesDir()}/${id}/layer.tar`
222222
}
223223

224224
genSingleLayerStoreKey(id: string) {
225-
return `layer-${this.originalKeyToStore}-${id}`
225+
return `layer-${this.getFormattedOriginalCacheKey()}-${id}`
226+
}
227+
228+
getFormattedOriginalCacheKey() {
229+
return format(this.originalKeyThatMayUnformatted, {
230+
hash: this.getIdhashesPathFriendly()
231+
})
226232
}
227233

228234
async getLayerTarFiles(): Promise<string[]> {

0 commit comments

Comments
 (0)