Skip to content

Commit

Permalink
prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
mattes committed Sep 28, 2020
1 parent 319d0ca commit b542b6a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 49 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
47 changes: 9 additions & 38 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()}`)
}

})();


Expand Down
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b542b6a

Please sign in to comment.