create-github-app-token

repository·main·Indexed 21 days ago

https://github.com/actions/create-github-app-token

GitHub Action for creating a GitHub App Installation Access Token. This allows workflows to perform actions with the identity and specific permissions of a GitHub App rather than a standard user or the default GITHUB_TOKEN. It supports scoping tokens to the current repository, specific owners, multiple repositories, or an entire enterprise installation. Version 3.2.0.

Tokens
3.5K
Snippets
10
Records
15
Agent score
25%

What's inside create-github-app-token

  1. Understand token scoping and target selection

    main

    The scope of the generated token is determined by the combination of inputs provided. The action uses the POST /app/installations/{installation_id}/access_tokens endpoint to create the token.

    Input CombinationToken Scope
    enterprise is setScoped to an enterprise installation (can call enterprise APIs, but no org/repo access)
    owner is set, repositories is emptyAll repositories in the provided owner's installation
    repositories is setOnly the specified repositories
    No target inputs (owner, repositories, or enterprise)Only the current repository

    Important Lifecycle Note: Unless skip-token-revoke is set to true, the token is revoked at the end of the job. This means you cannot pass the token to a different job via outputs unless you disable revocation.

  2. Create tokens for multiple repositories or owners

    main

    You can scope the generated token to specific owners, specific repositories within an owner, or an entire enterprise installation using the following inputs:

    • owner: The account (user or organization) that owns the installation.
    • repositories: A multi-line string or comma-separated list of repository names to scope the token to.
    • enterprise: The slug of the GitHub Enterprise.
    • github-api-url: The API URL for GitHub Enterprise Server (GHES).
  3. Setup Create GitHub App Token

    main

    To use this action, you must first register a GitHub App and configure your repository with the necessary credentials:

    1. Register a new GitHub App via GitHub's documentation.
    2. Store the App's Client ID in your repository variables (e.g., APP_CLIENT_ID).
    3. Store the App's private key in your repository secrets (e.g., APP_PRIVATE_KEY).
    IMPORTANT

    Installation access tokens expire after 1 hour. For long-running processes, consider alternative approaches.

  4. Use app token with `actions/checkout`

    main

    When using the generated token with actions/checkout, you should set persist-credentials: false to ensure the GITHUB_TOKEN is not persisted in the repository's local git configuration, which helps maintain security.

    - uses: actions/create-github-app-token@v3
      id: app-token
      with:
        client-id: ${{ vars.APP_CLIENT_ID }}
        private-key: ${{ secrets.APP_PRIVATE_KEY }}
    - uses: actions/checkout@v6
      with:
        token: ${{ steps.app-token.outputs.token }}
        ref: ${{ github.head_ref }}
        persist-credentials: false
  5. Configure Git CLI for an app's bot user

    main

    To allow Git commands like commit and push to appear as being performed by the GitHub App bot, you must:

    1. Set the permission-contents to write.
    2. Retrieve the App's numeric User ID using the GitHub CLI (gh api).
    3. Configure the global Git user name and email using the app-slug[bot] format.
    4. Run gh auth setup-git using the generated token.
    - uses: actions/create-github-app-token@v3
      id: app-token
      with:
        client-id: ${{ vars.APP_CLIENT_ID }}
        private-key: ${{ secrets.APP_PRIVATE_KEY }}
        permission-contents: write
    - name: Get GitHub App User ID
      id: get-user-id
      run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
      env:
        GH_TOKEN: ${{ steps.app-token.outputs.token }}
    - run: |
        git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
        git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
        gh auth setup-git
      env:
        GH_TOKEN: ${{ steps.app-token.outputs.token }}
    - run: |
        git add .
        git commit -m "Auto-generated changes"
        git push
  6. Resolve installation targets for GitHub App tokens

    main

    The action supports three distinct installation targets to scope the generated token:

    1. Enterprise: Scopes the token to a GitHub Enterprise installation. Requires the enterprise input. Cannot be used with owner or repositories.
    2. Owner: Scopes the token to all repositories within a specific user or organization installation. Requires the owner input and an empty repositories list.
    3. Repository: Scopes the token to specific repositories.
      • If owner and repositories are provided, it targets those specific repositories.
      • If only repositories are provided, it uses the GITHUB_REPOSITORY_OWNER environment variable as the owner.
      • If neither owner nor repositories are provided, it defaults to the current repository defined by GITHUB_REPOSITORY.

    Repository Input Format: Repositories can be provided as a simple name (repo-name) or as a full path (owner/repo-name). If a full path is used, the owner must match the resolved owner input.

  7. Configure Proxy support

    main

    This action uses Node.js native proxy support. To ensure HTTP_PROXY or HTTPS_PROXY environment variables are honored, you must set NODE_USE_ENV_PROXY: "1" on the action step. You can also use NO_PROXY for bypass rules.

    - uses: actions/create-github-app-token@v3
      id: app-token
      env:
        HTTPS_PROXY: http://proxy.example.com:8080
        NO_PROXY: github.example.com
        NODE_USE_ENV_PROXY: "1"
      with:
        client-id: ${{ vars.APP_CLIENT_ID }}
        private-key: ${{ secrets.APP_PRIVATE_KEY }}
  8. Decode a Base64 encoded private key for the action

    main

    If your GitHub App private key is stored as a Base64 encoded secret, you must decode it before passing it to the private-key input. Use the following pattern to decode the key, mask it to prevent accidental logging, and pass it to the action via $GITHUB_OUTPUT.

    steps:
      - name: Decode the GitHub App Private Key
        id: decode
        run: |
          private_key=$(echo "${{ secrets.APP_PRIVATE_KEY }}" | base64 -d | awk 'BEGIN {ORS="\\n"} {print}' | head -c -2) &> /dev/null
          echo "::add-mask::$private_key"
          echo "private-key=$private_key" >> "$GITHUB_OUTPUT"
      - name: Generate GitHub App Token
        id: app-token
        uses: actions/create-github-app-token@v3
        with:
          client-id: ${{ vars.APP_CLIENT_ID }}
          private-key: ${{ steps.decode.outputs.private-key }}
  9. Create a token for the current repository

    main

    Use this configuration to generate a token scoped to the repository where the workflow is running. The token is available via the app-token step output.

    - uses: actions/create-github-app-token@v3
      id: app-token
      with:
        client-id: ${{ vars.APP_CLIENT_ID }}
        private-key: ${{ secrets.APP_PRIVATE_KEY }}
    name: Run tests on staging
    on:
      push:
        branches:
          - main
    
    jobs:
      hello-world:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/create-github-app-token@v3
            id: app-token
            with:
              client-id: ${{ vars.APP_CLIENT_ID }}
              private-key: ${{ secrets.APP_PRIVATE_KEY }}
          - uses: ./actions/staging-tests
            with:
              token: ${{ steps.app-token.outputs.token }}
  10. Create a token with specific permissions

    main

    You can request specific permissions for the generated token by using the permission-<scope> input (e.g., permission-issues: write).

    NOTE

    The selected permissions must already be granted to the GitHub App installation. If you request a permission that the installation does not have, the action will return an error.

    - uses: actions/create-github-app-token@v3
      id: app-token
      with:
        client-id: ${{ vars.APP_CLIENT_ID }}
        private-key: ${{ secrets.APP_PRIVATE_KEY }}
        owner: ${{ github.repository_owner }}
        permission-issues: write
  11. Access outputs from create-github-app-token

    main

    The action provides the following outputs which can be used in subsequent steps of your workflow:

    • token: The generated GitHub App installation access token.
    • installation-id: The GitHub App installation ID.
    • app-slug: The slug of the GitHub App.
  12. Configure inputs for create-github-app-token

    main

    The actions/create-github-app-token action requires specific inputs to generate an installation access token.

    Required Inputs

    • client-id (or legacy app-id): The GitHub App Client ID.
    • private-key: The GitHub App private key. The action automatically replaces escaped newlines (\n) with actual newlines.

    Optional Inputs

    • owner: The owner of the GitHub App installation. Defaults to the current repository owner.
    • repositories: A comma or newline-separated list of repositories (e.g., owner/repo1).
    • enterprise: The slug of the enterprise account. Note: This is mutually exclusive with owner and repositories.
    • permission-<permission name>: Explicitly grant specific permissions (e.g., permission-pull-requests). By default, the token inherits all installation permissions.
    • skip-token-revoke: If true, the token is not revoked when the job completes. If false (default), the token is revoked in the post step and cannot be passed to other jobs.
    • github-api-url: The URL of the GitHub REST API. Defaults to the API URL of the environment where the workflow is running.
    # Example of input structure
    with:
      client-id: ${{ vars.APP_CLIENT_ID }}
      private-key: ${{ secrets.APP_PRIVATE_KEY }}
      owner: 'my-org'
      repositories: 'my-org/repo1, my-org/repo2'
      permission-pull-requests: write