Clean GitHub caches #20009
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
workflow_dispatch: | |
inputs: | |
cache_type: | |
type: choice | |
description: What kind of caches to delete. Note that *all* caches of that type will be deleted. | |
options: | |
- branch | |
- tar | |
- selective | |
required: false | |
default: selective | |
cache_keys: | |
type: string | |
description: Space separated string of cache keys to delete. | |
required: false | |
workflow_run: | |
workflows: | |
- CI | |
- Deployments | |
types: | |
- completed | |
delete: | |
pull_request_target: | |
types: | |
- closed | |
name: Clean GitHub caches | |
jobs: | |
automatic_cleanup: | |
name: Clean up GitHub caches produced by other workflows | |
if: github.event_name == 'workflow_run' | |
permissions: write-all | |
runs-on: ubuntu-latest | |
steps: | |
- name: Download cache keys | |
id: artifacts | |
run: | | |
artifacts_url=${{ github.event.workflow_run.artifacts_url }} | |
artifacts=$(gh api $artifacts_url -q '.artifacts[] | {name: .name, url: .archive_download_url}') | |
for artifact in `echo "$artifacts"`; do | |
name=`echo $artifact | jq -r '.name'` | |
if [ "${name#cache_key}" != "${name}" ]; then | |
url=`echo $artifact | jq -r '.url'` | |
gh api $url > cache_keys.zip | |
unzip -d cache_keys cache_keys.zip | |
for file in `find cache_keys/ -type f`; do | |
keys+=" `cat $file`" | |
done | |
rm -rf cache_keys cache_keys.zip | |
fi | |
done | |
echo "cache_keys='$(echo $keys)'" >> $GITHUB_OUTPUT | |
env: | |
GH_TOKEN: ${{ github.token }} | |
- run: | | |
gh extension install actions/gh-actions-cache | |
for key in `echo ${{ steps.artifacts.outputs.cache_keys }}`; do | |
(gh actions-cache delete $key -R ${{ github.repository }} --confirm && echo "Deleted cache $key") \ | |
|| echo "Cache $key not found" | |
done | |
env: | |
GH_TOKEN: ${{ github.token }} | |
selective_cleanup: | |
name: Clean up selective GitHub caches | |
if: (github.event_name == 'workflow_dispatch' && inputs.cache_keys != '' ) | |
runs-on: ubuntu-latest | |
permissions: write-all | |
steps: | |
- run: | | |
gh extension install actions/gh-actions-cache | |
for key in ${{ inputs.cache_keys }}; do | |
(gh actions-cache delete $key -R ${{ github.repository }} --confirm && echo "Deleted cache $key") \ | |
|| echo "Cache $key not found" | |
done | |
env: | |
GH_TOKEN: ${{ github.token }} | |
tar_cleanup: | |
name: Clean up Github caches with tar archives | |
if: (github.event_name == 'workflow_dispatch' && inputs.cache_type == 'tar' ) | |
runs-on: ubuntu-latest | |
permissions: write-all | |
steps: | |
- run: | | |
gh extension install actions/gh-actions-cache | |
keys=$(gh actions-cache list -R ${{ github.repository }} --key tar- | cut -f 1 ) | |
for key in $keys; do | |
(gh actions-cache delete $key -R ${{ github.repository }} --confirm && echo "Deleted cache $key") \ | |
|| echo "Cache $key not found" | |
done | |
env: | |
GH_TOKEN: ${{ github.token }} | |
branch_cleanup: | |
name: Clean up branch specific caches | |
if: (github.event_name == 'workflow_dispatch' && inputs.cache_type == 'branch' ) || (github.event_name == 'delete' && github.event.ref_type == 'branch') | |
runs-on: ubuntu-latest | |
permissions: write-all | |
steps: | |
- name: Delete GitHub caches | |
run: | | |
gh extension install actions/gh-actions-cache | |
keys=$(gh actions-cache list -R ${{ github.repository }} -B ${{ github.event.ref }} | cut -f 1 ) | |
for key in $keys; do | |
(gh actions-cache delete $key -R ${{ github.repository }} --confirm && echo "Deleted cache $key") \ | |
|| echo "Cache $key not found" | |
done | |
env: | |
GH_TOKEN: ${{ github.token }} | |
# For the installer validation, we push a cache for the minimal MPI build. | |
# This cache is pushed every time, and some caches are even shared between different branches. | |
# We can hence delete the entire build cache for all branches at any time without significant | |
# downsides, and do just that here to have some periodic clean up. | |
- name: Delete build caches for MPI asset (ARM64) | |
uses: actions/delete-package-versions@v5 | |
with: | |
package-name: buildcache-cuda-quantum-assets-openmpi-gcc11-arm64 | |
package-type: 'container' | |
min-versions-to-keep: 1 # the used action does not support 0 here | |
- name: Delete build caches for MPI asset (AMD64) | |
uses: actions/delete-package-versions@v5 | |
with: | |
package-name: buildcache-cuda-quantum-assets-openmpi-gcc11-amd64 | |
package-type: 'container' | |
min-versions-to-keep: 1 # the used action does not support 0 here | |
pr_cleanup: | |
name: Clean up PR related GitHub caches | |
if: github.event_name == 'pull_request_target' | |
runs-on: ubuntu-latest | |
permissions: write-all | |
steps: | |
- run: | | |
gh extension install actions/gh-actions-cache | |
keys=$(gh actions-cache list -R ${{ github.repository }} -B refs/pull/${{ github.event.number }}/merge | cut -f 1 ) | |
for key in $keys; do | |
(gh actions-cache delete $key -R ${{ github.repository }} --confirm && echo "Deleted cache $key") \ | |
|| echo "Cache $key not found" | |
done | |
env: | |
GH_TOKEN: ${{ github.token }} |