diff --git a/README.md b/README.md index fd94173..e7a19d0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,27 @@ -# cached-docker-build-action +# 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. + + +## Github Action Inputs + +| Variable | Description | +|----------------------------------|-----------------------------------------------------------------------------| +| `args` | ***Required*** Arguments passed to `docker build` command | +| `cache_key` | ***Required*** Key used for caching | + + +## Example Usage + +``` +uses: mattes/cached-docker-build-action@v1 +with: + args: --pull --file Dockerfile --tag my-image:tag . + cache_key: ${{ hashFiles('**/lockfiles') }} +``` + +## Future work + + * Implement `expires` flag, blocked by [Clear cache #2](https://github.com/actions/cache/issues/2). ---pull Always attempt to pull a newer version of the image diff --git a/action.yml b/action.yml index 03a9352..c48f003 100644 --- a/action.yml +++ b/action.yml @@ -12,10 +12,6 @@ inputs: cache_key: description: "Cache key" required: true - expires: - description: "Set expiration for cache (example '12 hours' or '3 days', ...)" - -# ${{runner.temp}} directory is guaranteed to be empty at the start of each job, even on self-hosted runners. runs: using: "node12" diff --git a/index.js b/index.js index beac2af..86956a9 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,6 @@ const duration = require('parse-duration'); // 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 == "") { @@ -32,7 +31,7 @@ const duration = require('parse-duration'); return } - const primaryKey = sha256(`${cacheKey} ${dockerBuildArgs} ${expiresStr}`) + const primaryKey = sha256(`${cacheKey} ${dockerBuildArgs}`) const cachePath = path.join(runnerTemp, "cached-docker-build", primaryKey) let cacheHit = false @@ -52,20 +51,11 @@ const duration = require('parse-duration'); } } - // 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")}`, 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 @@ -73,16 +63,6 @@ const duration = require('parse-duration'); exec(`mkdir -p ${cachePath}`, false); exec(`docker save -o ${path.join(cachePath, "image.tar")} ${dockerBuildTags.join(" ")}`, 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 { await cache.saveCache([cachePath], primaryKey); @@ -90,11 +70,6 @@ const duration = require('parse-duration'); core.error(error.message); } - if (expires > 0) { - let expiresDate = new Date(expires) - core.info(`Cache expires ${expiresDate.toUTCString()}`) - } - })();