Skip to content

Commit

Permalink
implement ccache as github action
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikmuhs committed Dec 6, 2020
1 parent abf06fd commit a83c88f
Show file tree
Hide file tree
Showing 9 changed files with 111,410 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ typings/

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
Expand Down
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "C-Cache for gh actions"
description: "A GitHub Action that speeds up compiling for C/C++ projects."
author: "Hendrik Muhs<[email protected]>"
inputs:
key:
description: "An additional key for the cache"
required: false
runs:
using: "node12"
main: "dist/restore/index.js"
post: "dist/save/index.js"
post-if: "success()"
branding:
icon: "archive"
color: "gray-dark"
55,397 changes: 55,397 additions & 0 deletions dist/restore/index.js

Large diffs are not rendered by default.

55,352 changes: 55,352 additions & 0 deletions dist/save/index.js

Large diffs are not rendered by default.

496 changes: 496 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "ccache-action",
"version": "1.0.0",
"description": "github action to speedup building using ccache",
"main": "dist/restore/index.js",
"scripts": {
"prepare": "ncc build --target es2020 -o dist/restore src/restore.ts && ncc build --target es2020 -o dist/save src/save.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hendrikmuhs/ccache-action.git"
},
"keywords": [
"actions",
"ccache",
"cache",
"c++",
"cpp",
"c"
],
"author": "Hendrik Muhs",
"license": "MIT",
"dependencies": {
"@actions/cache": "^1.0.4",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/io": "^1.0.2"
},
"devDependencies": {
"@vercel/ncc": "^0.25.1",
"typescript": "^4.1.2"
},
"bugs": {
"url": "https://github.com/hendrikmuhs/ccache-action/issues"
},
"homepage": "https://github.com/hendrikmuhs/ccache-action#readme"
}
68 changes: 68 additions & 0 deletions src/restore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as core from "@actions/core";
import * as io from "@actions/io";
import * as exec from "@actions/exec";
import * as process from "process";
import * as cache from "@actions/cache";

// based on https://cristianadam.eu/20200113/speeding-up-c-plus-plus-github-actions-using-ccache/

async function install() {
if (process.platform === "darwin") {
await exec.exec("brew install ccache");
} else {
await exec.exec("apt-get update");
await exec.exec("apt-get install -y ccache");
}
}

async function restore() {
let restoreKey = `ccache-`;

let inputKey = core.getInput("key");
if (inputKey) {
restoreKey += `${inputKey}-`;
}

const restoreKeys = [
restoreKey
]

const key = restoreKey + "-" + new Date().toUTCString();
const paths = [
'.ccache'
]

await cache.restoreCache(paths, key, restoreKeys)
}

async function configure() {
const ghWorkSpace = process.env.GITHUB_WORKSPACE;

core.info("Configure ccache");
await exec.exec("ccache --set-config=cache_dir=" + ghWorkSpace + "/.ccache");
await exec.exec("ccache --set-config=max_size=500M");
await exec.exec("ccache --set-config=compression=true");

core.info("Ccache config:")
await exec.exec("ccache -p");
}

async function run() {
let ccachePath = await io.which("ccache");
if (!ccachePath) {
core.info(`Install ccache`);
await install();
ccachePath = await io.which("ccache", true);
}

await restore();
await configure();

await exec.exec("ccache -z");
}

try {
run();
} catch (err) {
core.setFailed(`Action failed with error ${err}`);
}
24 changes: 24 additions & 0 deletions src/save.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as core from "@actions/core";
import * as cache from "@actions/cache";

async function run() {
let restoreKey = `ccache-`;
let inputKey = core.getInput("key");

if (inputKey) {
restoreKey += `${inputKey}-`;
}

const key = restoreKey + "-" + new Date().toUTCString();
const paths = [
'.ccache'
]

await cache.saveCache(paths, key)
}

try {
run();
} catch (err) {
core.setFailed(`Action failed with error ${err}`);
}
21 changes: 21 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"noEmitOnError": false,
"diagnostics": true,
"lib": ["esnext"],

"target": "es2020",

"resolveJsonModule": true,
"moduleResolution": "node",
"module": "esnext",
"esModuleInterop": true,

"strict": true,
"skipLibCheck": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true
},
"exclude": ["dist"]
}

0 comments on commit a83c88f

Please sign in to comment.