Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete artifacts uploaded by actions/upload-artifact@v4 #24

Closed
wants to merge 25 commits into from

Conversation

muzimuzhi
Copy link
Owner

@muzimuzhi muzimuzhi commented Dec 19, 2023

@muzimuzhi
Copy link
Owner Author

muzimuzhi commented Dec 19, 2023

GeekyEggo/delete-artifact@v2 can only delete artifacts uploaded by actions/upload-artifact@v3, not v4.

Comment on lines 118 to +125
run: |
echo "Hello GitHub Actions"
export GH_REPO="${{ github.repository }}"
gh api \
--method DELETE \
/repos/{owner}/{repo}/actions/artifacts/"$EXTRA_ID"
env:
EXTRA_ID: ${{ needs.upload.outputs.extra-id }}
GH_TOKEN: ${{ github.token }}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Artifact IDs can be passed to downstream step through

  • upstream step output artifact-id set by actions/upload-artifact@v4
  • upstream job output <output_name>: ${{ steps.<step_id>.outputs.artifact-id }}
  • (optional) downstream job (step) env <env_var>: ${{ needs.<upstream_job_id>.outputs.<output_name> }}

Job outputs are available to all downstream jobs that depend on this job.
https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs

@muzimuzhi
Copy link
Owner Author

muzimuzhi commented Dec 20, 2023

Till now, two successful ways to delete artifact(s) uploaded by actions/upload-artifact@v4 are demonstrated.

The common part is that an artifact is deleted through

gh api --method DELETE /repos/OWNER/REPO/actions/artifacts/ARTIFACT_ID

see API doc https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#delete-an-artifact.

What's different is how artifact id is fetched:

  • Job "Upload artifacts", step "Delete multiple artifacts with gh" uses gh api /repos/OWNER/REPO/actions/runs/RUN_ID/artifacts to get list of first 30 artifacts in a workflow run, then filter json response with jq to get artifact name-id bindings.
  • Job "Delete artifacts", step "Delete last artifact" takes an artifact id from upstream job outputs, which comes from a step output.
    This way, the bash script is shorter, but one has to name steps and jobs.

@muzimuzhi
Copy link
Owner Author

muzimuzhi commented Dec 20, 2023

Other possibilities

@muzimuzhi
Copy link
Owner Author

muzimuzhi commented Dec 22, 2023

@octokit/plugin-rest-endpoint-methods (npm package) provides "one method for each REST API endpoint".

octokit.rest.actions.listWorkflowRunArtifacts({
  owner,
  repo,
  run_id,
});

octokit.rest.actions.deleteArtifact({
  owner,
  repo,
  artifact_id,
});

@muzimuzhi
Copy link
Owner Author

Supported by GeekyEggo/delete-artifact@v4.

https://github.com/GeekyEggo/delete-artifact/releases/tag/v4.0.0

@muzimuzhi muzimuzhi closed this Dec 24, 2023
@muzimuzhi
Copy link
Owner Author

The ideal solution is that @actions/download-artifact supports a new boolean input to control whether to delete downloaded artifacts in runs.post step.

  • @actions/checkout uses !!core.getState('isPost') to distinguish main and post states, so it can set main: dist/index.js along with post: dist/index.js. See its action.yml and main.ts.
  • Compared to GeekyEggo/delete-artifact, actions/download-artifact is much simpler (see download-artifact.ts). It simply calls methods artifactClient.getArtifact(), artifactClient.listArtifacts(), and artifactClient.downloadArtifact() from @actions/artifact package, which wraps @octokit/core.
    Currently @actions/artifact doesn't provide artifactClient.deleteArtifact().

@muzimuzhi muzimuzhi deleted the self/delete-artifacts branch December 24, 2023 23:20
@muzimuzhi muzimuzhi added the discovery What? Why? How? label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discovery What? Why? How?
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant