AI Hints
Home›CI/CD with GitHub Actions Tutorial›Secrets, Tokens and Outputs
CI/CD with GitHub Actions · Lesson 41 of 138

GITHUB_TOKEN and Permissions

A key for every run.

⏱ 2 min read▶ Video at 47:06🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Every workflow run gets a temporary token, called GITHUB token, created automatically. It lets the workflow talk to GitHub, to comment on a pull request, create a release, or push a package. It expires when the job ends.

GITHUB_TOKEN and Permissions
From the video: GITHUB_TOKEN and Permissions
  • A token for every run
  • Created automatically
  • Expires when the job ends

Setting permissions

The permissions key decides what the token is allowed to do. Here, the workflow can read the code, and write comments on pull requests, but nothing else. Setting permissions at the top keeps the token as weak as possible. You use it like a secret, with secrets dot GITHUB token.

.github/workflows/comment.ymlyaml
name: Comment
on: pull_request
permissions:
  contents: read
  pull-requests: write
jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
      - env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh pr comment ${{ github.event.number }} --body "Tests passed!"

Common permissions

These are the permissions you'll set most often. Contents covers the code and releases. Pull requests covers comments and labels. Packages covers container images. And i d token is for logging in to cloud providers, which we'll see later. Each can be read, write, or none.

PermissionAllows
contentsread code, create releases
pull-requestscomment, label PRs
packagespush container images
id-tokenlog in to clouds (OIDC)

Summary

GITHUB token is a short lived key for each run. The permissions key limits what it can do. Keep it as small as possible. Next, passing values between steps.

Key takeaways
  • secrets.GITHUB_TOKEN
  • permissions:
  • Smallest possible
Next up: Step Outputs — passing values on