actions/checkout

repository·main·Indexed 27 days ago

https://github.com/actions/checkout

A fundamental GitHub Action used to check out a repository into the runner's workspace. It supports sparse checkouts, submodule handling, and authentication via Personal Access Tokens (PAT) or SSH. Version 7 includes security updates to prevent 'pwn request' vulnerabilities by refusing to check out fork pull request code when triggered by pull_request_target or workflow_run by default.

Tokens
3.3K
Snippets
11
Records
17
Agent score
44%

What's inside actions/checkout

  1. Manage Git credentials and authentication

    main

    By default, actions/checkout@v2 persists credentials (PAT or SSH key) in the local git config. This allows you to run authenticated git commands (like git fetch or git commit) in subsequent workflow steps.

    Using PATs

    When using a PAT, the token is stored in the local git config via http.https://github.com/.extraheader. This enables an AUTHORIZATION: basic <BASE64_U:P> header for all authenticated commands.

    Using SSH Keys

    When using an ssh-key, the key is written to $RUNNER_TEMP and the core.sshCommand is configured to use it. The key and the temporary host key database are removed by the action's post-job hook.

    Opting out

    To prevent credentials from being stored on disk, set persist-credentials: false.

    Git Commit Configuration

    If you are scripting git commit, you must manually set your identity as the action does not provide defaults:

    git config user.name <NAME>
    git config user.email <EMAIL>
  2. Use actions/checkout@v7

    main

    The actions/checkout@v7 action checks out your repository under $GITHUB_WORKSPACE.

    Key Security Update in v7: By default, v7 refuses to check out fork pull request code when triggered by pull_request_target or workflow_run to prevent "pwn request" vulnerabilities. To explicitly allow this, set allow-unsafe-pr-checkout: true after reviewing the security risks.

    - uses: actions/checkout@v7
  3. Push a commit to a PR using the built-in token

    main

    When working within a pull_request trigger, GitHub Actions checks out in detached HEAD mode. To push changes back to the pull request branch, you must explicitly provide the ref input (e.g., using ${{ github.head_ref }}).

    Note: The following account information is for GitHub.com and will not work on GitHub Enterprise Server (GHES).

    on: pull_request
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v7
            with:
              ref: ${{ github.head_ref }}
          - run: |
              date > generated.txt
              # Note: the following account information will not work on GHES
              git config user.name "github-actions[bot]"
              git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
              git add .
              git commit -m "generated"
              git push
  4. Push a commit using the built-in token

    main

    You can use the built-in GITHUB_TOKEN to push commits back to the repository. When doing so, you must configure the git user identity.

    Note: The following account information is for GitHub.com and will not work on GitHub Enterprise Server (GHES). The user email format is {user.id}+{user.login}@users.noreply.github.com.

    on: push
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v7
          - run: |
              date > generated.txt
              # Note: the following account information will not work on GHES
              git config user.name "github-actions[bot]"
              git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
              git add .
              git commit -m "generated"
              git push
  5. Checkout pull request HEAD commit instead of merge commit

    main

    By default, pull_request events check out a merge commit. To check out the actual HEAD commit of the pull request instead, set the ref input to ${{ github.event.pull_request.head.sha }}.

    - uses: actions/checkout@v7
      with:
        ref: ${{ github.event.pull_request.head.sha }}
  6. Checkout pull request on closed event

    main

    To ensure the action runs when a pull request is closed, include the closed type in your pull_request trigger configuration.

    on:
      pull_request:
        branches: [main]
        types: [opened, synchronize, closed]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v7
  7. Configure recommended GITHUB_TOKEN permissions

    main

    To ensure the checkout action functions correctly using the built-in GITHUB_TOKEN, it is recommended to set the contents permission to read. This is required unless you provide alternative authentication via the token or ssh-key inputs.

    permissions:
      contents: read
  8. Fetch only specific files using sparse-checkout

    main

    You can use sparse-checkout to limit the files downloaded to reduce checkout time and disk usage.

    # Fetch only the root files
    - uses: actions/checkout@v7
      with:
        sparse-checkout: .
    
    # Fetch only the root files and .github and src folder
    - uses: actions/checkout@v7
      with:
        sparse-checkout: |
          .github
          src
    
    # Fetch only a single file
    - uses: actions/checkout@v7
      with:
        sparse-checkout: |
          README.md
        sparse-checkout-cone-mode: false
  9. Checkout a specific branch or HEAD^

    main

    Use the ref input to checkout a specific branch, tag, or SHA. To check out a specific parent commit (like HEAD^), use fetch-depth to ensure enough history is available, then use a standard git checkout command.

    # Checkout a different branch
    - uses: actions/checkout@v7
      with:
        ref: my-branch
    
    # Checkout HEAD^
    - uses: actions/checkout@v7
      with:
        fetch-depth: 2
    - run: git checkout HEAD^
  10. Configure checkout paths for multiple repositories

    main

    In v2, all checkouts must be located under $GITHUB_WORKSPACE. If you are checking out multiple repositories, you must use the path input to avoid collisions.

    Nested Layout

    To check out a second repository into a subdirectory of the workspace:

    # Self repo - Checkout to $GITHUB_WORKSPACE
    - uses: actions/checkout@v2
    
    # Other repo - Checkout to $GITHUB_WORKSPACE/myscripts
    - uses: actions/checkout@v2
      with:
        repository: myorg/myscripts
        path: myscripts

    Side-by-Side Layout

    To check out multiple repositories into separate directories at the root of the workspace:

    # Self repo - Checkout to $GITHUB_WORKSPACE/foo
    - uses: actions/checkout@v2
      with:
        path: foo
    
    # Other repo - Checkout to $GITHUB_WORKSPACE/myscripts
    - uses: actions/checkout@v2
      with:
        repository: myorg/myscripts
        path: myscripts