Skip to content

[workflow] Remove artifacts after successful runs - #1740

Draft
Steven Ramirez Rosa (Steven6798) wants to merge 1 commit into
qualcomm:mainfrom
Steven6798:issue-1739
Draft

[workflow] Remove artifacts after successful runs#1740
Steven Ramirez Rosa (Steven6798) wants to merge 1 commit into
qualcomm:mainfrom
Steven6798:issue-1739

Conversation

@Steven6798

Copy link
Copy Markdown
Contributor

Artifacts are now removed after a succesful run. The retention policy has been changed to 7 days in case there are repeated failures.

Fix: #1739

Artifacts are now removed after a succesful run. The retention
policy has been changed to 7 days in case there are repeated
failures.

Fix: qualcomm#1739

Signed-off-by: Steven Ramirez Rosa <ramirezr@qti.qualcomm.com>
Comment on lines +24 to +112
script: |
const names = `${{ inputs.artifact-names }}`
.split(/\r?\n/)
.map(name => name.trim())
.filter(Boolean);
const pullRequestNumber = `${{ inputs.pull-request-number }}`;
const workflowId = `${{ inputs.workflow-id }}`;
const currentRunId = context.runId;
const workflowRunCache = new Map();
let workflowFileId = null;

if (workflowId) {
const { data } = await github.rest.actions.getWorkflow({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowId
});
workflowFileId = String(data.id);
}

async function workflowRun(runId) {
if (!runId) {
return null;
}
if (!workflowRunCache.has(runId)) {
const { data } = await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});
workflowRunCache.set(runId, data);
}
return workflowRunCache.get(runId);
}

async function belongsToScope(runId) {
const run = await workflowRun(runId);
if (!run) {
return false;
}
if (workflowFileId && String(run.workflow_id) !== workflowFileId) {
return false;
}
if (pullRequestNumber &&
!(run.pull_requests || []).some(pullRequest =>
String(pullRequest.number) === pullRequestNumber)) {
return false;
}
return true;
}

for (const name of names) {
const artifacts = [];
let page = 1;
while (true) {
const response = await github.rest.actions.listArtifactsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
name,
per_page: 100,
page
});
artifacts.push(...(response.data.artifacts || []));

if ((response.data.artifacts || []).length < 100) {
break;
}
page += 1;
}

for (const artifact of artifacts) {
const artifactRunId = artifact.workflow_run?.id;
if (String(artifactRunId) === String(currentRunId) ||
artifact.expired) {
continue;
}
if ((pullRequestNumber || workflowId) &&
!(await belongsToScope(artifactRunId))) {
continue;
}

core.info(`Deleting ${name} artifact ${artifact.id}`);
await github.rest.actions.deleteArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[workflow] Remove artifacts after successful runs

2 participants