Skip to content

Commit

Permalink
feat: cache-update: [true|false] option (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 authored Oct 21, 2022
1 parent 8438a24 commit 7a8851a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
- uses: ./
with:
release-name: v0.3.0
cache-key: ${{ runner.os }}-sccache
cache-suffix: ${{ github.event_name != 'pull_request' }}
cache-key: ${{ runner.os }}-sccache-v1.2
cache-update: ${{ github.event_name != 'pull_request' }}
- run: sccache -s
- run: cargo build -v
env:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ This repository provides GitHub Actions which enables projects to cache the comp
with:
# Optional
cache-key: sccache-ubuntu-latest
# Optional
# Optional, whether or not saving the cache
cache-save: true
# Optional
cache-suffix: true
# Optional whether or not updating cache when hit
cache-update: true
# Optional, e.g. v0.3.0
release-name: latest
# Optional
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ inputs:
cache-save:
description: "A Flag that indicates whether or not saving the cache"
default: true
cache-suffix:
description: "A Flag that indicates whether or not appending timestamp suffix to the cache key when saving"
cache-update:
description: "A Flag that indicates whether or not updating cache when hit"
default: true
release-name:
description: "The sccache version. e.g. v0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum State {
RestoredCacheKey = "RESTORED_CACHE_KEY",
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Octokit } from "@octokit/rest";
import * as core from '@actions/core';
import * as cache from '@actions/cache';
import { exec } from '@actions/exec';
import { State } from './constants';

function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
Expand Down Expand Up @@ -77,6 +78,7 @@ export const restoreCache = async (restoreKey: string) => {
[`${process.env.HOME}/.cache/sccache`, `${process.env.HOME}/Library/Caches/Mozilla.sccache`], restoreKey, [`${restoreKey}-`]);
if (restoredCacheKey) {
core.info(`Cache restored from ${restoredCacheKey}.`);
core.saveState(State.RestoredCacheKey, restoredCacheKey);
} else {
core.info("Cache not found.");
}
Expand Down
19 changes: 15 additions & 4 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@
import * as core from '@actions/core';
import * as cache from '@actions/cache';
import { exec } from '@actions/exec';
import { State } from './constants';

export const showStat = async () => {
await exec("sccache -s");
};

export const saveCache = async () => {
try {
const shouldSaveCache = core.getBooleanInput('cache-save');
let shouldSaveCache = core.getBooleanInput('cache-save');
if (!shouldSaveCache) {
console.log(`Aborting, shouldSaveCache: ${shouldSaveCache}`);
return;
}
const shouldAppendSuffix = core.getBooleanInput('cache-suffix');
console.log(`shouldAppendSuffix: ${shouldAppendSuffix}`);
const cacheKey = shouldAppendSuffix ? `${core.getInput('cache-key')}-${new Date().toISOString()}` : core.getInput('cache-key');
let shouldUpdateCache = core.getBooleanInput('cache-update');
if (!shouldUpdateCache) {
const cacheKey = core.getState(State.RestoredCacheKey);
console.log(`cacheKey: ${cacheKey}`);
if (!cacheKey) {
shouldUpdateCache = true;
}
}
if (!shouldUpdateCache) {
console.log(`Aborting, shouldUpdateCache: ${shouldUpdateCache}`);
return;
}
const cacheKey = `${core.getInput('cache-key')}-${new Date().toISOString()}`;
console.log(`Using cacheKey: ${cacheKey}`);
await cache.saveCache([`${process.env.HOME}/.cache/sccache`, `${process.env.HOME}/Library/Caches/Mozilla.sccache`], cacheKey);
} catch (err: any) {
Expand Down

0 comments on commit 7a8851a

Please sign in to comment.