Skip to content

Commit

Permalink
fix action cache clean (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
guguducken authored Dec 17, 2024
1 parent 4f08d5d commit 83596d3
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 82 deletions.
7 changes: 2 additions & 5 deletions actions/action-cache-clean/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 8,8 @@ inputs:
description: "The maximum time the cache is allowed to exist, in seconds"
required: false
default: "86400"
owner:
description: "Account where cache exists"
required: true
repository:
description: "The specific repository where the cache exists"
org:
description: "Organization account where cache exists"
required: true

runs:
Expand Down
109 changes: 71 additions & 38 deletions actions/action-cache-clean/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29950,58 29950,91 @@ const github = __importStar(__nccwpck_require__(5241));
const core = __importStar(__nccwpck_require__(6117));
const token = core.getInput("github_token", { required: true });
let maxLiveDuration = parseInt(core.getInput("max_live_duration", { required: false }), 10);
let owner = core.getInput("owner", { required: false });
let repository = core.getInput("repository", { required: false });
let org = core.getInput("org", { required: false });
const octo = github.getOctokit(token);
function setDefaultValues() {
core.info(`maxLiveDuration = ${maxLiveDuration}`);
maxLiveDuration = !isNaN(maxLiveDuration) ? maxLiveDuration * 1000 : 24 * 60 * 60 * 1000;
owner = owner != "" ? owner : github.context.repo.owner;
repository = repository != "" ? repository : github.context.repo.repo;
org = org != "" ? org : github.context.repo.owner;
}
async function run() {
const { data: cacheList } = await octo.rest.actions.getActionsCacheList({
owner: owner,
repo: repository,
per_page: 100,
});
if (cacheList.total_count === 0) {
core.info(`There is not action cache in repo ${owner}/${repository}, so skip it`);
}
const now = new Date();
core.info(`Now is ${now.toISOString()}`);
for (const cache of cacheList.actions_caches) {
if (cache.key === undefined) {
core.warning("The Key of cache is undefined, so skip it");
continue;
}
core.info(`Start process cache ${cache.key} for repository ${owner}/${repository}`);
if (cache.last_accessed_at === undefined) {
core.info(`The last access time of cache ${cache.key} is undefined, so delete it`);
deleteCache(cache.key);
continue;
async function deleteFroRepo(owner, repo) {
core.info(`Start process repository ${org}/${repo}`);
let page = 1;
let per_page = 100;
let needDeleteCaches = new Array();
while (true) {
const { data: cacheList } = await octo.rest.actions.getActionsCacheList({
owner: owner,
repo: repo,
page: page ,
per_page: per_page,
});
if (cacheList.total_count === 0) {
core.info(`There is not action cache in repo ${owner}/${repo}, so skip it`);
break;
}
const lastAccessTime = Date.parse(cache.last_accessed_at);
const result = now.getTime() - lastAccessTime > maxLiveDuration;
core.debug(`now.getTime() - lastAccessTime = ${now.getTime() - lastAccessTime}`);
core.debug(`maxLiveDuration = ${maxLiveDuration}`);
core.info(`The result of 'now.getTime() - lastAccessTime > maxLiveDuration' is ${result}`);
if (result) {
deleteCache(cache.key);
const now = new Date();
core.info(`Now is ${now.toISOString()}`);
cacheList.actions_caches.forEach(cache => {
if (cache.key === undefined) {
core.warning("The Key of cache is undefined, so skip it");
return;
}
if (cache.id === undefined) {
core.warning("The ID of cache is undefined, so skip it");
return;
}
core.info(`Start process cache ${cache.key} for repository ${owner}/${repo}`);
if (cache.last_accessed_at === undefined) {
core.info(`The last access time of cache ${cache.key} is undefined, so delete it`);
needDeleteCaches.push(cache.id);
return;
}
const lastAccessTime = Date.parse(cache.last_accessed_at);
const result = now.getTime() - lastAccessTime > maxLiveDuration;
core.debug(`now.getTime() - lastAccessTime = ${now.getTime() - lastAccessTime}`);
core.debug(`maxLiveDuration = ${maxLiveDuration}`);
core.info(`The result of 'now.getTime() - lastAccessTime > maxLiveDuration' is ${result}`);
if (result) {
needDeleteCaches.push(cache.id);
}
});
if (cacheList.total_count < per_page) {
break;
}
}
needDeleteCaches.forEach(id => {
deleteCache(owner, repo, id);
});
return null;
}
function deleteCache(key) {
octo.rest.actions.deleteActionsCacheByKey({
function deleteCache(owner, repo, id) {
octo.rest.actions.deleteActionsCacheById({
owner: owner,
repo: repository,
key: key,
repo: repo,
cache_id: id,
}).then((data) => {
core.info(`Delete cache in ${owner}/${repository} with key ${key} finished,
core.info(`Delete cache in ${owner}/${repo} with id ${id} finished,
response status: ${data.status}`);
});
}
async function run() {
let page = 1;
let per_page = 100;
while (true) {
const { data: repos } = await octo.rest.repos.listForOrg({
org: org,
page: page ,
per_page: per_page,
});
repos.forEach((repo) => {
deleteFroRepo(org, repo.name);
});
if (repos.length < per_page) {
break;
}
}
return null;
}
async function main() {
setDefaultValues();
try {
Expand Down
116 changes: 77 additions & 39 deletions actions/action-cache-clean/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 2,111 @@ import * as github from "@actions/github";
import * as core from "@actions/core";



const token = core.getInput("github_token",{required: true});
let maxLiveDuration = parseInt(core.getInput("max_live_duration",{required: false}), 10);
let owner = core.getInput("owner",{required: false});
let repository = core.getInput("repository",{required: false});
let org = core.getInput("org",{required: false});

const octo = github.getOctokit(token);

function setDefaultValues() {
maxLiveDuration = !isNaN(maxLiveDuration) ? maxLiveDuration * 1000 : 24* 60 * 60 * 1000;
owner = owner != "" ? owner : github.context.repo.owner;
repository = repository != "" ? repository : github.context.repo.repo;
org = org != "" ? org : github.context.repo.owner;
}

async function run(){
const {data: cacheList} = await octo.rest.actions.getActionsCacheList(
{
owner: owner,
repo: repository,
per_page: 100,
},
);
if (cacheList.total_count === 0) {
core.info(`There is not action cache in repo ${owner}/${repository}, so skip it`);
}
const now = new Date();
core.info(`Now is ${now.toISOString()}`);
for (const cache of cacheList.actions_caches) {
if (cache.key === undefined) {
core.warning("The Key of cache is undefined, so skip it");
continue;
async function deleteFroRepo(owner: string, repo: string) {
core.info(`Start process repository ${org}/${repo}`)
let page = 1;
let per_page = 100;
let needDeleteCaches = new Array<number>();
while (true) {
const {data: cacheList} = await octo.rest.actions.getActionsCacheList(
{
owner: owner,
repo: repo,
page: page ,
per_page: per_page,
},
);
if (cacheList.total_count === 0) {
core.info(`There is not action cache in repo ${owner}/${repo}, so skip it`);
break;
}
core.info(`Start process cache ${cache.key} for repository ${owner}/${repository}`);
const now = new Date();
core.info(`Now is ${now.toISOString()}`);
cacheList.actions_caches.forEach(cache => {
if (cache.key === undefined) {
core.warning("The Key of cache is undefined, so skip it");
return;
}
if (cache.id === undefined) {
core.warning("The ID of cache is undefined, so skip it");
return;
}
core.info(`Start process cache ${cache.key} for repository ${owner}/${repo}`);

if (cache.last_accessed_at === undefined) {
core.info(`The last access time of cache ${cache.key} is undefined, so delete it`);
deleteCache(cache.key);
continue;
}
if (cache.last_accessed_at === undefined) {
core.info(`The last access time of cache ${cache.key} is undefined, so delete it`);
needDeleteCaches.push(cache.id)
return;
}

const lastAccessTime = Date.parse(cache.last_accessed_at);
const result = now.getTime() - lastAccessTime > maxLiveDuration;
const lastAccessTime = Date.parse(cache.last_accessed_at);
const result = now.getTime() - lastAccessTime > maxLiveDuration;

core.debug(`now.getTime() - lastAccessTime = ${now.getTime() - lastAccessTime}`)
core.debug(`maxLiveDuration = ${maxLiveDuration}`)
core.debug(`now.getTime() - lastAccessTime = ${now.getTime() - lastAccessTime}`)
core.debug(`maxLiveDuration = ${maxLiveDuration}`)

core.info(`The result of 'now.getTime() - lastAccessTime > maxLiveDuration' is ${result}`);
if (result) {
deleteCache(cache.key);
core.info(`The result of 'now.getTime() - lastAccessTime > maxLiveDuration' is ${result}`);
if (result) {
needDeleteCaches.push(cache.id)
}
})
if (cacheList.total_count < per_page) {
break;
}
}

needDeleteCaches.forEach(id => {
deleteCache(owner, repo, id);
})
return null;
}

function deleteCache(key: string) {
octo.rest.actions.deleteActionsCacheByKey(
function deleteCache(owner: string, repo: string,id: number) {
octo.rest.actions.deleteActionsCacheById(
{
owner: owner,
repo: repository,
key: key,
repo: repo,
cache_id:id,
},
).then(
(data) => {
core.info(`Delete cache in ${owner}/${repository} with key ${key} finished,
core.info(`Delete cache in ${owner}/${repo} with id ${id} finished,
response status: ${data.status}`);
}
)
}

async function run() {
let page = 1;
let per_page = 100;
while (true) {
const {data: repos} = await octo.rest.repos.listForOrg(
{
org: org,
page: page ,
per_page: per_page,
},
)
repos.forEach((repo) => {
deleteFroRepo(org,repo.name)
})
if (repos.length < per_page) {
break
}
}
return null
}

async function main() {
Expand Down

0 comments on commit 83596d3

Please sign in to comment.