From b542b6aa1b00f9081647557113367e26e9749048 Mon Sep 17 00:00:00 2001 From: Matthias Kadenbach Date: Sun, 27 Sep 2020 17:24:43 -0700 Subject: [PATCH] prepare release --- README.md | 8 ++++---- dist/index.js | 47 +++++++++-------------------------------------- index.js | 11 ++++------- 3 files changed, 17 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index e7a19d0..af85dc2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Cached Docker Build -This Github Action stores and retrieves a Docker image from Github's cache. -It ueses the official [actions/cache](https://github.com/actions/toolkit/tree/main/packages/cache) library. +This Github Action caches docker builds using the +official [actions/cache](https://github.com/actions/toolkit/tree/main/packages/cache) library. ## Github Action Inputs @@ -17,8 +17,8 @@ It ueses the official [actions/cache](https://github.com/actions/toolkit/tree/ma ``` uses: mattes/cached-docker-build-action@v1 with: - args: --pull --file Dockerfile --tag my-image:tag . - cache_key: ${{ hashFiles('**/lockfiles') }} + args: "--pull --file Dockerfile --tag my-image:tag ." + cache_key: "${{ hashFiles('**/lockfiles') }}" ``` ## Future work diff --git a/dist/index.js b/dist/index.js index 21fcd82..c5f77d0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -19,31 +19,27 @@ const duration = __webpack_require__(3805); // read and validate inputs const dockerBuildArgs = process.env["INPUT_ARGS"] || "" const cacheKey = process.env["INPUT_CACHE_KEY"] || "" - const expiresStr = process.env["INPUT_EXPIRES"] || "" const runnerTemp = process.env['RUNNER_TEMP'] || "" if (runnerTemp == "") { - core.setFailed("RUNNER_TEMP env var missing") - return + abort("RUNNER_TEMP env var missing") } if (dockerBuildArgs == "") { - core.setFailed("docker build args missing") - return + abort("docker build args missing") } // parse docker build args const dockerBuildTags = getDockerBuildTags(dockerBuildArgs) if (dockerBuildTags.length == 0) { - core.setFailed("docker build args require at least one --tag") - return + abort("docker build args require at least one --tag") } - const primaryKey = sha256(`${cacheKey} ${dockerBuildArgs} ${expiresStr}`) + const primaryKey = sha256(`${cacheKey} ${dockerBuildArgs}`) const cachePath = path.join(runnerTemp, "cached-docker-build", primaryKey) let cacheHit = false - core.info(`Cached key: ${primaryKey}`) + core.info(`Cache Key Hash: ${primaryKey}`) // try to restore cachePath from Github cache try { @@ -59,37 +55,17 @@ const duration = __webpack_require__(3805); } } - // load docker image if it was cached and not expired + // load docker image if it was cached if (cacheHit) { - let expires = 0; - try { - expires = Number(fs.readFileSync(path.join(cachePath, ".meta.expires"))); - } catch (err) {} - - if (expires > 0 && Date.now() >= expires) { - core.info("Cache is expired") - } else { - exec(`docker load -i ${path.join(cachePath, "image.tar.gz")}`, false); - core.info(`${dockerBuildTags.join(", ")} successfully loaded from cache`) - return - } + exec(`docker load -i ${path.join(cachePath, "image.tar")}`, false); + core.info(`${dockerBuildTags.join(", ")} successfully loaded from cache`) + return } // docker build/save and store meta data in cache path exec(`docker build ${dockerBuildArgs}`, true); exec(`mkdir -p ${cachePath}`, false); exec(`docker save -o ${path.join(cachePath, "image.tar")} ${dockerBuildTags.join(" ")}`, false); - exec(`gzip ${path.join(cachePath, "image.tar")}`, false) - - // parse expiresStr into timestamp - let expires = 0 - if (expiresStr != "") { - expires = Date.now() + duration(expiresStr, "ms") - } - - if (expires > 0) { - fs.writeFileSync(path.join(cachePath, ".meta.expires"), expires) - } // save cache try { @@ -98,11 +74,6 @@ const duration = __webpack_require__(3805); core.error(error.message); } - if (expires > 0) { - let expiresDate = new Date(expires) - core.info(`Cache expires ${expiresDate.toUTCString()}`) - } - })(); diff --git a/index.js b/index.js index 86956a9..09bb014 100644 --- a/index.js +++ b/index.js @@ -15,27 +15,24 @@ const duration = require('parse-duration'); const runnerTemp = process.env['RUNNER_TEMP'] || "" if (runnerTemp == "") { - core.setFailed("RUNNER_TEMP env var missing") - return + abort("RUNNER_TEMP env var missing") } if (dockerBuildArgs == "") { - core.setFailed("docker build args missing") - return + abort("docker build args missing") } // parse docker build args const dockerBuildTags = getDockerBuildTags(dockerBuildArgs) if (dockerBuildTags.length == 0) { - core.setFailed("docker build args require at least one --tag") - return + abort("docker build args require at least one --tag") } const primaryKey = sha256(`${cacheKey} ${dockerBuildArgs}`) const cachePath = path.join(runnerTemp, "cached-docker-build", primaryKey) let cacheHit = false - core.info(`Cached key: ${primaryKey}`) + core.info(`Cache Key Hash: ${primaryKey}`) // try to restore cachePath from Github cache try {