'Github Actions: keep artifacts only for the latest build on each branch
I'm using Github Actions to build my project and upload artifacts to Github with the following part of workflow:
- uses: actions/upload-artifact@v2
with:
name: some-file
path: some-path
Problem is that artifacts are quite big and consume available storage quota quickly. And also I don't even need all of them, just ones from the latest build on each branch.
Setting up retention period is not a solution because it will also delete artifacts from the latest build.
Solution 1:[1]
You can use Artifacts API for this.
First you should get a list of all your artifacts:
GET /repos/{owner}/{repo}/actions/artifacts
You'll need to adjust artifact's name to include the branch name, because there is no info about it in the response object.
Then filter through them, get the old ones and delete them:
DELETE /repos/{owner}/{repo}/actions/artifacts/{artifact_id}
This step should be run probably before you upload your new one. But that is up to you, what strategy you use ?
Here's a simple example using octokit:
jobs:
doit:
runs-on: ubuntu-latest
steps:
- name: Create some file to upload
run: echo "test" > test.txt
- name: Store artifact's name
id: artifact-name
env:
REF: ${{ github.ref }}
run: echo "::set-output name=value::${REF////-}-test"
- name: List, filter and delete artifacts
uses: actions/github-script@v4
id: artifact
with:
script: |
const { owner, repo } = context.issue
const res = await github.actions.listArtifactsForRepo({
owner,
repo,
})
res.data.artifacts
.filter(({ name }) => name === '${{ steps.artifact-name.outputs.value }}')
.forEach(({ id }) => {
github.actions.deleteArtifact({
owner,
repo,
artifact_id: id,
})
})
- name: Upload latest artifact
uses: actions/upload-artifact@v2
with:
name: ${{ steps.artifact-name.outputs.value }}
path: test.txt
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
