Skip to content

Commit

Permalink
Merge pull request #23 from satackey/fix_#20_windows_runner
Browse files Browse the repository at this point in the history
Fix bug running on Windows (#20)
  • Loading branch information
satackey authored Aug 15, 2020
2 parents b69d586 + a26d576 commit 4bd1c2d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
36 changes: 24 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
pull_request:
delete:

defaults:
run:
shell: bash

jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -48,6 +52,10 @@ jobs:
'test_project_scratch',
'hello-world',
],
os: [
'ubuntu-latest',
'windows-latest',
],
include: [
{
inspect_image: 'test_project_scratch',
Expand All @@ -57,6 +65,14 @@ jobs:
inspect_image: 'hello-world',
prepare_command: ':',
build_command: 'docker pull hello-world',
}, {
branch: process.env.GITHUB_REF.replace('refs/heads/', '')
}
],
exclude: [
{
inspect_image: 'test_project_scratch',
os: 'windows-latest',
},
],
}
Expand All @@ -69,36 +85,32 @@ jobs:
needs: build
strategy:
matrix: ${{ fromJSON(needs.build.outputs.matrix) }}
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2

- run: ${{ matrix.prepare_command }}

- name: Extract
id: extract
run: |
echo "##[set-output name=branch;]${GITHUB_REF#refs/heads/}"
- name: Download action
uses: actions/download-artifact@v2
with:
name: built
path: action-dlc

- uses: ./action-dlc
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
name: Run satackey/action-docker-layer-caching@${{ matrix.branch }}
with:
key: docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}
key: docker-layer-caching-${{ matrix.os }}-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}

- run: ${{ matrix.build_command }}

test_restoring:
needs: [build, test_saving]
strategy:
matrix: ${{ fromJSON(needs.build.outputs.matrix) }}
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2

Expand All @@ -116,10 +128,10 @@ jobs:
path: action-dlc

- uses: ./action-dlc
name: Run satackey/action-docker-layer-caching@${{ steps.extract.outputs.branch }}
name: Run satackey/action-docker-layer-caching@${{ matrix.branch }}
with:
key: never-restored-docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}
restore-keys: docker-layer-caching-${{ matrix.inspect_image }}-sha:${{ github.sha }}-
key: never-restored-docker-layer-caching-${{ matrix.os }}-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash}
restore-keys: docker-layer-caching-${{ matrix.os }}-${{ matrix.inspect_image }}-sha:${{ github.sha }}-
skip-save: 'true'

- name: Show cached image info
Expand Down
2 changes: 1 addition & 1 deletion src/ImageDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class ImageDetector {
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 !== ``);
const repotags = (await exec.exec(`docker`, `image ls --format {{.Repository}}:{{.Tag}} --filter dangling=false`.split(' '), { silent: true })).stdoutStr.split(`\n`).filter(id => id !== ``);
core.debug(JSON.stringify({ log: "getExistingImages", ids, repotags }));
([...ids, ...repotags]).forEach(image => existingSet.add(image))
core.debug(JSON.stringify({ existingSet }))
Expand Down
29 changes: 18 additions & 11 deletions src/LayerCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class LayerCache {
}

private async saveImageAsUnpacked() {
await this.exec('mkdir -p', [this.getSavedImageTarDir()], { silent: true })
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C ${this.getSavedImageTarDir()}`])
await fs.mkdir(this.getSavedImageTarDir(), { recursive: true })
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C .`], { cwd: this.getSavedImageTarDir() })
}

private async makeRepotagsDockerSaveArgReady(repotags: string[]): Promise<string[]> {
Expand Down Expand Up @@ -133,14 +133,15 @@ class LayerCache {
}
}

private async storeSingleLayerBy(id: string): Promise<number> {
const path = this.genSingleLayerStorePath(id)
const key = await this.generateSingleLayerSaveKey(id)
private async storeSingleLayerBy(layerId: string): Promise<number> {
const path = this.genSingleLayerStorePath(layerId)
const key = await this.generateSingleLayerSaveKey(layerId)

core.info(`Start storing layer cache: ${key}`)
core.info(`Start storing layer cache: ${JSON.stringify({ layerId, key })}`)
const cacheId = await LayerCache.dismissError(cache.saveCache([path], key), LayerCache.ERROR_CACHE_ALREAD_EXISTS_STR, -1)
core.info(`Stored layer cache, key: ${key}, id: ${cacheId}`)
core.info(`Stored layer cache: ${JSON.stringify({ key, cacheId })}`)

core.debug(JSON.stringify({ log: `storeSingleLayerBy`, layerId, path, key, cacheId}))
return cacheId
}

Expand Down Expand Up @@ -177,8 +178,9 @@ class LayerCache {
}

private async restoreLayers(): Promise<boolean> {
const pool = new PromisePool(this.concurrency)


const pool = new PromisePool(this.concurrency)
const tasks = (await this.getLayerIds()).map(
layerId => pool.open(() => this.restoreSingleLayerBy(layerId))
)
Expand All @@ -201,9 +203,14 @@ class LayerCache {
}

private async restoreSingleLayerBy(id: string): Promise<string> {
core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id }))
const path = this.genSingleLayerStorePath(id)
const key = await this.recoverSingleLayerKey(id)
const dir = path.replace(/[^/\\]+$/, ``)

core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id, path, dir, key }))

const result = await cache.restoreCache([this.genSingleLayerStorePath(id)], await this.recoverSingleLayerKey(id))
await fs.mkdir(dir, { recursive: true })
const result = await cache.restoreCache([path], key)

if (result == null) {
throw new Error(`${LayerCache.ERROR_LAYER_CACHE_NOT_FOUND_STR}: ${JSON.stringify({ id })}`)
Expand Down Expand Up @@ -243,7 +250,7 @@ class LayerCache {
}

genSingleLayerStorePath(id: string) {
return `${this.getLayerCachesDir()}/${id}/layer.tar`
return path.resolve(`${this.getLayerCachesDir()}/${id}/layer.tar`)
}

async generateRootHashFromManifest(): Promise<string> {
Expand Down
1 change: 1 addition & 0 deletions src/Tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function assertManifests(x: unknown): asserts x is Manifests {
export async function loadRawManifests(path: string) {
return (await fs.readFile(`${path}/manifest.json`)).toString()
}

export async function loadManifests(path: string) {
const raw = await loadRawManifests(path)
const manifests = JSON.parse(raw.toString())
Expand Down

0 comments on commit 4bd1c2d

Please sign in to comment.