From ba8f69a5ce0d2862c509cba06468a0373a680427 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 16 Nov 2022 23:51:38 +0100 Subject: [PATCH] Cache improvements [ci] (#80898) * Cleanup old pip caches * Add workflow to delete caches for closed PRs --- .github/workflows/cache.yml | 56 ++++++++++++++++++++++++++++++++ .github/workflows/ci.yaml | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 .github/workflows/cache.yml diff --git a/.github/workflows/cache.yml b/.github/workflows/cache.yml new file mode 100644 index 00000000000..8a1bcad6570 --- /dev/null +++ b/.github/workflows/cache.yml @@ -0,0 +1,56 @@ +name: Delete Caches + +# yamllint disable-line rule:truthy +on: + pull_request: + types: [closed] + workflow_dispatch: + inputs: + pr-number: + description: "Closed PR number" + type: string + required: true + +jobs: + delete: + name: "Delete unused caches (PR #${{ github.event.inputs.pr-number || github.event.number }})" + runs-on: ubuntu-latest + permissions: + actions: write + steps: + - name: Delete caches + run: | + ref="refs/pull/${{ github.event.inputs.pr-number || github.event.number }}/merge" + + page=1 + per_page=100 + del_count=0 + + while true; do + res=$(curl -fsS \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${{ github.token }}" \ + "https://api.github.com/repos/${{ github.repository }}/actions/caches?per_page=$per_page&page=$page") + + res_count=$(echo $res | jq '.actions_caches | length') + if [ $res_count -eq 0 ]; then break; fi + + targets=$(echo $res | jq \ + --arg ref "$ref" \ + '.actions_caches[] | select(.ref == $ref) | del(.created_at, .size_in_bytes, .version)') + echo "$targets" + + for id in $(echo $targets | jq '.id'); do + curl -fsS \ + -X DELETE \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${{ github.token }}" \ + "https://api.github.com/repos/${{ github.repository }}/actions/caches/$id" + ((del_count+=1)) + done + + if [ $res_count -lt $per_page ]; then break; fi + ((page+=1)) + done + + echo "Delete $del_count caches." diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 262229e6ade..cd21717142a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -551,6 +551,70 @@ jobs: pip install --cache-dir=$PIP_CACHE -r requirements_test.txt --use-deprecated=legacy-resolver pip install -e . + cleanup-cache: + name: Cleanup old pip caches + runs-on: ubuntu-20.04 + needs: + - base + permissions: + actions: write + steps: + - name: Cleanup + run: | + sort="last_accessed_at" + page=1 + per_page=100 + del_count=0 + index=0 + + all_versions=($(echo $ALL_PYTHON_VERSIONS | tr -d \',[])) + + if [[ "${{ github.ref }}" == "refs/heads/dev" ]] \ + || [[ "${{ contains(github.event.pull_request.labels.*.name, 'ci-keep-cache') }}" == "true" ]] + then + index=1 + echo "Keep one entry per Python version" + fi + + while true; do + res=$(curl -fsS \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${{ github.token }}" \ + "https://api.github.com/repos/${{ github.repository }}/actions/caches?sort=$sort&per_page=$per_page&page=$page") + + res_count=$(echo $res | jq '.actions_caches | length') + if [ $res_count -eq 0 ]; then break; fi + + echo "Check ref: ${{ github.ref }}" + for version in ${all_versions[@]}; do + key="${{ runner.os }}-$version.+-pip" + echo "Check key regex: $key" + + # Select all cache keys which match the ref and key regex + # Keep the first entry for 'ref/heads/dev' + targets=$(echo $res | jq \ + --arg ref "${{ github.ref }}" --arg key "$key" --arg index $index \ + '[.actions_caches[] | select(.ref == $ref) | select(.key | test($key))][$index | fromjson:][] | del(.created_at, .size_in_bytes, .version)') + + if [ $(echo $targets | jq -s 'length') -eq 0 ]; then continue; fi + echo "$targets" + + for id in $(echo $targets | jq '.id'); do + curl -fsS \ + -X DELETE \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: token ${{ github.token }}" \ + "https://api.github.com/repos/${{ github.repository }}/actions/caches/$id" + ((del_count += 1)) + done + done + + if [ $res_count -lt $per_page ]; then break; fi + ((page += 1)) + done + + echo "Deleted $del_count caches." + hassfest: name: Check hassfest runs-on: ubuntu-20.04