[workflow] Remove artifacts after successful runs - #1740
Draft
Steven Ramirez Rosa (Steven6798) wants to merge 1 commit into
Draft
[workflow] Remove artifacts after successful runs#1740Steven Ramirez Rosa (Steven6798) wants to merge 1 commit into
Steven Ramirez Rosa (Steven6798) wants to merge 1 commit into
Conversation
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 | ||
| }); | ||
| } | ||
| } |
Steven Ramirez Rosa (Steven6798)
marked this pull request as draft
August 19, 2026 20:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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