gh-pages

repository·main·Indexed 25 days ago

https://github.com/tschaub/gh-pages

A utility for publishing files to a gh-pages branch on GitHub or any other Git remote. It automates cloning, committing, and pushing build artifacts via a Node.js API or CLI. Version 6.3.0 requires Git >= 1.9 and Node > 14.

Tokens
3.1K
Snippets
7
Records
18
Agent score
84%

What's inside gh-pages

  1. Deploy to GitHub Pages using GitHub Actions

    main

    To deploy via GitHub Actions, you must set the git remote URL using the GITHUB_TOKEN and define a user with the -u flag. The GITHUB_TOKEN and GITHUB_REPOSITORY variables are provided automatically by GitHub Actions and should be passed to the environment.

    If you are using a named script from package.json (e.g., npm run deploy), you must use the -- separator to pass arguments through to the underlying gh-pages command.

    # Example: Direct deployment
    - name: Deploy with gh-pages
      run: |
        git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
        npx gh-pages -d build -u "github-actions-bot <support+actions@github.com>"
       env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    
    # Example: Deployment via npm script (e.g., "deploy": "gh-pages -d build")
    - name: Deploy with gh-pages
      run: |
        git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
        npm run deploy -- -u "github-actions-bot <support+actions@github.com>"
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  2. Configure asset paths for GitHub Pages Project Sites

    main

    When hosting on a GitHub Project Site (e.g., username.github.io/repo-name/), assets may fail to load with 404 errors if paths are absolute. You must configure your build tool to use relative paths or a specific base path:

    • Create React App: Set the "homepage" property in package.json.
    • Vite: Set the "base" property in vite.config.js.
    • Next.js: Set the "basePath" property in next.config.js.
  3. Resolve `branch already exists` error

    main

    If you encounter the error fatal: A branch named 'gh-pages' already exists., it is often due to a corrupted or existing cache. You can resolve this by manually cleaning the cache directory.

    You can either run the provided clean script or manually delete the directory:

  4. Use the `publish` API

    main

    The publish function creates a temporary clone of the current repository, creates a branch (defaulting to gh-pages), copies files from the specified directory, commits them, and pushes to the remote.

    Warning: By default, any files in the target branch that are not in the source directory will be removed. To prevent this, use the add: true option.

  5. Configure ESLint using eslint-config-tschaub

    main

    To use the pre-defined ESLint configurations from this project, import configs from eslint-config-tschaub in your eslint.config.mjs file and spread them into the default export array.

    import configs from 'eslint-config-tschaub';
    
    export default [...configs];
  6. Configure `publish` options

    main

    The publish method accepts an optional options object to customize the deployment behavior:

    OptionTypeDefaultDescription
    srcstring|Array<string>'**/*'Minimatch pattern(s) to select files to publish.
    branchstring'gh-pages'The name of the branch to push to. Use -b | --branch in CLI.
    deststring'.'The destination folder within the target branch.
    dotfilesbooleanfalseIf true, includes files starting with . (dotfiles).
    nojekyllbooleanfalseIf true, writes a .nojekyll file to bypass Jekyll on GitHub Pages.
    cnamestringundefinedWrites a CNAME file with the provided custom domain name.
    addbooleanfalseIf true, only adds files and never removes existing files in the target branch.
    repostringorigin URLThe repository URL to push to. Use -r | --repo in CLI.
    remotestring'origin'The name of the remote to push to.
    tagstring''Creates a git tag with this name after committing.
    messagestring'Updates'The commit message used for the changes.
    userObjectnullObject with { name, email } for git commits if not configured globally.
    removestring'**/*'Pattern of files to remove in the target branch (ignored if add: true is set).
    pushbooleantrueIf false, only commits changes without pushing to remote.
    historybooleantrueIf false, pushes a force new commit without parent history.
    silentbooleanfalseIf true, avoids showing repository URLs in error messages.
    beforeAddfunctionnullAsync callback executed right before git add. Receives git object.
    gitstring'git'Path to the git executable.
  7. Use the `gh-pages` CLI

    main

    The gh-pages package provides a command line utility. You can use it directly or via npm scripts.

    To deploy a dist folder using an npm script, add the following to your package.json:

    "scripts": {
      "deploy": "gh-pages -d dist"
    }

    Then run:

    npm run deploy