Fixing "Resource not available" in GitHub Actions
Published on May 5, 2023
The "Resource not available" error occurs when you try to use a GitHub Token for some GitHub API call, but the call fails because the token does not have the necessary permissions.
Let's walk through the real-world scenario which inspired this post. A default token is available to each GitHub Action as ${{ github.token }}
. I tried to use that token to delete my action's cache, like so:
1 - name: Clean up the cache2 env:3 GH_TOKEN: ${{ github.token }}4 run: |5 gh extension install actions/gh-actions-cache6 gh actions-cache delete --confirm $CACHE_KEY
That attempt failed with the "Resource not available" error because the token does have the permission to delete the cache by default. I fixed the problem by adding the required permission to the job's definition:
1jobs:2 my-custom-job:3 permissions:4 actions: write # used to clean up the cache
If you run into that problem, the solution is to define which permissions your token needs and add them, as done above.
That being said, how would you figure out which permissions are needed for your scenario? Unfortunately, I do not have a precise procedure for that. I usually try to infer the required permissions by picking apart the API URLs used in the action. As an example, in this case I searched through the GitHub Rest API documentation for the API URL used to delete the cache, which is:
I noticed that the[DELETE] https://api.github.com/repos/$OWNER/$REPO
/actions/caches?key=$CACHE_KEY
/caches
endpoint is nested under the /actions
route. There is an actions
permission, so I narrowed to that one. Further, since it is necessary to write to the cache, I thought I probably needed the actions: write
permission. The error indeed went away after that permission was added, so problem solved.