🎓 A1Learner
🔍
0 of 50 lessons completed

GitHub Actions: Permissions and GITHUB_TOKEN

GITHUB_TOKEN is the automatic token that GitHub gives to each workflow run. A permissions block sets what that token is allowed to do.

Least privilege in one block

This job only reads the code, so contents read is all it needs.

permissions:
  contents: read

A write permission, only when needed

A step that writes a file back to the repo needs contents write.

permissions:
  contents: write

Check the token is present

Pass the token into the step as an environment variable. Never print the token itself.

- run: test -n "$GITHUB_TOKEN" && echo "Token is present."
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Example

name: Permissions demo
on:
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: '24'
      - run: npm test
      - run: test -n "$GITHUB_TOKEN" && echo "Token is present."
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Key points

  • Use a permissions block to give the token only the access the job needs.
  • Least privilege means the smallest access that still lets the job work.
  • The token is created fresh for each run, and it expires when the run ends.
  • The token can read and write only inside its own repository by default.
  • A push made with GITHUB_TOKEN starts no new workflow run.
  • The token cannot reach other repositories or private data outside the repo.

In this example, the permissions block gives the token contents read only. The job checks out the code and sets up Node 24, then runs the tests. The last step checks that GITHUB_TOKEN is set, without printing the token. A push made with this token would start no new run, so a workflow cannot trigger itself in a loop.

Note

The default token access is read only for most jobs. Add a write permission only when a step truly needs it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

A job only reads code and runs tests. Which permissions block fits it?