Lesson 2 of 7 Authentication and Access

Managing GitHub Authentication

This lesson explains GitHub Apps, GITHUB_TOKEN and personal access tokens.

Learning outcomes

After completing this lesson, you will be able to:

  • Set GITHUB_TOKEN permissions.
  • Explain when a GitHub App is needed.
  • Distinguish classic from fine-grained tokens.
  • Choose a credential from stated requirements.

GITHUB_TOKEN

At the start of each job, GitHub automatically creates a token for that repository, available as secrets.GITHUB_TOKEN or github.token. It expires when the job finishes, or after a maximum of 24 hours, so it needs no storage or rotation. It can reach only the repository that holds the workflow.

Its default permissions are set at organization and repository level, and the safe default is read-only for contents and packages. A workflow should then narrow or raise permissions explicitly through the permissions key, at workflow or job level. Any permission not listed is set to none, so this approach gives least privilege.

permissions:
  contents: read
  pull-requests: write

jobs:
  label:
    runs-on: ubuntu-latest
    steps:
      - run: gh pr edit "$PR" --add-label reviewed
        env:
          GH_TOKEN: ${{ github.token }}
          PR: ${{ github.event.pull_request.number }}

One behaviour often appears in questions. To prevent loops, events created with GITHUB_TOKEN do not start new workflow runs, with the exception of workflow_dispatch and repository_dispatch. If a workflow pushes a commit and that push must trigger another workflow, a GitHub App token is needed. Workflows triggered from forks receive a read-only token.

True or false

How GITHUB_TOKEN behaves

Exam skills 4.1.2

Check the lifetime, scope and permissions of GITHUB_TOKEN.

Log in to try this activity Interactive activities are free with an account.

GitHub Apps

A GitHub App is a first-class identity, which belongs to an organization and not a person. It declares fine-grained permissions, and it is installed on selected repositories. It signs a JSON Web Token with a private key, then exchanges it for a short-lived installation access token, which lasts for one hour.

This suits automation across several repositories. It survives staff changes, it has its own higher rate limits, and its actions appear under its own name. The actions/create-github-app-token action creates a token inside a workflow, and the private key is stored as a secret.

steps:
  - uses: actions/create-github-app-token@v2
    id: app-token
    with:
      app-id: ${{ vars.APP_ID }}
      private-key: ${{ secrets.APP_PRIVATE_KEY }}
      owner: contoso
      repositories: web,api
  - uses: actions/checkout@v4
    with:
      repository: contoso/api
      token: ${{ steps.app-token.outputs.token }}
Put in order

How a GitHub App gets a token

Exam skills 4.1.2

Arrange how a workflow obtains a GitHub App installation token.

Log in to try this activity Interactive activities are free with an account.

Personal access tokens

A personal access token (PAT) acts as the user who created it. There are two kinds. A classic token has broad scopes, such as repo, which cover all repositories that the user can reach. A fine-grained token is limited to chosen repositories and specific permissions, and it must have an expiry, so it is the better choice when a PAT is unavoidable. Organizations can require approval for fine-grained tokens, and can restrict classic ones.

PATs remain useful for a developer's personal scripts, or for the Git command line. They are a poor choice for shared automation, because they stop working when the person leaves, and because they often hold more access than a job needs.

Sort into groups

Classic or fine-grained PAT?

Exam skills 4.1.2

Decide which kind of GitHub personal access token each statement describes.

Log in to try this activity Interactive activities are free with an account.

Authenticating people

Developers sign in with a password and two-factor authentication, and an organization can make that mandatory. SSH keys or the Git Credential Manager handle Git operations. GitHub Enterprise Cloud can connect to Microsoft Entra ID through SAML single sign-on, and Enterprise Managed Users lets an identity provider control accounts completely. With SAML, a PAT or SSH key must be authorized for the organization before it works.

Choosing a credential

The following table recaps the choice. Start from the narrowest option that works.

GITHUB_TOKEN
IdentityThe workflow
ScopeSame repository
LifetimeThe job
Starts other workflowsNo
Choose when Same repository, in a workflow.
GitHub App
IdentityThe app
ScopeSelected repositories
LifetimeOne hour per token
Starts other workflowsYes
Choose when Cross-repository automation.
Fine-grained PAT
IdentityA user
ScopeSelected repositories
LifetimeSet expiry
Starts other workflowsYes
Choose when A personal script, when no other option exists.

The following diagram shows a GitHub App replacing a leaving engineer's personal access token in a workflow.

Replacing a leaving engineer's PAT with a GitHub App. The app has pull request and contents permissions and is installed on the 30 repositories; the workflow creates a short-lived token with actions/create-github-app-token and opens the pull requests. GITHUB_TOKEN reaches only its own repository.
PAT stands for personal access token.
Scenario questions

Which GitHub credential?

Exam skills 4.1.2

Choose the narrowest GitHub credential that works.

Log in to try this activity Interactive activities are free with an account.
Worked example

Cross-repository automation

A workflow in one repository must open pull requests in 30 other repositories. It currently uses an engineer's classic PAT, and the engineer is leaving. What should you use instead?

Create a GitHub App with permission on pull requests and contents, install it on the 30 repositories, and create a token in the workflow with actions/create-github-app-token. GITHUB_TOKEN reaches only its own repository. A new PAT from another engineer would repeat the same problem, and an account shared by a team would be hard to audit.

Check yourself

A workflow pushes a commit with GITHUB_TOKEN. Why does the push not start another workflow?

Show answer

Events created with GITHUB_TOKEN do not start new runs, which prevents loops. A GitHub App token would start one.

Exam Tips

Remember for the exam

  • GITHUB_TOKEN is per job and limited to its repository.
  • Set permissions explicitly; unlisted permissions become none.
  • GITHUB_TOKEN events do not start new workflow runs, apart from dispatch events.
  • GitHub Apps suit cross-repository automation.
  • Fine-grained PATs are preferred to classic PATs when a PAT is required.