skip to Main Content

I want to periodically remove all GitHub caches for my project using a workflow action. While this has been discussed before, the GitHub toolkit has evolved and the proposed solutions seem rather messy and hard to understand.

The project is private and its organization on the free plan, if that’s of relevance.

I figured it should be possible to run gh cache delete --all in the workflow. So I devised the following workflow:

name: Clear all GitHub actions caches
on:
  schedule:
    - cron: "0 4 * * 0"
  workflow_dispatch:

jobs:
  clear-github-caches:
    name: Clear all GitHub action caches
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - run: |
          gh repo list
          gh cache delete --all --repo me/my-project
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

However, when I run it, it fails with:

me/my-project   <description>   private 2023-11-05T17:17:15Z
HTTP 403: Resource not accessible by integration (https://api.github.com/repos/me/my-project/actions/caches?per_page=100)

I also tried to use a local work copy by adding:

      - uses: actions/checkout@v3

but this did not change anything.

Do I need a different permission than "contents: write"? Or am I missing something else?

2

Answers


  1. Chosen as BEST ANSWER

    This is the actual action I ended up with:

    name: Clear all GitHub actions caches
    on:
      schedule:
        - cron: "0 4 * * 0"  # run automatically each Sunday 4:00 UTC
      workflow_dispatch:     # allow to run manually 
    
    jobs:
      clear-github-caches:
        name: Clear all GitHub action caches
        runs-on: ubuntu-latest
        steps:
          - name: Clear all caches
            run: gh cache delete --all --repo me/my-project  # TODO: Use your project.
            env:
              # This token requires the "repo" scope.
              GITHUB_TOKEN: ${{ secrets.ACTION_CLEAR_CACHES_TOKEN }}
    

    Change the argument for --repo to your project.

    To provide the token for this, create a personal access token with the "repo" permission enabled. Then add a GitHub secret named ACTION_CLEAR_CACHES_TOKEN and use the token as content.


  2. The gh cache delete docs mentions:

    Deletion requires authorization with the "repo" scope.

    which means you need to create a PAT (Personal Access Token) for this with repo scope as the default GITHUB_TOKEN doesn’t have it.

    And, you do need the checkout step also (or you’ll have to specify the repo using --repo flag with gh commands).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search