GitHub Actions Cache Permissions: Safe Setup
In this article
Setting GitHub Actions cache permissions safely means giving your workflow just enough access to read and write the cache without exposing secrets or allowing unauthorized modifications. By default, the GITHUB_TOKEN has read-only cache permissions, but if you need to write to the cache (for example, to persist dependencies), you must explicitly grant write access. This guide walks you through configuring those permissions correctly, whether you're using the actions/cache action or direct cache APIs, and how to avoid common pitfalls that lead to security vulnerabilities or cache corruption.
What You'll Need
- A GitHub repository with GitHub Actions enabled (free for public repos, included in plans for private repos).
- Basic familiarity with YAML syntax and your workflow file (e.g.,
.github/workflows/*.yml). - Access to the repository settings if you need to manage environment or secret configurations (though not strictly required for basic cache permissions).
Step-by-Step
- Identify your cache needs. Determine if your workflow only reads the cache (e.g., restoring dependencies) or also writes to it (e.g., saving new dependencies after a build). Reading is allowed by default; writing requires explicit permission.
- Open your workflow file. Navigate to your repository, select the Actions tab, choose the workflow you want to edit, and click the pencil icon to edit the
.ymlfile. Alternatively, edit the file directly in your local clone and push changes. - Locate the
permissionsblock. If your workflow already has a top-levelpermissions:key, you'll modify it. If not, add it at the top level, right after theon:trigger section. Example:
Thepermissions: contents: read actions: writeactions: writepermission is what grants write access to the cache (and other actions-related resources). If you only need read access, you can omit theactionskey or set it toread. - Set the minimal permissions. For security, always grant the least privilege. If your workflow doesn't need to write to the cache, set
actions: reador omit theactionskey entirely. If you need write access, setactions: writebut keep other permissions (likecontents) as read-only unless absolutely necessary. - Apply permissions to specific jobs (optional). You can override top-level permissions per job by adding a permissions: block inside a job definition. This is useful if only one job needs cache write access. Example: jobs: build: permissions: actions: write steps: - uses: actions/cache@v3 with: path: ~/.npm This overrides the top-level permissions for that job only.
- Use the
actions/cacheaction correctly. When usingactions/cache, ensure you set thepathandkeyappropriately. The action automatically uses the token's permissions, so as long as you've setactions: write, it can save the cache. For read-only scenarios, you can use therestore-keysto partially match caches. - Test your workflow. Push the changes to your repository and run the workflow. Check the Actions tab to see if the cache step succeeds. If you get a 403 error, it means the token lacks write permission.
- Review security implications. Granting
actions: writealso allows the token to modify other actions-related resources, such as cancelling workflows or creating workflow dispatches. Be cautious if your workflow runs untrusted code (e.g., from pull requests) — consider usingpull_request_targetwith care, or limit permissions to read-only for those events.
Common Mistakes
- Setting
actions: writeglobally when not needed. This expands the token's power unnecessarily, increasing risk if a step is compromised. - Forgetting to set
permissionsat all. If you rely on the default, you get read-only cache access, and any write attempt fails with a 403 error. - Using
GITHUB_TOKENin a custom step to write to cache. The token only works with the official cache action or the cache API; you cannot directly write to the cache filesystem. - Overriding permissions incorrectly. If you set a job-level
permissions, it completely replaces the top-level permissions for that job — it does not merge. So if you need bothcontents: readandactions: write, you must specify both. - Using
actions/cachewith a key that changes too often. This can cause cache misses, but it's not a security issue — just inefficiency.
Tips
- Always use the latest version of
actions/cache(e.g.,@v4as of this writing) to get security fixes and improvements. - For pull request workflows that need cache writes, consider using a separate workflow triggered by
pushto populate the cache, and have the PR workflow only read it. This avoids giving write permissions to potentially untrusted code. - Use
restore-keysto fall back to older caches if the exact key doesn't match, reducing build time without needing write access. - If you're using a self-hosted runner, cache permissions work the same, but ensure the runner's token is scoped correctly.
- Regularly audit your workflow permissions by reviewing the
permissionsblock in each workflow file.
Troubleshooting
- 403 error when saving cache. This means the token lacks
actions: write. Add the permission as described in Step 3. - Cache not being restored. Check the cache key. If the key doesn't match exactly, and you have no
restore-keys, it won't restore. Also verify that the cache was actually saved in a previous run. - Cache size exceeds limits. GitHub has a 10 GB limit per repository. If you exceed it, old caches are evicted. You can't change this, but you can reduce cache size by only caching essential files.
- Permission denied even after setting
actions: write. Ensure you placed thepermissionsblock at the correct level (top-level or job-level) and that you didn't accidentally override it later in the job. - Workflow fails with 'Resource not accessible by integration'. This is a common error when the token lacks permissions. Double-check your YAML indentation — the
permissionskey must be at the same indentation level asjobs.
Frequently Asked Questions
Do I need to set cache permissions if I only use actions/cache for restoring?
No, restoring only requires read access, which is the default for GITHUB_TOKEN. You only need to set actions: write when you want to save a new cache entry.
Can I set cache permissions per branch or environment?
Permissions are set per workflow run, not per branch. However, you can use conditional logic in your workflow to set different permissions based on the branch (e.g., only allow writes on the main branch).
What is the difference between actions: read and actions: write for cache?
actions: read allows the token to read cache entries and view workflow run metadata. actions: write additionally allows creating and updating cache entries. It also grants broader actions API permissions, so use it sparingly.
Is it safe to use actions: write on pull_request workflows?
It's generally safe if the workflow only runs trusted code (e.g., from maintainers). But for pull requests from forks, the token is read-only by default, and you cannot grant write permissions without using pull_request_target, which carries risks. Prefer read-only for fork PRs.
Can I use a personal access token (PAT) instead of GITHUB_TOKEN for cache?
Yes, but it's not recommended because PATs are broader in scope and harder to rotate. The official actions/cache action works with GITHUB_TOKEN, and using a PAT would require passing it as a secret, which is less secure.
Why does my cache save but not restore in the next run?
This usually happens because the cache key changes (e.g., due to a different lockfile hash). Check the key in the restore step and ensure it matches the key used when saving. Also verify that the cache wasn't evicted due to size limits.