• Stars
    star
    336
  • Rank 125,564 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Publish to GitHub pages with Grunt

grunt-gh-pages

Publish to GitHub Pages with Grunt

NPM version Build Status Dependency Status devDependency Status

Use Grunt to push to your gh-pages branch hosted on GitHub or any other branch anywhere else.

Getting Started

This plugin requires Grunt >=0.4.0 and Git >=1.7.6.

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a gruntfile.js as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-gh-pages --save-dev

Once the plugin has been installed, it may be enabled inside your gruntfile.js with this line:

grunt.loadNpmTasks('grunt-gh-pages');

The gh-pages task

Overview

In your project's Gruntfile, add a section named gh-pages to the data object passed into initConfig.

grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'dist'
    },
    src: ['**']
  }
});

Running this task with grunt gh-pages will create a temporary clone of the current repository, create a gh-pages branch if one doesn't already exist, copy over all files from the dist directory that match patterns from thesrc configuration, commit all changes, and push to the origin remote.

If a gh-pages branch already exists, it will be updated with all commits from the remote before adding any commits from the provided src files.

Note that any files in the gh-pages branch that are not in the src files will be removed. See the add option if you don't want any of the existing files removed.

The gh-pages task is a multi-task, so different targets can be configured with different src files and options. For example, to have the gh-pages:gh-pages target push to gh-pages and a second gh-pages:foo target push to a bar branch, the multi-task could be configured as follows:

grunt.initConfig({
  'gh-pages': {
    options: {
      // Options for all targets go here.
    },
    'gh-pages': {
      options: {
        base: 'build'
      },
      // These files will get pushed to the `gh-pages` branch (the default).
      src: ['index.html']
    },
    'foo': {
      options: {
        base: 'bar-build',
        branch: 'bar'
      },
      // These files will get pushed to the `bar` branch.
      src: ['other.txt']
    }
  }
});

Options

The default task options work for simple cases. The options described below let you push to alternate branches, customize your commit messages, and more.

Options for all targets can be configured on the task level. Individual tasks can also have their own options that override task level options.

All options can be overriden with command line flags. The pattern to provide an option is like --gh-pages-optname foo where optname is the option name and foo is the option value. For example, to supply the tag and message, the task could be run as follows:

grunt gh-pages --gh-pages-tag 'v1.2.3' --gh-pages-message 'Tagging v1.2.3'

options.base

  • type: string
  • default: process.cwd()

The base directory for all source files (those listed in the src config property). By default, source files are assumed to be relative to the current working directory, and they will be copied to the target with this relative path. If your source files are all in a different directory (say, build), and you want them to be copied with a path relative to that directory, provide the directory path in the base option (e.g. base: 'build').

Example use of the base option:

/**
 * Given the following directory structure:
 *
 *   build/
 *     index.html
 *     js/
 *       site.js
 *
 * The task below will create a `gh-pages` branch that looks like this:
 *
 *   index.html
 *   js/
 *     site.js
 *
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build'
    },
    src: '**/*'
  }
});

options.dotfiles

  • type: boolean
  • default: false

Include dotfiles. By default, files starting with . are ignored unless they are explicitly provided in the src array. If you want to also include dotfiles that otherwise match your src patterns, set dotfiles: true in your options.

Example use of the dotfiles option:

/**
 * The task below will push dotfiles (directories and files)
 * that otherwise match the `src` pattern.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'dist',
      dotfiles: true
    },
    src: '**/*'
  }
});

options.add

  • type: boolean
  • default: false

Only add, and never remove existing files. By default, existing files in the target branch are removed before adding the ones from your src config. If you want the task to add new src files but leave existing ones untouched, set add: true in your target options.

Example use of the add option:

/**
 * The task below will only add files to the `gh-pages` branch, never removing
 * any existing files (even if they don't exist in the `src` config).
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      add: true
    },
    src: '**/*'
  }
});

options.only

  • type: string or array of strings
  • default: '.'

When options.add is false, you may specify a filter to select the files to remove, instead of removing all files.

