forked from satackey/action-docker-layer-caching
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
e
committed
Nov 5, 2022
1 parent
cc3f382
commit 1184386
Showing
8 changed files
with
610 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,35 +16,35 @@ jobs: | |
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/setup-node@v1 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 12.x | ||
node-version: 16.x | ||
|
||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Output info | ||
id: info | ||
run: | | ||
if [ '${{ github.event_name }}' = 'pull_request' ]; then | ||
echo '::set-output name=commit_message::' | ||
echo "commit_message=" >> $GITHUB_OUTPUT | ||
exit 0 | ||
fi | ||
echo '::set-output name=commit_message::[auto]' | ||
echo "commit_message=[auto]" >> $GITHUB_OUTPUT | ||
- uses: satackey/[email protected].0-beta3 | ||
- uses: jpribyl/[email protected].3 | ||
with: | ||
commit-message: ${{ steps.info.outputs.commit_message }} | ||
push-branch: '{branch}-release' | ||
|
||
- uses: actions/upload-artifact@v2 | ||
- uses: actions/upload-artifact@v3 | ||
if: github.event_name != 'delete' | ||
with: | ||
name: built | ||
path: ./ | ||
|
||
- name: Output matrix | ||
id: set_matrix | ||
uses: actions/github-script@v2 | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
return { | ||
|
@@ -69,12 +69,12 @@ jobs: | |
}, { | ||
inspect_image: 'nothing', | ||
os: 'ubuntu-latest', | ||
prepare_command: 'docker tag node:12 nothing', | ||
prepare_command: 'docker tag node:16 nothing', | ||
build_command: ':', | ||
}, { | ||
inspect_image: 'nothing', | ||
os: 'windows-latest', | ||
prepare_command: 'docker tag mcr.microsoft.com/windows/nanoserver:1809 nothing', | ||
prepare_command: 'docker pull mcr.microsoft.com/windows/nanoserver:1809; docker tag mcr.microsoft.com/windows/nanoserver:1809 nothing', | ||
build_command: ':', | ||
}, { | ||
branch: process.env.GITHUB_REF.replace('refs/heads/', '') | ||
|
@@ -99,18 +99,17 @@ jobs: | |
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v3 | ||
|
||
- run: ${{ matrix.prepare_command }} | ||
|
||
- name: Download action | ||
uses: actions/download-artifact@v2 | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: built | ||
path: action-dlc | ||
|
||
- uses: ./action-dlc | ||
name: Run satackey/action-docker-layer-caching@${{ matrix.branch }} | ||
name: Run jpribyl/action-docker-layer-caching@${{ matrix.branch }} | ||
with: | ||
key: docker-layer-caching-${{ matrix.os }}-${{ matrix.inspect_image }}-sha:${{ github.sha }}-{hash} | ||
|
||
|
@@ -123,23 +122,22 @@ jobs: | |
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v3 | ||
|
||
- run: ${{ matrix.prepare_command }} | ||
|
||
- name: Extract | ||
id: extract | ||
run: | | ||
echo "##[set-output name=branch;]${GITHUB_REF#refs/heads/}" | ||
run: echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT | ||
|
||
- name: Download action | ||
uses: actions/download-artifact@v2 | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: built | ||
path: action-dlc | ||
|
||
- uses: ./action-dlc | ||
name: Run satackey/action-docker-layer-caching@${{ matrix.branch }} | ||
name: Run jpribyl/action-docker-layer-caching@${{ matrix.branch }} | ||
with: | ||
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 }}- | ||
|
@@ -149,7 +147,7 @@ jobs: | |
run: docker inspect ${{ matrix.inspect_image }} | ||
|
||
- name: Get cached image ID | ||
run: echo ::set-output name=id::$(docker image ls -q ${{ matrix.inspect_image }}) | ||
run: echo id=$(docker image ls -q ${{ matrix.inspect_image }}) >> $GITHUB_OUTPUT | ||
id: cached | ||
|
||
- run: ${{ matrix.build_command }} | ||
|
@@ -158,7 +156,7 @@ jobs: | |
run: docker inspect ${{ matrix.inspect_image }} | ||
|
||
- name: Show built image ID | ||
run: echo ::set-output name=id::$(docker image ls -q ${{ matrix.inspect_image }}) | ||
run: echo id=$(docker image ls -q ${{ matrix.inspect_image }}) >> $GITHUB_OUTPUT | ||
id: latest | ||
|
||
- name: Compare cached ID and after build ID | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
import * as core from '@actions/core' | ||
import * as exec from 'actions-exec-listener' | ||
import { LayerCache } from './src/LayerCache' | ||
import { ImageDetector } from './src/ImageDetector' | ||
import * as core from "@actions/core"; | ||
import * as exec from "actions-exec-listener"; | ||
import { LayerCache } from "./src/LayerCache"; | ||
import { ImageDetector } from "./src/ImageDetector"; | ||
|
||
const main = async () => { | ||
const primaryKey = core.getInput(`key`, { required: true }) | ||
const restoreKeys = core.getInput(`restore-keys`, { required: false }).split(`\n`).filter(key => key !== ``) | ||
const primaryKey = core.getInput(`key`, { required: true }); | ||
const restoreKeys = core | ||
.getInput(`restore-keys`, { required: false }) | ||
.split(`\n`) | ||
.filter((key) => key !== ``); | ||
|
||
const imageDetector = new ImageDetector() | ||
const imageDetector = new ImageDetector(); | ||
|
||
const alreadyExistingImages = await imageDetector.getExistingImages() | ||
core.saveState(`already-existing-images`, JSON.stringify(alreadyExistingImages)) | ||
const alreadyExistingImages = await imageDetector.getExistingImages(); | ||
core.saveState( | ||
`already-existing-images`, | ||
JSON.stringify(alreadyExistingImages) | ||
); | ||
|
||
const layerCache = new LayerCache([]) | ||
layerCache.concurrency = parseInt(core.getInput(`concurrency`, { required: true }), 10) | ||
const restoredKey = await layerCache.restore(primaryKey, restoreKeys) | ||
await layerCache.cleanUp() | ||
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(`restored-images`, JSON.stringify(await imageDetector.getImagesShouldSave(alreadyExistingImages))) | ||
} | ||
core.saveState( | ||
`restored-key`, | ||
JSON.stringify(restoredKey !== undefined ? restoredKey : "") | ||
); | ||
core.saveState( | ||
`restored-images`, | ||
JSON.stringify( | ||
await imageDetector.getImagesShouldSave(alreadyExistingImages) | ||
) | ||
); | ||
}; | ||
|
||
main().catch(e => { | ||
console.error(e) | ||
core.setFailed(e) | ||
main().catch((e) => { | ||
console.error(e); | ||
core.setFailed(e); | ||
|
||
core.saveState(`restored-key`, JSON.stringify(``)) | ||
core.saveState(`restored-images`, JSON.stringify([])) | ||
}) | ||
core.saveState(`restored-key`, JSON.stringify(``)); | ||
core.saveState(`restored-images`, JSON.stringify([])); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
import * as exec from 'actions-exec-listener' | ||
import * as core from '@actions/core' | ||
import * as exec from "actions-exec-listener"; | ||
import * as core from "@actions/core"; | ||
|
||
export class ImageDetector { | ||
async getExistingImages(): Promise<string[]> { | ||
const existingSet = new Set<string>([]) | ||
const ids = (await exec.exec(`docker image ls -q`, [], { silent: true, listeners: { stderr: console.warn }})).stdoutStr.split(`\n`).filter(id => id !== ``) | ||
const repotags = (await exec.exec(`docker`, `image ls --format {{.Repository}}:{{.Tag}} --filter dangling=false`.split(' '), { silent: true, listeners: { stderr: console.warn }})).stdoutStr.split(`\n`).filter(id => id !== ``); | ||
const existingSet = new Set<string>([]); | ||
const ids = ( | ||
await exec.exec(`docker image ls -q`, [], { | ||
silent: true, | ||
listeners: { stderr: console.warn }, | ||
}) | ||
).stdoutStr | ||
.split(`\n`) | ||
.filter((id) => id !== ``); | ||
const repotags = ( | ||
await exec.exec( | ||
`docker`, | ||
`image ls --format {{.Repository}}:{{.Tag}} --filter dangling=false`.split( | ||
" " | ||
), | ||
{ silent: true, listeners: { stderr: console.warn } } | ||
) | ||
).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 })) | ||
return Array.from(existingSet) | ||
[...ids, ...repotags].forEach((image) => existingSet.add(image)); | ||
core.debug(JSON.stringify({ existingSet })); | ||
return Array.from(existingSet); | ||
} | ||
|
||
async getImagesShouldSave(alreadRegisteredImages: string[]): Promise<string[]> { | ||
const resultSet = new Set(await this.getExistingImages()) | ||
alreadRegisteredImages.forEach(image => resultSet.delete(image)) | ||
return Array.from(resultSet) | ||
async getImagesShouldSave( | ||
alreadRegisteredImages: string[] | ||
): Promise<string[]> { | ||
const resultSet = new Set(await this.getExistingImages()); | ||
alreadRegisteredImages.forEach((image) => resultSet.delete(image)); | ||
return Array.from(resultSet); | ||
} | ||
} |
Oops, something went wrong.