Skip to content

Commit

Permalink
Merge pull request #3 from MyAlbum/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
Arjan-Zuidema authored Jul 19, 2023
2 parents b25e4f0 + 60af826 commit 6bd5afa
Show file tree
Hide file tree
Showing 10 changed files with 10,034 additions and 53 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

"on":
push:
workflow_dispatch:
schedule:
- cron: "0 0,12 * * *"

jobs:
build:
name: Build app
runs-on: ubuntu-latest
permissions:
contents: 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
run: npm ci
- name: Commit & Push changes
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
lib
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
This action allows the cache of GitHub Actions to be automatically purged

## Basic usage

See [action.yml](action.yml)

```yaml
steps:
# Do other steps like checkout, install, compile, etc.
- uses: MyAlbum/purge-cache
- uses: MyAlbum/purge-cache@v2
with:
max-age: 604800 # Cache max 7 days since last use (this is the default)
```
## Example workflow
See [ci.yaml](.github/workflows/ci.yaml)
## Other options
### Debug
Expand All @@ -26,7 +31,7 @@ Output debug data (defaults to `false`)
```yaml
steps:
# Do other steps like checkout, install, compile, etc.
- uses: MyAlbum/purge-cache
- uses: MyAlbum/purge-cache@v2
with:
debug: true # Set to true to output debug info
```
Expand All @@ -38,7 +43,7 @@ Set a GitHub token, will default to `${github.token}`. This will probably not be
```yaml
steps:
# Do other steps like checkout, install, compile, etc.
- uses: MyAlbum/purge-cache
- uses: MyAlbum/purge-cache@v2
with:
token: $GITHUBTOKEN # Set a GitHub token
```
```
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ description: Purge GitHub Actions cache
inputs:
debug:
description: 'Out debug info'
default: false
default: "false"
max-age:
description: 'Delete all caches older than this value in seconds'
required: true
default: 604800
default: "604800"
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
9,783 changes: 9,781 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

55 changes: 53 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "purge-cache",
"version": "1.0.0",
"version": "2.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "npx @vercel/ncc build src/index.js -m",
"build": "export NODE_OPTIONS=--openssl-legacy-provider && tsc && ncc build -o dist/ src/index.ts",
"postinstall": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -22,5 +22,10 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1"
},
"devDependencies": {
"@vercel/ncc": "^0.36.1",
"typescript": "^5.1.6",
"@types/node": "^20.4.1"
}
}
40 changes: 0 additions & 40 deletions src/index.js

This file was deleted.

67 changes: 67 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as github from '@actions/github';
import * as core from '@actions/core';

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 maxDate = new Date(Date.now() - Number.parseInt(maxAge) * 1000)

interface Cache {
id?: number | undefined;
ref?: string | undefined;
key?: string | undefined;
version?: string | undefined;
last_accessed_at?: string | undefined;
created_at?: string | undefined;
size_in_bytes?: number | undefined;
}

const results: Cache[] = []

for (let i = 1; i <= 100; i += 1) {
const { data: cachesRequest } = await octokit.rest.actions.getActionsCacheList({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
per_page: 100,
page: i
});

if (cachesRequest.actions_caches.length == 0) {
break
}

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

if (debug) {
console.log(`Found ${results.length} caches`);
}

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 (debug) {
console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`);
}

try {
await octokit.rest.actions.deleteActionsCacheById({
per_page: 100,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
cache_id: cache.id,
});
} catch (error) {
console.log(`Failed to delete cache ${cache.key}; ${error}`);
}
} else if (debug) {
console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`);
}
}
});
}

run();
Loading

0 comments on commit 6bd5afa

Please sign in to comment.