Example of the only option:

/**
 * The task below will only remove the index.html and .js files from the
 * `gh-pages` branch before copying over files from the `src`.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      only: ['index.html', '**/*.js']
    },
    src: '**/*'
  }
});

/**
 * The task below will only remove all files except the README.md from the
 * `gh-pages` branch before copying over files from the `src`.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      only: ['**/*', '!README.md']
    },
    src: '**/*'
  }
});

options.repo

  • type: string
  • default: url for the origin remote of the current dir (assumes a git repository)

By default, the gh-pages task assumes that the current working directory is a git repository, and that you want to push changes to the origin remote. This is the most common case - your gruntfile.js builds static resources and the gh-pages task pushes them to a remote.

If instead your gruntfile.js is not in a git repository, or if you want to push to another repository, you can provide the repository URL in the repo option.

Example use of the repo option:

/**
 * If the current directory is not a clone of the repository you want to work
 * with, set the URL for the repository in the `repo` option.  This task will
 * push all files in the `src` config to the `gh-pages` branch of the `repo`.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      repo: 'https://example.com/other/repo.git'
    },
    src: '**/*'
  }
});

options.branch

  • type: string
  • default: 'gh-pages'

The name of the branch you'll be pushing to. The default uses GitHub's gh-pages branch, but this same task can be used to push to any branch on any remote.

Example use of the branch option:

/**
 * This task pushes to the `master` branch of the configured `repo`.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      branch: 'master',
      repo: 'https://example.com/other/repo.git'
    },
    src: '**/*'
  }
});

options.tag

  • type: string
  • default: ''

Create a tag after committing changes on the target branch. By default, no tag is created. To create a tag, provide the tag name as the option value.

Example use of the tag option from the command line:

grunt gh-pages --gh-pages-tag 'v3.2.1'

options.message

  • type: string
  • default: 'Updates'

The commit message for all commits.

Example use of the message option:

/**
 * This adds commits with a custom message.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      message: 'Auto-generated commit'
    },
    src: '**/*'
  }
});

Alternatively, this option can be set on the command line:

grunt gh-pages --gh-pages-message 'Making commits'

options.user

  • type: Object
  • default: null

If you are running the gh-pages task in a repository without a user.name or user.email git config properties (or on a machine without these global config properties), you must provide user info before git allows you to commit. The options.user object accepts name and email string values to identify the committer.

Example use of the user option:

grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      user: {
        name: 'Joe Code',
        email: '[email protected]'
      }
    },
    src: '**/*'
  }
});

options.clone

  • type: string
  • default: '.grunt/grunt-gh-pages/gh-pages/repo'

Path to a directory where your repository will be cloned. If this directory doesn't already exist, it will be created. If it already exists, it is assumed to be a clone of your repository. If you stick with the default value (recommended), you will likely want to add .grunt to your .gitignore file.

Example use of the clone option:

/**
 * If you already have a temp directory, and want the repository cloned there,
 * use the `clone` option as below.  To avoid re-cloning every time the task is
 * run, this should be a directory that sticks around for a while.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      clone: 'path/to/tmp/dir'
    },
    src: '**/*'
  }
});

options.push

  • type: boolean
  • default: true

Push branch to remote. To commit only (with no push) set to false.

Example use of the push option:

grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      push: false
    },
    src: '**/*'
  }
});

options.silent

  • type: boolean
  • default: false

Suppress logging. This option should be used if the repository URL or other information passed to git commands is sensitive and should not be logged. With silent true log messages are suppressed and error messages are sanitized.

Example use of the silent option:

/**
 * This configuration will suppress logging and sanitize error messages.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      repo: 'https://' + process.env.GH_TOKEN + '@github.com/user/private-repo.git',
      silent: true
    },
    src: '**/*'
  }
});

options.git

  • type: string
  • default: 'git'

Your git executable.

Example use of the git option:

/**
 * If `git` is not on your path, provide the path as shown below.
 */
grunt.initConfig({
  'gh-pages': {
    options: {
      base: 'build',
      git: '/path/to/git'
    },
    src: '**/*'
  }
});

