Skip to content

Commit

Permalink
Add inputs and upd workflow (#4)
Browse files Browse the repository at this point in the history
* add by-time input

* upd workflow to run built action

* action: build the action

* commit and push only on push

* upd permissions

* use separate inputs

* action: build the action

* select date dynamically

* action: build the action

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
deemp and github-actions authored Jul 31, 2023
1 parent 6bd5afa commit 2651a14
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 36 deletions.
33 changes: 20 additions & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,49 @@ name: CI

jobs:
build:
name: Build app
name: Build and test the action
runs-on: ubuntu-latest
permissions:
contents: write
actions: write
steps:
- uses: actions/checkout@v3

# -------- THIS ACTION --------
- name: Purge caches
uses: ./.
with:
debug: true
# 3 days
max-age: 259200
# -------- THIS ACTION --------

# http://man7.org/linux/man-pages/man1/date.1.html
- name: Get Date
id: get-date
run: |
echo "date=$(/bin/date -u "+date-%Y-%m-%d-time-%H-%M-%S")" >> $GITHUB_OUTPUT
shell: bash

- uses: actions/cache@v3
with:
path: |
~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json', 'package.json') }}-${{ steps.get-date.outputs.date }}
restore-keys: |
${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json', 'package.json') }}-
- name: Install packages and build the app
- name: Install packages & Build the action
run: npm ci

# -------- THIS ACTION --------
- name: Purge caches
uses: ./.
with:
debug: true
created: true
accessed: true
# 3 days
max-age: 259200
# -------- THIS ACTION --------

- name: Commit & Push changes
if: github.event_name == 'push'
run: |
git config --global user.name github-actions
git config --global user.email github-actions@github.com
git pull --rebase --autostash
git add dist
git commit -m "action: build the app" || echo ""
git push
git commit -m "action: build the action" || echo ""
git push
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ steps:
# Do other steps like checkout, install, compile, etc.
- uses: MyAlbum/purge-cache@v2
with:
max-age: 604800 # Cache max 7 days since last use (this is the default)
accessed: true # Purge caches by their last accessed time (default)
created: false # Purge caches by their created time (default)
max-age: 604800 # Leave only caches accessed in the last 7 days (default)
```
## Example workflow
Expand Down
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ name: "Purge cache"
description: Purge GitHub Actions cache
inputs:
debug:
description: 'Out debug info'
description: 'Output debug info'
default: "false"
max-age:
description: 'Delete all caches older than this value in seconds'
required: true
default: "604800"
accessed:
description: 'When "true", delete caches by "last accessed" time'
required: false
default: "true"
created:
description: 'When "true", delete caches by "created" time'
required: false
default: "false"
token:
description: Used to communicate with GitHub API. Since there's a default, this is typically not supplied by the user.
default: ${{ github.token }}
Expand Down
49 changes: 39 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9547,13 +9547,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true }));
const github = __importStar(__nccwpck_require__(5438));
const core = __importStar(__nccwpck_require__(2186));
function setFailedWrongValue(input, value) {
core.setFailed(`Wrong value for the input '${input}': ${value}`);
}
var Inputs;
(function (Inputs) {
Inputs["Debug"] = "debug";
Inputs["MaxAge"] = "max-age";
Inputs["Accessed"] = "accessed";
Inputs["Created"] = "created";
Inputs["Token"] = "token";
})(Inputs || (Inputs = {}));
function run() {
return __awaiter(this, void 0, void 0, function* () {
const debug = core.getInput('debug', { required: false }) === 'true';
const maxAge = core.getInput('max-age', { required: true });
const token = core.getInput('token', { required: false });
const octokit = github.getOctokit(token);
const debug = core.getInput(Inputs.Debug, { required: false }) === 'true';
const maxAge = core.getInput(Inputs.MaxAge, { required: true });
const maxDate = new Date(Date.now() - Number.parseInt(maxAge) * 1000);
if (maxDate === null) {
setFailedWrongValue(Inputs.MaxAge, maxAge);
}
const accessed = core.getInput(Inputs.Accessed, { required: false }) === 'true';
const created = core.getInput(Inputs.Created, { required: false }) === 'true';
const token = core.getInput(Inputs.Token, { required: false });
const octokit = github.getOctokit(token);
const results = [];
for (let i = 1; i <= 100; i += 1) {
const { data: cachesRequest } = yield octokit.rest.actions.getActionsCacheList({
Expand All @@ -9571,11 +9587,19 @@ function run() {
console.log(`Found ${results.length} caches`);
}
results.forEach((cache) => __awaiter(this, void 0, void 0, function* () {
if (cache.last_accessed_at !== undefined && cache.id !== undefined) {
const cacheDate = new Date(cache.last_accessed_at);
if (cacheDate < maxDate) {
if (cache.last_accessed_at !== undefined && cache.created_at !== undefined && cache.id !== undefined) {
const accessedAt = new Date(cache.last_accessed_at);
const createdAt = new Date(cache.created_at);
const accessedCondition = accessed && accessedAt < maxDate;
const createdCondition = created && createdAt < maxDate;
if (accessedCondition || createdCondition) {
if (debug) {
console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`);
if (accessedCondition) {
console.log(`Deleting cache ${cache.key}, last accessed at ${accessedAt} before ${maxDate}`);
}
if (createdCondition) {
console.log(`Deleting cache ${cache.key}, created at ${createdAt} before ${maxDate}`);
}
}
try {
yield octokit.rest.actions.deleteActionsCacheById({
Expand All @@ -9586,11 +9610,16 @@ function run() {
});
}
catch (error) {
console.log(`Failed to delete cache ${cache.key}; ${error}`);
console.log(`Failed to delete cache ${cache.key};\n\n${error}`);
}
}
else if (debug) {
console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`);
if (accessed) {
console.log(`Skipping cache ${cache.key}, last accessed at ${accessedAt} after ${maxDate}`);
}
if (created) {
console.log(`Skipping cache ${cache.key}, created at ${createdAt} after ${maxDate}`);
}
}
}
}));
Expand Down
52 changes: 41 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import * as github from '@actions/github';
import * as core from '@actions/core';

function setFailedWrongValue(input: string, value: string) {
core.setFailed(`Wrong value for the input '${input}': ${value}`);
}

enum Inputs {
Debug = "debug",
MaxAge = "max-age",
Accessed = "accessed",
Created = "created",
Token = "token"
}

async function run() {
const debug = core.getInput('debug', { required: false }) === 'true';
const maxAge = core.getInput('max-age', { required: true });
const token = core.getInput('token', { required: false });
const octokit = github.getOctokit(token);
const debug = core.getInput(Inputs.Debug, { required: false }) === 'true';
const maxAge = core.getInput(Inputs.MaxAge, { required: true });
const maxDate = new Date(Date.now() - Number.parseInt(maxAge) * 1000)
if (maxDate === null) {
setFailedWrongValue(Inputs.MaxAge, maxAge)
}
const accessed = core.getInput(Inputs.Accessed, { required: false }) === 'true';
const created = core.getInput(Inputs.Created, { required: false }) === 'true';
const token = core.getInput(Inputs.Token, { required: false });
const octokit = github.getOctokit(token);

interface Cache {
id?: number | undefined;
Expand All @@ -31,7 +48,7 @@ async function run() {
if (cachesRequest.actions_caches.length == 0) {
break
}

results.push(...cachesRequest.actions_caches)
}

Expand All @@ -40,11 +57,19 @@ async function run() {
}

results.forEach(async (cache) => {
if (cache.last_accessed_at !== undefined && cache.id !== undefined) {
const cacheDate = new Date(cache.last_accessed_at);
if (cacheDate < maxDate) {
if (cache.last_accessed_at !== undefined && cache.created_at !== undefined && cache.id !== undefined) {
const accessedAt = new Date(cache.last_accessed_at);
const createdAt = new Date(cache.created_at);
const accessedCondition = accessed && accessedAt < maxDate;
const createdCondition = created && createdAt < maxDate;
if (accessedCondition || createdCondition) {
if (debug) {
console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`);
if (accessedCondition) {
console.log(`Deleting cache ${cache.key}, last accessed at ${accessedAt} before ${maxDate}`);
}
if (createdCondition) {
console.log(`Deleting cache ${cache.key}, created at ${createdAt} before ${maxDate}`);
}
}

try {
Expand All @@ -55,10 +80,15 @@ async function run() {
cache_id: cache.id,
});
} catch (error) {
console.log(`Failed to delete cache ${cache.key}; ${error}`);
console.log(`Failed to delete cache ${cache.key};\n\n${error}`);
}
} else if (debug) {
console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`);
if (accessed) {
console.log(`Skipping cache ${cache.key}, last accessed at ${accessedAt} after ${maxDate}`);
}
if (created) {
console.log(`Skipping cache ${cache.key}, created at ${createdAt} after ${maxDate}`);
}
}
}
});
Expand Down

0 comments on commit 2651a14

Please sign in to comment.