Skip to content

Commit

Permalink
remove expires arg and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mattes committed Sep 27, 2020
1 parent ab5ab7b commit 319d0ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 5 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "") {
Expand All @@ -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

Expand All @@ -52,49 +51,25 @@ 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
exec(`docker build ${dockerBuildArgs}`, true);
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);
} catch (error) {
core.error(error.message);
}

if (expires > 0) {
let expiresDate = new Date(expires)
core.info(`Cache expires ${expiresDate.toUTCString()}`)
}

})();


Expand Down

0 comments on commit 319d0ca

Please sign in to comment.