From 95805b5c6d0684e6bcc364808e017b84c1b50373 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Sun, 28 Aug 2022 19:15:51 +0900 Subject: [PATCH 01/13] Add clear-cache workflow --- .github/workflows/clear-cache.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/clear-cache.yml diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml new file mode 100644 index 00000000..f0ea137a --- /dev/null +++ b/.github/workflows/clear-cache.yml @@ -0,0 +1,31 @@ +name: Clear cache flow + +on: + workflow_call: + +jobs: + clear-cache: + runs-on: ubuntu-20.04 + + steps: + - name: Remove caches + run: | + TOTAL_COUNT=0 + while true; do + LIST_CACHES=$(gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches) + REMAINING_COUNT=$(echo $LIST_CACHES | jq .total_count) + [[ $REMAINING_COUNT > 0 ]] || break + IDS=$(echo $LIST_CACHES | jq .actions_caches[].id) + for ID in ${IDS[@]}; do + echo "Clearing $ID" + gh api --method DELETE -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches/$ID + TOTAL_COUNT=$((TOTAL_COUNT + 1)) + done + done + echo "Cleared $TOTAL_COUNT caches" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: List available caches + run: | + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches/$ID From 3c9b605bda425a86faa40c3c6960197076f16c6f Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 11:44:14 +0900 Subject: [PATCH 02/13] Fix reusable workflow --- .github/workflows/clear-cache.yml | 35 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index f0ea137a..f595726e 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -2,30 +2,35 @@ name: Clear cache flow on: workflow_call: + inputs: + pattern: + description: 'string to grep cache keys with' + required: true + type: string jobs: clear-cache: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: + - name: Get the list of cache keys + run: | + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt + cat cache_keys.txt + + - name: Filter cache keys + run: | + grep -E ${{ inputs.pattern }} cache_keys.txt > cache_keys_to_remove.txt + cat cache_keys_to_remove.txt + - name: Remove caches run: | - TOTAL_COUNT=0 - while true; do - LIST_CACHES=$(gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches) - REMAINING_COUNT=$(echo $LIST_CACHES | jq .total_count) - [[ $REMAINING_COUNT > 0 ]] || break - IDS=$(echo $LIST_CACHES | jq .actions_caches[].id) - for ID in ${IDS[@]}; do - echo "Clearing $ID" - gh api --method DELETE -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches/$ID - TOTAL_COUNT=$((TOTAL_COUNT + 1)) - done + for key in $(cat cache_keys_to_remove.txt); do + gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" done - echo "Cleared $TOTAL_COUNT caches" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: List available caches + - name: Show the list of remaining caches run: | - gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches/$ID + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches From 3187ba86419c6d3dada1cf7899152bc92e703ec4 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 11:46:38 +0900 Subject: [PATCH 03/13] Apply clear cache --- .github/workflows/schedule-clear-cache.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/schedule-clear-cache.yml diff --git a/.github/workflows/schedule-clear-cache.yml b/.github/workflows/schedule-clear-cache.yml new file mode 100644 index 00000000..d8d331d2 --- /dev/null +++ b/.github/workflows/schedule-clear-cache.yml @@ -0,0 +1,14 @@ +name: Clear cache weekly + +on: + push: {} + # schedule: + # # on Sundays + # - cron: "0 0 * * 0" + +jobs: + clear-cache: + if: ${{ github.repository_owner == 'Lightning-AI' }} + uses: Lightning-AI/devtools/.github/workflows/clear-cache.yml@v0.3.0 + with: + pattern: 'pip' From 9900f63103c17e33bd390de1f4df28c28e09f6b5 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 11:48:43 +0900 Subject: [PATCH 04/13] Fix tag --- .github/workflows/schedule-clear-cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/schedule-clear-cache.yml b/.github/workflows/schedule-clear-cache.yml index d8d331d2..9c6096d0 100644 --- a/.github/workflows/schedule-clear-cache.yml +++ b/.github/workflows/schedule-clear-cache.yml @@ -9,6 +9,6 @@ on: jobs: clear-cache: if: ${{ github.repository_owner == 'Lightning-AI' }} - uses: Lightning-AI/devtools/.github/workflows/clear-cache.yml@v0.3.0 + uses: ./.github/workflows/clear-cache.yml with: pattern: 'pip' From d5b90c32995f429c177ae7a918fc4c02aa1b6ea9 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:06:25 +0900 Subject: [PATCH 05/13] succeeds if no match --- .github/workflows/clear-cache.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index f595726e..20906882 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -15,13 +15,11 @@ jobs: steps: - name: Get the list of cache keys run: | - gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt - cat cache_keys.txt + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt && cat cache_keys.txt || echo "no cache keys found with the pattern" - name: Filter cache keys run: | - grep -E ${{ inputs.pattern }} cache_keys.txt > cache_keys_to_remove.txt - cat cache_keys_to_remove.txt + grep -E ${{ inputs.pattern }} cache_keys.txt > cache_keys_to_remove.txt && cat cache_keys_to_remove.txt || echo "no cache keys found with the pattern" - name: Remove caches run: | From 89765dfcc85a8473094641e6033e35f0a60f6615 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:07:42 +0900 Subject: [PATCH 06/13] gh token --- .github/workflows/clear-cache.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 20906882..702100a2 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -16,6 +16,8 @@ jobs: - name: Get the list of cache keys run: | gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt && cat cache_keys.txt || echo "no cache keys found with the pattern" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Filter cache keys run: | @@ -32,3 +34,5 @@ jobs: - name: Show the list of remaining caches run: | gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 7dd887c6c545ce4c7444b7f601e65b1d0e03ae32 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:09:00 +0900 Subject: [PATCH 07/13] friendlier output --- .github/workflows/clear-cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 702100a2..71d6cb33 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -33,6 +33,6 @@ jobs: - name: Show the list of remaining caches run: | - gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 9945dcfb9267505c480675dbb27b13a4de4f92bf Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:10:28 +0900 Subject: [PATCH 08/13] experiment: remove pre-commit caches --- .github/workflows/schedule-clear-cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/schedule-clear-cache.yml b/.github/workflows/schedule-clear-cache.yml index 9c6096d0..a029b619 100644 --- a/.github/workflows/schedule-clear-cache.yml +++ b/.github/workflows/schedule-clear-cache.yml @@ -11,4 +11,4 @@ jobs: if: ${{ github.repository_owner == 'Lightning-AI' }} uses: ./.github/workflows/clear-cache.yml with: - pattern: 'pip' + pattern: 'pre-commit' From 265ac9f2006d5be5201fe4b8102af32eb83f90df Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:29:11 +0900 Subject: [PATCH 09/13] friendlier output --- .github/workflows/clear-cache.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 71d6cb33..1b2da212 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -25,8 +25,9 @@ jobs: - name: Remove caches run: | + echo last_accessed_at created_at ref key size_in_bytes for key in $(cat cache_keys_to_remove.txt); do - gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" + gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" --jq '.actions_caches[] | [.last_accessed_at, .created_at, .ref, .key, .size_in_bytes] | join(" ")' done env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 7000314346ba860c599dbe4c876aeae4797922a5 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:31:18 +0900 Subject: [PATCH 10/13] friendlier output --- .github/workflows/clear-cache.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 1b2da212..d7692b42 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -25,9 +25,9 @@ jobs: - name: Remove caches run: | - echo last_accessed_at created_at ref key size_in_bytes + echo created_at last_accessed_at ref key size_in_bytes for key in $(cat cache_keys_to_remove.txt); do - gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" --jq '.actions_caches[] | [.last_accessed_at, .created_at, .ref, .key, .size_in_bytes] | join(" ")' + gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" --jq '.actions_caches[] | [.created_at, .last_accessed_at, .ref, .key, .size_in_bytes] | join(" ")' done env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From b13671e9f4c09495aaae1e2c2c4556d17c88a8b3 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 12:36:13 +0900 Subject: [PATCH 11/13] Fix trigger for merge --- .github/workflows/schedule-clear-cache.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/schedule-clear-cache.yml b/.github/workflows/schedule-clear-cache.yml index a029b619..3657a93f 100644 --- a/.github/workflows/schedule-clear-cache.yml +++ b/.github/workflows/schedule-clear-cache.yml @@ -1,10 +1,9 @@ name: Clear cache weekly on: - push: {} - # schedule: - # # on Sundays - # - cron: "0 0 * * 0" + schedule: + # on Sundays + - cron: "0 0 * * 0" jobs: clear-cache: From 2a24a856ac0b529c7baf29e5a2a040e58fe78e28 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 8 Sep 2022 13:34:15 +0900 Subject: [PATCH 12/13] Remove remaining logic --- .github/actions/cache/action.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/actions/cache/action.yml b/.github/actions/cache/action.yml index 246128df..53104039 100644 --- a/.github/actions/cache/action.yml +++ b/.github/actions/cache/action.yml @@ -32,12 +32,6 @@ runs: run: python .github/assistant.py set-oldest-versions shell: bash - - run: python -c "import time ; days = time.time() / 60 / 60 / 24 ; print(f'TIME_PERIOD={int(days / ${{ inputs.interval }})}')" >> $GITHUB_ENV - if: inputs.requires == 'latest' - shell: bash - - # Note: This uses an internal pip API and may not always work - # https://github.com/actions/cache/blob/master/examples.md#multiple-oss-in-a-workflow - name: Determine caches env: PWD: $(pwd) @@ -49,13 +43,13 @@ runs: uses: actions/cache@v3 with: path: $PIP_CACHE_DIR - key: ${{ runner.os }}-py${{ inputs.python-version }}-pip-td${{ env.TIME_PERIOD }}-${{ inputs.offset }}-${{ hashFiles('requirements.txt') }} - restore-keys: ${{ runner.os }}-py${{ inputs.python-version }}-pip-td${{ env.TIME_PERIOD }}-${{ inputs.offset }}- + key: ${{ runner.os }}-py${{ inputs.python-version }}-pip-${{ inputs.offset }}-${{ hashFiles('requirements.txt') }} + restore-keys: ${{ runner.os }}-py${{ inputs.python-version }}-pip-${{ inputs.offset }}- - name: Cache conda uses: actions/cache@v3 if: runner.os == 'Linux' with: path: ~/conda_pkgs_dir - key: py${{ inputs.python-version }}-conda-td${{ env.TIME_PERIOD }}-${{ inputs.offset }} - restore-keys: py${{ inputs.python-version }}-conda-td${{ env.TIME_PERIOD }}-${{ inputs.offset }} + key: py${{ inputs.python-version }}-conda-${{ inputs.offset }} + restore-keys: py${{ inputs.python-version }}-conda-${{ inputs.offset }} From bb6d9e03d01045c119083ffa0bfbfab738893ee8 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Fri, 9 Sep 2022 05:53:47 +0900 Subject: [PATCH 13/13] Update --- .github/workflows/clear-cache.yml | 12 ++++++------ .github/workflows/schedule-clear-cache.yml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index d7692b42..aff5e707 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -14,26 +14,26 @@ jobs: steps: - name: Get the list of cache keys - run: | - gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt && cat cache_keys.txt || echo "no cache keys found with the pattern" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq > cache_keys.txt && cat cache_keys.txt || echo "no cache keys found with the pattern" - name: Filter cache keys run: | grep -E ${{ inputs.pattern }} cache_keys.txt > cache_keys_to_remove.txt && cat cache_keys_to_remove.txt || echo "no cache keys found with the pattern" - name: Remove caches + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | echo created_at last_accessed_at ref key size_in_bytes for key in $(cat cache_keys_to_remove.txt); do gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${{ github.repository }}/actions/caches?key=${key}" --jq '.actions_caches[] | [.created_at, .last_accessed_at, .ref, .key, .size_in_bytes] | join(" ")' done - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Show the list of remaining caches - run: | - gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh api -H "Accept: application/vnd.github+json" /repos/${{ github.repository }}/actions/caches --paginate --jq ".actions_caches[].key" | sort | uniq diff --git a/.github/workflows/schedule-clear-cache.yml b/.github/workflows/schedule-clear-cache.yml index 3657a93f..cdc7ab96 100644 --- a/.github/workflows/schedule-clear-cache.yml +++ b/.github/workflows/schedule-clear-cache.yml @@ -10,4 +10,4 @@ jobs: if: ${{ github.repository_owner == 'Lightning-AI' }} uses: ./.github/workflows/clear-cache.yml with: - pattern: 'pre-commit' + pattern: 'pip|conda'