11import * as path from 'path'
22import exec from 'actions-exec-listener'
3+ import crypto from 'crypto'
34import * as core from '@actions/core'
45import * as cache from '@actions/cache'
56import { ExecOptions } from '@actions/exec/lib/interfaces'
67import { promises as fs } from 'fs'
78import { assertManifests , Manifest , Manifests } from './Tar'
9+ import format from 'string-format'
810
911class 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 {
0 commit comments