Skip to content

Commit 165a53d

Browse files
committed
test
1 parent 2d2ac71 commit 165a53d

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import * as core from '@actions/core'
2-
2+
import exec from 'actions-exec-listener'
33
import { LayerCache } from './src/LayerCache'
44

55
const main = async () => {
66
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

10-
const layerCache = new LayerCache(repotag)
10+
core.saveState(`already-existing-image-ids`, (await exec.exec(`docker image ls -q`)).stdoutStr.split(`\n`).filter(id => id !== ``))
11+
12+
const layerCache = new LayerCache([repotag])
1113
const restoredKey = await layerCache.restore(primaryKey, restoreKeys)
1214
await layerCache.cleanUp()
1315

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
"@actions/cache": "^0.2.1",
1010
"@actions/core": "^1.2.4",
1111
"actions-exec-listener": "^0.0.1",
12+
"crypto": "^1.0.1",
13+
"string-format": "^2.0.0",
1214
"typescript-is": "^0.16.2"
1315
},
1416
"devDependencies": {
1517
"@types/node": "^14.0.11",
18+
"@types/string-format": "^2.0.0",
1619
"@zeit/ncc": "^0.22.3",
1720
"ts-node": "^8.10.2",
1821
"ttypescript": "^1.5.10",

post.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from '@actions/core'
2+
import exec from 'actions-exec-listener'
23

34
import { LayerCache } from './src/LayerCache'
45
const main = async () => {
@@ -10,7 +11,14 @@ const main = async () => {
1011
return
1112
}
1213

13-
const layerCache = new LayerCache(repotag)
14+
const alreadyExistingImageIds = JSON.parse(core.getState(`already-existing-image-ids`)) as string[]
15+
16+
const currentImageIds = (await exec.exec(`docker image ls -q`)).stdoutStr.split(`\n`).filter(id => id !== ``)
17+
18+
const imageIdsToSave = new Set([...currentImageIds])
19+
alreadyExistingImageIds.forEach(id => imageIdsToSave.delete(id))
20+
21+
const layerCache = new LayerCache(Array.from(imageIdsToSave))
1422
await layerCache.store(primaryKey)
1523
await layerCache.cleanUp()
1624
}

src/LayerCache.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import * as path from 'path'
22
import exec from 'actions-exec-listener'
3+
import crypto from 'crypto'
34
import * as core from '@actions/core'
45
import * as cache from '@actions/cache'
56
import { ExecOptions } from '@actions/exec/lib/interfaces'
67
import { promises as fs } from 'fs'
78
import { assertManifests, Manifest, Manifests } from './Tar'
9+
import format from 'string-format'
810

911
class LayerCache {
10-
repotag: string
12+
// repotag: string
13+
ids: string[]
1114
originalKeyToStore: string = ''
1215
// tarFile: string = ''
1316
imagesDir: string = path.resolve(`${process.cwd()}/./.action-docker-layer-caching-docker_images`)
1417
enabledParallel = true
1518
// unpackedTarDir: string = ''
1619
// manifests: Manifests = []
1720

18-
constructor(repotag: string) {
19-
this.repotag = repotag
21+
constructor(ids: string[]) {
22+
// this.repotag = repotag
23+
this.ids = ids
2024
}
2125

2226
async exec(command: string, args?: string[], options?: ExecOptions) {
@@ -30,6 +34,9 @@ class LayerCache {
3034

3135
async store(key: string) {
3236
this.originalKeyToStore = key
37+
// this.originalKeyToStore = format(key, {
38+
// hash: this.getIdhashesPathFriendly()
39+
// })
3340
await this.saveImageAsUnpacked()
3441
if (this.enabledParallel) {
3542
await this.separateAllLayerCaches()
@@ -46,7 +53,7 @@ class LayerCache {
4653

4754
private async saveImageAsUnpacked() {
4855
await this.exec('mkdir -p', [this.getSavedImageTarDir()])
49-
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady([this.repotag])).join(`' '`)}' | tar xf - -C ${this.getSavedImageTarDir()}`])
56+
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C ${this.getSavedImageTarDir()}`])
5057
}
5158

5259
private async makeRepotagsDockerSaveArgReady(repotags: string[]): Promise<string[]> {
@@ -191,19 +198,19 @@ class LayerCache {
191198
}
192199

193200
getUnpackedTarDir(): string {
194-
return `${this.getImagesDir()}/${this.getRepotagPathFriendly()}`
201+
return `${this.getImagesDir()}/${this.getIdhashesPathFriendly()}`
195202
}
196203

197204
getLayerCachesDir() {
198205
return `${this.getUnpackedTarDir()}-layers`
199206
}
200207

201208
getSavedImageTarDir(): string {
202-
return `${this.getImagesDir()}/${this.getRepotagPathFriendly()}`
209+
return `${this.getImagesDir()}/${this.getIdhashesPathFriendly()}`
203210
}
204211

205-
getRepotagPathFriendly(): string {
206-
return this.repotag.replace('/', '-')
212+
getIdhashesPathFriendly(): string {
213+
return crypto.createHash(`sha256`).update(this.ids.join(`-`), `utf8`).digest(`hex`)
207214
}
208215

209216
getRootKey(): string {

yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.11.tgz#61d4886e2424da73b7b25547f59fdcb534c165a3"
5353
integrity sha512-lCvvI24L21ZVeIiyIUHZ5Oflv1hhHQ5E1S25IRlKIXaRkVgmXpJMI3wUJkmym2bTbCe+WoIibQnMVAU3FguaOg==
5454

55+
"@types/string-format@^2.0.0":
56+
version "2.0.0"
57+
resolved "https://registry.yarnpkg.com/@types/string-format/-/string-format-2.0.0.tgz#c1588f507be7b8ef5eb5074a41e48e4538f3f6d5"
58+
integrity sha512-mMwtmgN0ureESnJ3SuMM4W9lsi4CgOxs43YxNo14SDHgzJ+OPYO3yM7nOTJTh8x5YICseBdtrySUbvxnpb+NYQ==
59+
5560
"@zeit/ncc@^0.22.3":
5661
version "0.22.3"
5762
resolved "https://registry.yarnpkg.com/@zeit/ncc/-/ncc-0.22.3.tgz#fca6b86b4454ce7a7e1e7e755165ec06457f16cd"
@@ -92,6 +97,11 @@ concat-map@0.0.1:
9297
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
9398
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
9499

100+
crypto@^1.0.1:
101+
version "1.0.1"
102+
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
103+
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==
104+
95105
diff@^4.0.1:
96106
version "4.0.2"
97107
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
@@ -149,6 +159,11 @@ source-map@^0.6.0:
149159
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
150160
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
151161

162+
string-format@^2.0.0:
163+
version "2.0.0"
164+
resolved "https://registry.yarnpkg.com/string-format/-/string-format-2.0.0.tgz#f2df2e7097440d3b65de31b6d40d54c96eaffb9b"
165+
integrity sha512-bbEs3scLeYNXLecRRuk6uJxdXUSj6le/8rNPHChIJTn2V79aXVTR1EH2OH5zLKKoz0V02fOUKZZcw01pLUShZA==
166+
152167
ts-node@^8.10.2:
153168
version "8.10.2"
154169
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.2.tgz#eee03764633b1234ddd37f8db9ec10b75ec7fb8d"

0 commit comments

Comments
 (0)