Dependencies

Note that this plugin requires Git 1.7.6 or higher (because it uses the --exit-code option for git ls-remote). If you'd like to see this working with earlier versions of Git, please open an issue.

Current Status

More Repositories

1

gh-pages

General purpose task for publishing files to a gh-pages branch on GitHub
JavaScript
3,090
star
2

grunt-newer

Configure Grunt tasks to run with newer files only.
JavaScript
949
star
3

mock-fs

Configurable mock for the fs module
JavaScript
906
star
4

gulp-newer

Pass through newer source files only
JavaScript
227
star
5

projzh

Projection utilities for working with Baidu maps
JavaScript
83
star
6

authorized

Action based authorization middleware.
JavaScript
59
star
7

es-main

Test if an ES module is run directly (require.main replacement)
JavaScript
58
star
8

karma-source-map-support

Karma plugin for inline sourcemap support
JavaScript
33
star
9

karma-phantomjs-shim

Provides shims when running tests in PhantomJS 1.x.
JavaScript
32
star
10

github-changelog

Create a simple changelog based on GitHub activity
JavaScript
28
star
11

mapjack

Map based editing of GeoJSON files on GitHub
JavaScript
22
star
12

pixelworks

βš™ Utilities for working with image data
JavaScript
16
star
13

stickymap

Sticky maps
JavaScript
14
star
14

ol-hashed

Maintain your map view's state in the URL hash
JavaScript
8
star
15

hashed

Serialize state from multiple providers using location.hash
JavaScript
8
star
16

jwt-claims

Minimal utility for decoding JWT claims
JavaScript
7
star
17

nik

Create a new package based on an existing one
JavaScript
5
star
18

wps-demo

OpenLayers Examples Using GeoServer's WPS
JavaScript
4
star
19

jugl

Jugl JavaScript Template Library
JavaScript
4
star
20

bitgeo

JavaScript
3
star
21

lightcycle

Remake of an 80s classic.
JavaScript
3
star
22

get-down

Download and optionally extract files
JavaScript
3
star
23

gitstatic

Make static websites with GitHub webhooks
JavaScript
3
star
24

F4GNA12-OL

FOSS4G NA 2012 OpenLayers Presentation
JavaScript
3
star
25

tilet

A demonstration tiled map client
JavaScript
3
star
26

geofunc

Functions for working with GeoJSON
JavaScript
2
star
27

static

Skeleton for static sites
Shell
2
star
28

thehoneybadger

Hungy, fearless, gross
JavaScript
2
star
29

doctest

Providing testfile and testmod methods for Rhino's doctest.
JavaScript
2
star
30

css-asset-rebaser

Copies assets from node_modules and rewrites URLs in CSS
JavaScript
2
star
31

kinda

JavaScript
2
star
32

ol3-video

Inspired by Mapbox's GL video demo
JavaScript
2
star
33

tiles

JavaScript
1
star
34

jsdoc-json

Template for JSDoc that outputs JSON
JavaScript
1
star
35

packajs

Build utility and debug server for Bower packages
JavaScript
1
star
36

bench-it

Benchmark your code
JavaScript
1
star
37

demo

A few demos
JavaScript
1
star
38

urel

Get the relative URL between two root-relative URLs
JavaScript
1
star
39

test-go-swagger

Makefile
1
star
40

suite

OpenGeo Suite Git Repository
JavaScript
1
star
41

restbin

App for examining HTTP requests.
JavaScript
1
star
42

pgfs

Postgres backed WFS 3
Go
1
star
43

karma-polyfill

Load polyfills from polyfill.io before running Karma tests
JavaScript
1
star
44

ol-raster

Raster sources for OL3
JavaScript
1
star
45

test-metalsmith-layouts

Testing partials with metalsmith-layouts
HTML
1
star
46

limited

Golang x/sync/errgroup like functionality with limits
Go
1
star
47

geoserver-geoscript-js

GeoScript extension for GeoScript JS
Java
1
star