|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +# Parameters: |
| 21 | +# |
| 22 | +# GITHUB_REPO - repository to delete the artifacts |
| 23 | +# GITHUB_USER - your personal user name |
| 24 | +# GITHUB_TOKEN - your personal token with `repo` scope |
| 25 | +# |
| 26 | +GITHUB_REPO=https://api.github.com/repos/apache/airflow |
| 27 | +readonly GITHUB_REPO |
| 28 | + |
| 29 | +if [[ -z ${GITHUB_USER} ]]; then |
| 30 | + echo 2>&1 |
| 31 | + echo 2>&1 "Set GITHUB_USER variable to your user" |
| 32 | + echo 2>&1 |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | +readonly GITHUB_USER |
| 36 | + |
| 37 | +if [[ -z ${GITHUB_TOKEN} ]]; then |
| 38 | + echo 2>&1 |
| 39 | + echo 2>&1 "Set GITHUB_TOKEN variable to a token with 'repo' scope" |
| 40 | + echo 2>&1 |
| 41 | + exit 2 |
| 42 | +fi |
| 43 | +GITHUB_TOKEN=${GITHUB_TOKEN} |
| 44 | +readonly GITHUB_TOKEN |
| 45 | + |
| 46 | +function github_api_call() { |
| 47 | + curl --silent --location --user "${GITHUB_USER}:${GITHUB_TOKEN}" "$@" |
| 48 | +} |
| 49 | + |
| 50 | +# A temporary file which receives HTTP response headers. |
| 51 | +TEMPFILE=$(mktemp) |
| 52 | +readonly TEMPFILE |
| 53 | + |
| 54 | +function loop_through_artifacts_and_delete() { |
| 55 | + |
| 56 | + # Process all artifacts on this repository, loop on returned "pages". |
| 57 | + artifact_url=${GITHUB_REPO}/actions/artifacts |
| 58 | + |
| 59 | + while [[ -n "${artifact_url}" ]]; do |
| 60 | + # Get current page, get response headers in a temporary file. |
| 61 | + json=$(github_api_call --dump-header "${TEMPFILE}" "$artifact_url") |
| 62 | + |
| 63 | + # Get artifact_url of next page. Will be empty if we are at the last page. |
| 64 | + artifact_url=$(grep '^Link:' "$TEMPFILE" | tr ',' '\n' | \ |
| 65 | + grep 'rel="next"' | head -1 | sed -e 's/.*<//' -e 's/>.*//') |
| 66 | + rm -f "${TEMPFILE}" |
| 67 | + |
| 68 | + # Number of artifacts on this page: |
| 69 | + count=$(($(jq <<<"${json}" -r '.artifacts | length'))) |
| 70 | + |
| 71 | + # Loop on all artifacts on this page. |
| 72 | + for ((i = 0; "${i}" < "${count}"; i++)); do |
| 73 | + # Get the name of artifact and count instances of this name |
| 74 | + name=$(jq <<<"${json}" -r ".artifacts[$i].name?") |
| 75 | + id=$(jq <<<"${json}" -r ".artifacts[$i].id?") |
| 76 | + size=$(($(jq <<<"${json}" -r ".artifacts[$i].size_in_bytes?"))) |
| 77 | + printf "Deleting '%s': [%s] : %'d bytes\n" "${name}" "${id}" "${size}" |
| 78 | + github_api_call -X DELETE "${GITHUB_REPO}/actions/artifacts/${id}" |
| 79 | + sleep 1 # There is a Github API limit of 5000 calls/hr. This is to limit the API calls below that |
| 80 | + done |
| 81 | + done |
| 82 | +} |
| 83 | + |
| 84 | +loop_through_artifacts_and_delete |
0 commit comments