Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/christian-korneck/delete-run-artifacts-action
github action to delete artifacts at the end of a workflow run
https://github.com/christian-korneck/delete-run-artifacts-action
artifact artifacts artifacts-deletion github github-actions github-workflow
Last synced: 3 months ago
JSON representation
github action to delete artifacts at the end of a workflow run
- Host: GitHub
- URL: https://github.com/christian-korneck/delete-run-artifacts-action
- Owner: christian-korneck
- License: mit
- Created: 2020-03-10T10:33:26.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-02T14:59:05.000Z (over 1 year ago)
- Last Synced: 2024-11-11T15:44:07.576Z (3 months ago)
- Topics: artifact, artifacts, artifacts-deletion, github, github-actions, github-workflow
- Language: JavaScript
- Homepage:
- Size: 503 KB
- Stars: 28
- Watchers: 3
- Forks: 10
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**WARNING: This action deletes data. Use with care and at your own risk.**

# delete-run-artifacts
This github action deletes all artifacts generated by a github workflow run. It's intended to be used to delete all artifacts generated by a run at the end of this run.
This can be useful if your workflow temporarily stores artifacts and they're not needed anymore by the end of the run (i.e. because they got uploaded or published as a release by one of the steps). Deleting these redundant artifacts at the end of the run saves storage space (which is limited for private repos).
# How to use
This action requires a bit more preparation in your repo than most other actions. Because artifacts cannot get deleted during a workflow run (only after a run has completed), we need to spawn a separate workflow run for the deletion by a webhook at the end of the main workflow run.
First, create a personal access token (required to call a webhook):
- in your github **user/org** settings: *Settings* -> *Developer Settings* -> *[Personal access tokens](https://github.com/settings/tokens)* -> *[generate new token](https://github.com/settings/tokens/new)*
- *Note*: `for webhooks`
- *Scope*: `repo` ([docs](https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/))
- copy the generated tokenthen store the token as a secret in the repo, so that the workflow can use it:
- in your github **repo** settings: *Settings* tab -> *Secrets* -> *add a new secret*
- *Name*: `FOR_WEBHOOKS_SECRET`
- *Value*: ``then create a file in your repo `.github/workflows/webhook_target.yml` with:
```
name: delete calling job's artifacts
on: repository_dispatch
jobs:
main:
runs-on: ubuntu-latest
steps:
- name: Delete artifacts
if: github.event.action == 'delete_all_artifacts'
uses: christian-korneck/delete-run-artifacts-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
parent_runid: ${{ github.event.client_payload.parent_runid }}
parent_repo: ${{ github.event.client_payload.parent_repo }}
```and finally add the following cleanup job to your actual workflow (i.e. in `.github/workflows/workflow.yml`).
Make sure that `needs:` points to your last job in the workflow, otherwise the cleanup might run too early.```
name: your workflow
on: [push]
your_last_job:
(... add your stuff here ...)
cleanup_job:
needs: [your_last_job]
if: always()
runs-on: ubuntu-latest
steps:
- name: call webhook to delete artifacts
env:
FOR_WEBHOOKS_SECRET: ${{ secrets.FOR_WEBHOOKS_SECRET }}
run: |
echo "::add-mask::$FOR_WEBHOOKS_SECRET"
curl --verbose --fail --show-error --location --request POST "https://api.github.com/repos/$GITHUB_REPOSITORY/dispatches" --header "Authorization: token $FOR_WEBHOOKS_SECRET" --header 'Content-Type: application/json' --header 'Accept: application/vnd.github.everest-preview+json' --data-raw "{ \"event_type\": \"delete_all_artifacts\", \"client_payload\": {\"parent_runid\": \"$GITHUB_RUN_ID\", \"parent_repo\": \"$GITHUB_REPOSITORY\"} }"
```Please note: Due to a github [limitation](https://github.community/t5/GitHub-Actions/repository-dispatch-not-triggering-actions/m-p/33845#M1677), the file `.github/workflows/webhook_target.yml` needs to be always present in your `master` or default branch. The action will work with any branch however.
(Check [this repo](https://github.com/christian-korneck/captive) or the `.github/workflow` dir for a working example)# Reference
The action can also be used without a webhook on any past workflow run:
## Inputs
### `parent_repo`
**Required** the github repo that hosts the parent github workflow run. Example: `"github-user/hello-world"`.
### `parent_runid`
**Required** the id of the parent github workflow run whos artifacts should get deleted. Example: `"0000000"`.
## Outputs
none (just succeeds or fails)