|
| 1 | +# This file was automatically generated by sbt-github-actions using the |
| 2 | +# githubWorkflowGenerate task. You should add and commit this file to |
| 3 | +# your git repository. It goes without saying that you shouldn't edit |
| 4 | +# this file by hand! Instead, if you wish to make changes, you should |
| 5 | +# change your sbt build configuration to revise the workflow description |
| 6 | +# to meet your needs, then regenerate this file. |
| 7 | + |
| 8 | +name: Clean |
| 9 | + |
| 10 | +on: push |
| 11 | + |
| 12 | +jobs: |
| 13 | + delete-artifacts: |
| 14 | + name: Delete Artifacts |
| 15 | + runs-on: ubuntu-latest |
| 16 | + env: |
| 17 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 18 | + steps: |
| 19 | + - name: Delete artifacts |
| 20 | + shell: bash {0} |
| 21 | + run: | |
| 22 | + # Customize those three lines with your repository and credentials: |
| 23 | + REPO=${GITHUB_API_URL}/repos/${{ github.repository }} |
| 24 | +
|
| 25 | + # A shortcut to call GitHub API. |
| 26 | + ghapi() { curl --silent --location --user _:$GITHUB_TOKEN "$@"; } |
| 27 | +
|
| 28 | + # A temporary file which receives HTTP response headers. |
| 29 | + TMPFILE=$(mktemp) |
| 30 | +
|
| 31 | + # An associative array, key: artifact name, value: number of artifacts of that name. |
| 32 | + declare -A ARTCOUNT |
| 33 | +
|
| 34 | + # Process all artifacts on this repository, loop on returned "pages". |
| 35 | + URL=$REPO/actions/artifacts |
| 36 | + while [[ -n "$URL" ]]; do |
| 37 | +
|
| 38 | + # Get current page, get response headers in a temporary file. |
| 39 | + JSON=$(ghapi --dump-header $TMPFILE "$URL") |
| 40 | +
|
| 41 | + # Get URL of next page. Will be empty if we are at the last page. |
| 42 | + URL=$(grep '^Link:' "$TMPFILE" | tr ',' '\n' | grep 'rel="next"' | head -1 | sed -e 's/.*<//' -e 's/>.*//') |
| 43 | + rm -f $TMPFILE |
| 44 | +
|
| 45 | + # Number of artifacts on this page: |
| 46 | + COUNT=$(( $(jq <<<$JSON -r '.artifacts | length') )) |
| 47 | +
|
| 48 | + # Loop on all artifacts on this page. |
| 49 | + for ((i=0; $i < $COUNT; i++)); do |
| 50 | +
|
| 51 | + # Get name of artifact and count instances of this name. |
| 52 | + name=$(jq <<<$JSON -r ".artifacts[$i].name?") |
| 53 | + ARTCOUNT[$name]=$(( $(( ${ARTCOUNT[$name]} )) + 1)) |
| 54 | +
|
| 55 | + id=$(jq <<<$JSON -r ".artifacts[$i].id?") |
| 56 | + size=$(( $(jq <<<$JSON -r ".artifacts[$i].size_in_bytes?") )) |
| 57 | + printf "Deleting '%s' #%d, %'d bytes\n" $name ${ARTCOUNT[$name]} $size |
| 58 | + ghapi -X DELETE $REPO/actions/artifacts/$id |
| 59 | + done |
| 60 | + done |
0 commit comments