├── .github └── workflows │ ├── ci.yml │ └── main-version-branch.yml ├── .gitignore ├── LICENSE ├── README.md └── action.yml /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: push 4 | 5 | jobs: 6 | ci: 7 | name: Use this action for check 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Check out this repository 11 | uses: actions/checkout@v3 12 | - name: Use this action 13 | uses: ./ 14 | -------------------------------------------------------------------------------- /.github/workflows/main-version-branch.yml: -------------------------------------------------------------------------------- 1 | name: Main Version Branch 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | main-version-branch: 9 | name: Create main version branch 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Check out 13 | uses: actions/checkout@v3 14 | - name: Push HEAD to main version branch 15 | run: | 16 | if [[ '${{ github.event.release.tag_name }}' =~ ^(v[0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then 17 | git push --force origin "HEAD:refs/heads/${BASH_REMATCH[1]}" 18 | fi 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Hiroyuki Kusu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Delete Branch Cache Action 2 | 3 | [![CI](https://github.com/snnaplab/delete-branch-cache-action/actions/workflows/ci.yml/badge.svg)](https://github.com/snnaplab/delete-branch-cache-action/actions/workflows/ci.yml) 4 | 5 | Deletes GitHub Actions caches for a branch. 6 | 7 | Besides if you want to clear the caches for some reason, you can save cache space by deleting the caches you no longer need immediately, because [total size of all caches in a repository is limited to 10 GB](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy). 8 | 9 | ## Inputs 10 | 11 | | Name | Description | Default | Required | 12 | | --- | --- | --- | --- | 13 | | `repository` | Target repository name with owner, eg `snnaplab/delete-branch-cache-action`. Specify when deleting the cache of a repository different from the workflow. | `${{ github.repository }}` | false | 14 | | `ref` | Git reference of target branch in `refs/heads/` format. To reference a pull request use `refs/pull//merge` format. | `${{ github.ref }}` | false | 15 | | `key` | Delete caches with keys that prefix match the key specified here. If not specified, all caches of the branch specified by `ref` will be deleted. || false | 16 | | `github-token` | Specify a personal access token (PAT) when targeting a repository different from the workflow. | `${{ github.token }}` | false | 17 | 18 | ## Example 19 | 20 | ### Remove unused caches when closing a pull request 21 | 22 | Caches created in the HEAD branch of a pull request are no longer used, so delete them immediately to save repository cache space. 23 | 24 | ```yaml 25 | name: Delete Unused Caches 26 | 27 | on: 28 | pull_request: 29 | types: [closed] 30 | 31 | jobs: 32 | delete: 33 | runs-on: ubuntu-latest 34 | steps: 35 | - uses: snnaplab/delete-branch-cache-action@v1 36 | with: 37 | # Specify explicitly because the ref at the time of merging will be a branch name such as 'main', 'develop' 38 | ref: refs/pull/${{ github.event.number }}/merge 39 | ``` 40 | 41 | ## License 42 | 43 | [MIT](LICENSE) 44 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Delete Branch Cache Action' 2 | description: 'GitHub Actions to delete branch caches.' 3 | author: 'Hiroyuki Kusu' 4 | branding: 5 | icon: 'trash-2' 6 | color: 'yellow' 7 | inputs: 8 | repository: 9 | description: '' 10 | required: false 11 | default: ${{ github.repository }} 12 | ref: 13 | description: '' 14 | required: false 15 | default: ${{ github.ref }} 16 | key: 17 | description: '' 18 | required: false 19 | default: '' 20 | github-token: 21 | description: '' 22 | required: false 23 | default: ${{ github.token }} 24 | runs: 25 | using: 'composite' 26 | steps: 27 | - name: Delete branch caches 28 | shell: bash 29 | env: 30 | REPOSITORY: ${{ inputs.repository }} 31 | REF: ${{ inputs.ref }} 32 | KEY: ${{ inputs.key }} 33 | GITHUB_TOKEN: ${{ inputs.github-token }} 34 | run: | 35 | set -eu 36 | 37 | page=1 38 | per_page=100 39 | del_count=0 40 | 41 | while true 42 | do 43 | # Do not use query strings as it is necessary to URL encode if the key contains spaces 44 | res=$(curl -f \ 45 | -H "Accept: application/vnd.github+json" \ 46 | -H "Authorization: token $GITHUB_TOKEN" \ 47 | "https://api.github.com/repos/$REPOSITORY/actions/caches?per_page=$per_page&page=$page") 48 | 49 | res_count=$(echo $res | tr -d '[:cntrl:]' | jq '.actions_caches | length') 50 | if [ $res_count -eq 0 ]; then break; fi 51 | 52 | targets=$(echo $res | tr -d '[:cntrl:]' | jq --arg ref "$REF" --arg key "$KEY" '.actions_caches[] | select(.ref == $ref) | select(.key | startswith($key))') 53 | echo "$targets" 54 | 55 | for id in $(echo $targets | jq '.id') 56 | do 57 | curl -f \ 58 | -X DELETE \ 59 | -H "Accept: application/vnd.github+json" \ 60 | -H "Authorization: token $GITHUB_TOKEN" \ 61 | "https://api.github.com/repos/$REPOSITORY/actions/caches/$id" 62 | ((del_count+=1)) 63 | done 64 | 65 | ((page+=1)) 66 | done 67 | 68 | echo "Delete $del_count caches." 69 | --------------------------------------------------------------------------------