From 3282ad5c2bf8350ea8c4aa0632d03ef309c84b93 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 19 Jul 2023 12:38:00 +0300 Subject: [PATCH 1/9] add by-time input --- README.md | 16 +++++++++++++++- action.yml | 6 +++++- src/index.ts | 41 ++++++++++++++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b227f7d..ec5f97e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ 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) + by-time: accessed # Purge caches by their last accessed time (default) + max-age: 604800 # Leave caches accessed in the last 7 days (default) ``` ## Example workflow @@ -20,6 +21,19 @@ See [ci.yaml](.github/workflows/ci.yaml) ## Other options +### By + +Each cache was `created` and then `accessed` at certain time. +Select by what time to purge caches. + +```yaml +steps: +# Do other steps like checkout, install, compile, etc. +- uses: MyAlbum/purge-cache@v2 + with: + by: created # Purge caches by their created time +``` + ### Debug Output debug data (defaults to `false`) diff --git a/action.yml b/action.yml index f7e32bf..7ea810d 100644 --- a/action.yml +++ b/action.yml @@ -2,12 +2,16 @@ 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" + by-time: + description: 'Delete by `created` or by last `accessed` time of a cache' + required: false + default: accessed token: description: Used to communicate with GitHub API. Since there's a default, this is typically not supplied by the user. default: ${{ github.token }} diff --git a/src/index.ts b/src/index.ts index 26d801d..1f59823 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,38 @@ 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", + ByTime = "by-time" +} + +enum ByTimeValues { + Accessed = "accessed", + Created = "created" +} + async function run() { - const debug = core.getInput('debug', { required: false }) === 'true'; - const maxAge = core.getInput('max-age', { required: true }); + 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 byTime = core.getInput(Inputs.ByTime, { required: false }); + if (!(byTime === ByTimeValues.Accessed || byTime === ByTimeValues.Created)) { + setFailedWrongValue(Inputs.ByTime, byTime) + } + const doUseAccessedTime = byTime == ByTimeValues.Accessed; + 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; @@ -31,7 +57,7 @@ async function run() { if (cachesRequest.actions_caches.length == 0) { break } - + results.push(...cachesRequest.actions_caches) } @@ -42,9 +68,10 @@ async function run() { results.forEach(async (cache) => { if (cache.last_accessed_at !== undefined && cache.id !== undefined) { const cacheDate = new Date(cache.last_accessed_at); + const phrase = doUseAccessedTime ? "last accessed" : "created"; if (cacheDate < maxDate) { if (debug) { - console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`); + console.log(`Deleting cache ${cache.key}, ${phrase} at ${cacheDate} before ${maxDate}`); } try { @@ -55,10 +82,10 @@ 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}`); + console.log(`Skipping cache ${cache.key}, ${phrase} at ${cacheDate} after ${maxDate}`); } } }); From 645dcb4be8037737bc13637f17790832d859f594 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 19 Jul 2023 12:38:55 +0300 Subject: [PATCH 2/9] upd workflow to run built action --- .github/workflows/ci.yaml | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ba3f9df..0313865 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,28 +8,20 @@ name: CI jobs: build: - name: Build app + name: Build and test the action 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: | @@ -37,13 +29,25 @@ jobs: 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 + by-time: created + # 3 days + max-age: 259200 + # -------- THIS ACTION -------- + - 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 \ No newline at end of file + git commit -m "action: build the action" || echo "" + git push From 2048c7c81efeeb1770d8765d5a123f39e6497a87 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 19 Jul 2023 09:39:25 +0000 Subject: [PATCH 3/9] action: build the action --- dist/index.js | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/dist/index.js b/dist/index.js index c376fac..d706b21 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9547,13 +9547,35 @@ 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["ByTime"] = "by-time"; +})(Inputs || (Inputs = {})); +var ByTimeValues; +(function (ByTimeValues) { + ByTimeValues["Accessed"] = "accessed"; + ByTimeValues["Created"] = "created"; +})(ByTimeValues || (ByTimeValues = {})); 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 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 byTime = core.getInput(Inputs.ByTime, { required: false }); + if (!(byTime === ByTimeValues.Accessed || byTime === ByTimeValues.Created)) { + setFailedWrongValue(Inputs.ByTime, byTime); + } + const doUseAccessedTime = byTime == ByTimeValues.Accessed; const token = core.getInput('token', { required: false }); const octokit = github.getOctokit(token); - const maxDate = new Date(Date.now() - Number.parseInt(maxAge) * 1000); const results = []; for (let i = 1; i <= 100; i += 1) { const { data: cachesRequest } = yield octokit.rest.actions.getActionsCacheList({ @@ -9573,9 +9595,10 @@ function run() { 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); + const phrase = doUseAccessedTime ? "last accessed" : "created"; if (cacheDate < maxDate) { if (debug) { - console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`); + console.log(`Deleting cache ${cache.key}, ${phrase} at ${cacheDate} before ${maxDate}`); } try { yield octokit.rest.actions.deleteActionsCacheById({ @@ -9586,11 +9609,11 @@ 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}`); + console.log(`Skipping cache ${cache.key}, ${phrase} at ${cacheDate} after ${maxDate}`); } } })); From a7ea0e6171b380043279e12264413bf0cb69d9eb Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 19 Jul 2023 12:49:53 +0300 Subject: [PATCH 4/9] commit and push only on push --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0313865..6aaedd3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,6 +44,7 @@ jobs: # -------- 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 From 32ba3d11273403512ade65f2102bfe03f9f616ce Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 19 Jul 2023 13:13:53 +0300 Subject: [PATCH 5/9] upd permissions --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6aaedd3..534ea40 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,6 +12,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + actions: write steps: - uses: actions/checkout@v3 From a890609e605ff7e8703f0be37004622de3854bed Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 19 Jul 2023 14:23:23 +0300 Subject: [PATCH 6/9] use separate inputs --- .github/workflows/ci.yaml | 3 ++- README.md | 18 +++--------------- action.yml | 10 +++++++--- src/index.ts | 34 +++++++++++++++++----------------- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 534ea40..d1156b9 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -39,7 +39,8 @@ jobs: uses: ./. with: debug: true - by-time: created + created: true + accessed: true # 3 days max-age: 259200 # -------- THIS ACTION -------- diff --git a/README.md b/README.md index ec5f97e..80cffbb 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,9 @@ steps: # Do other steps like checkout, install, compile, etc. - uses: MyAlbum/purge-cache@v2 with: - by-time: accessed # Purge caches by their last accessed time (default) - max-age: 604800 # Leave caches accessed in the last 7 days (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 @@ -21,19 +22,6 @@ See [ci.yaml](.github/workflows/ci.yaml) ## Other options -### By - -Each cache was `created` and then `accessed` at certain time. -Select by what time to purge caches. - -```yaml -steps: -# Do other steps like checkout, install, compile, etc. -- uses: MyAlbum/purge-cache@v2 - with: - by: created # Purge caches by their created time -``` - ### Debug Output debug data (defaults to `false`) diff --git a/action.yml b/action.yml index 7ea810d..f5dde91 100644 --- a/action.yml +++ b/action.yml @@ -8,10 +8,14 @@ inputs: description: 'Delete all caches older than this value in seconds' required: true default: "604800" - by-time: - description: 'Delete by `created` or by last `accessed` time of a cache' + accessed: + description: 'When "true", delete caches by "last accessed" time' required: false - default: accessed + 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 }} diff --git a/src/index.ts b/src/index.ts index 1f59823..f51cea1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,30 +8,21 @@ function setFailedWrongValue(input: string, value: string) { enum Inputs { Debug = "debug", MaxAge = "max-age", - ByTime = "by-time" -} - -enum ByTimeValues { Accessed = "accessed", - Created = "created" + Created = "created", + Token = "token" } async function run() { 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 byTime = core.getInput(Inputs.ByTime, { required: false }); - if (!(byTime === ByTimeValues.Accessed || byTime === ByTimeValues.Created)) { - setFailedWrongValue(Inputs.ByTime, byTime) - } - const doUseAccessedTime = byTime == ByTimeValues.Accessed; - - const token = core.getInput('token', { required: false }); + 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 { @@ -68,10 +59,14 @@ async function run() { results.forEach(async (cache) => { if (cache.last_accessed_at !== undefined && cache.id !== undefined) { const cacheDate = new Date(cache.last_accessed_at); - const phrase = doUseAccessedTime ? "last accessed" : "created"; if (cacheDate < maxDate) { if (debug) { - console.log(`Deleting cache ${cache.key}, ${phrase} at ${cacheDate} before ${maxDate}`); + if (accessed) { + console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`); + } + if (created) { + console.log(`Deleting cache ${cache.key}, created at ${cacheDate} before ${maxDate}`); + } } try { @@ -85,7 +80,12 @@ async function run() { console.log(`Failed to delete cache ${cache.key};\n\n${error}`); } } else if (debug) { - console.log(`Skipping cache ${cache.key}, ${phrase} at ${cacheDate} after ${maxDate}`); + if (accessed) { + console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`); + } + if (created) { + console.log(`Skipping cache ${cache.key}, created at ${cacheDate} after ${maxDate}`); + } } } }); From eb28fc9668b0417d1e6b5bf1355362509d451e0a Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 19 Jul 2023 11:23:56 +0000 Subject: [PATCH 7/9] action: build the action --- dist/index.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/dist/index.js b/dist/index.js index d706b21..27ee07d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9554,13 +9554,10 @@ var Inputs; (function (Inputs) { Inputs["Debug"] = "debug"; Inputs["MaxAge"] = "max-age"; - Inputs["ByTime"] = "by-time"; + Inputs["Accessed"] = "accessed"; + Inputs["Created"] = "created"; + Inputs["Token"] = "token"; })(Inputs || (Inputs = {})); -var ByTimeValues; -(function (ByTimeValues) { - ByTimeValues["Accessed"] = "accessed"; - ByTimeValues["Created"] = "created"; -})(ByTimeValues || (ByTimeValues = {})); function run() { return __awaiter(this, void 0, void 0, function* () { const debug = core.getInput(Inputs.Debug, { required: false }) === 'true'; @@ -9569,12 +9566,9 @@ function run() { if (maxDate === null) { setFailedWrongValue(Inputs.MaxAge, maxAge); } - const byTime = core.getInput(Inputs.ByTime, { required: false }); - if (!(byTime === ByTimeValues.Accessed || byTime === ByTimeValues.Created)) { - setFailedWrongValue(Inputs.ByTime, byTime); - } - const doUseAccessedTime = byTime == ByTimeValues.Accessed; - const token = core.getInput('token', { required: false }); + 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) { @@ -9595,10 +9589,14 @@ function run() { 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); - const phrase = doUseAccessedTime ? "last accessed" : "created"; if (cacheDate < maxDate) { if (debug) { - console.log(`Deleting cache ${cache.key}, ${phrase} at ${cacheDate} before ${maxDate}`); + if (accessed) { + console.log(`Deleting cache ${cache.key}, last accessed at ${cacheDate} before ${maxDate}`); + } + if (created) { + console.log(`Deleting cache ${cache.key}, created at ${cacheDate} before ${maxDate}`); + } } try { yield octokit.rest.actions.deleteActionsCacheById({ @@ -9613,7 +9611,12 @@ function run() { } } else if (debug) { - console.log(`Skipping cache ${cache.key}, ${phrase} at ${cacheDate} after ${maxDate}`); + if (accessed) { + console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`); + } + if (created) { + console.log(`Skipping cache ${cache.key}, created at ${cacheDate} after ${maxDate}`); + } } } })); From e1871149745946c0b8400a6cdb90231994477512 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Wed, 19 Jul 2023 17:53:53 +0300 Subject: [PATCH 8/9] select date dynamically --- src/index.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index f51cea1..9b07020 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,15 +57,18 @@ 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) { - if (accessed) { - 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 (created) { - console.log(`Deleting cache ${cache.key}, created at ${cacheDate} before ${maxDate}`); + if (createdCondition) { + console.log(`Deleting cache ${cache.key}, created at ${createdAt} before ${maxDate}`); } } @@ -81,10 +84,10 @@ async function run() { } } else if (debug) { if (accessed) { - console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`); + console.log(`Skipping cache ${cache.key}, last accessed at ${accessedAt} after ${maxDate}`); } if (created) { - console.log(`Skipping cache ${cache.key}, created at ${cacheDate} after ${maxDate}`); + console.log(`Skipping cache ${cache.key}, created at ${createdAt} after ${maxDate}`); } } } From cbc1438a38574a0d691e41a259f06304f7b7c845 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 19 Jul 2023 14:54:25 +0000 Subject: [PATCH 9/9] action: build the action --- dist/index.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/dist/index.js b/dist/index.js index 27ee07d..01ad5bc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9587,15 +9587,18 @@ 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) { - if (accessed) { - 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 (created) { - console.log(`Deleting cache ${cache.key}, created at ${cacheDate} before ${maxDate}`); + if (createdCondition) { + console.log(`Deleting cache ${cache.key}, created at ${createdAt} before ${maxDate}`); } } try { @@ -9612,10 +9615,10 @@ function run() { } else if (debug) { if (accessed) { - console.log(`Skipping cache ${cache.key}, last accessed at ${cacheDate} after ${maxDate}`); + console.log(`Skipping cache ${cache.key}, last accessed at ${accessedAt} after ${maxDate}`); } if (created) { - console.log(`Skipping cache ${cache.key}, created at ${cacheDate} after ${maxDate}`); + console.log(`Skipping cache ${cache.key}, created at ${createdAt} after ${maxDate}`); } } }