UNPKG

8.81 kBMarkdownView Raw
1# gh-pages
2
3Publish files to a `gh-pages` branch on GitHub (or any other branch anywhere else).
4
5## Getting Started
6
7```shell
8npm install gh-pages --save-dev
9```
10
11This module requires Git `>=1.7.6`.
12
13## Basic Usage
14
15```js
16var ghpages = require('gh-pages');
17var path = require('path');
18
19ghpages.publish(path.join(__dirname, 'dist'), function(err) { ... });
20```
21
22
23## `publish`
24
25```js
26ghpages.publish(basePath, callback);
27// or...
28ghpages.publish(basePath, options, callback);
29```
30
31Calling this function 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 base path, or only those that match patterns from the optional `src` configuration, commit all changes, and push to the `origin` remote.
32
33If 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.
34
35**Note** that any files in the `gh-pages` branch that are *not* in the `src` files **will be removed**. See the [`add` option](#optionsadd) if you don't want any of the existing files removed.
36
37
38### <a id="optionsbase">`basePath`</a>
39* type: `string`
40
41The base directory for all source files (those listed in the `src` config property).
42
43Example use of the `basePath`:
44
45```js
46/**
47 * Given the following directory structure:
48 *
49 * build/
50 * index.html
51 * js/
52 * site.js
53 *
54 * The usage below will create a `gh-pages` branch that looks like this:
55 *
56 * index.html
57 * js/
58 * site.js
59 *
60 */
61ghpages.publish(path.join(__dirname, 'build'), callback);
62```
63
64
65### Options
66
67The default options work for simple cases cases. The options described below let you push to alternate branches, customize your commit messages, and more.
68
69
70#### <a id="optionssrc">options.src</a>
71 * type: `string`
72 * default: `'**/*'`
73
74The [minimatch](https://github.com/isaacs/minimatch) pattern used to select which files should be published.
75
76
77#### <a id="optionsdotfiles">options.dotfiles</a>
78 * type: `boolean`
79 * default: `false`
80
81Include 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.
82
83Example use of the `dotfiles` option:
84
85```js
86/**
87 * The usage below will push dotfiles (directories and files)
88 * that otherwise match the `src` pattern.
89 */
90ghpages.publish(path.join(__dirname, 'dist'), { dotfiles: true }, callback);
91```
92
93
94#### <a id="optionsadd">options.add</a>
95 * type: `boolean`
96 * default: `false`
97
98Only 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 options.
99
100Example use of the `add` option:
101
102```js
103/**
104 * The usage below will only add files to the `gh-pages` branch, never removing
105 * any existing files (even if they don't exist in the `src` config).
106 */
107ghpages.publish(path.join(__dirname, 'build'), { add: true }, callback);
108```
109
110
111#### <a id="optionsrepo">options.repo</a>
112 * type: `string`
113 * default: url for the origin remote of the current dir (assumes a git repository)
114
115By default, `gh-pages` assumes that the current working directory is a git repository, and that you want to push changes to the `origin` remote.
116
117If instead your script 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.
118
119Example use of the `repo` option:
120
121```js
122/**
123 * If the current directory is not a clone of the repository you want to work
124 * with, set the URL for the repository in the `repo` option. This usage will
125 * push all files in the `src` config to the `gh-pages` branch of the `repo`.
126 */
127ghpages.publish(path.join(__dirname, 'build'), {
128 repo: 'https://example.com/other/repo.git'
129}, callback);
130```
131
132
133#### <a id="optionsbranch">options.branch</a>
134 * type: `string`
135 * default: `'gh-pages'`
136
137The name of the branch you'll be pushing to. The default uses GitHub's `gh-pages` branch, but this can be configured to push to any branch on any remote.
138
139Example use of the `branch` option:
140
141```js
142/**
143 * This task pushes to the `master` branch of the configured `repo`.
144 */
145ghpages.publish(path.join(__dirname, 'build'), {
146 branch: 'master',
147 repo: 'https://example.com/other/repo.git'
148}, callback);
149```
150
151
152#### <a id="optionstag">options.tag</a>
153 * type: `string`
154 * default: `''`
155
156Create 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.
157
158
159#### <a id="optionsmessage">options.message</a>
160 * type: `string`
161 * default: `'Updates'`
162
163The commit message for all commits.
164
165Example use of the `message` option:
166
167```js
168/**
169 * This adds commits with a custom message.
170 */
171ghpages.publish(path.join(__dirname, 'build'), {
172 message: 'Auto-generated commit'
173}, callback);
174```
175
176
177#### <a id="optionsuser">options.user</a>
178 * type: `Object`
179 * default: `null`
180
181If 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.
182
183Example use of the `user` option:
184
185```js
186ghpages.publish(path.join(__dirname, 'build'), {
187 user: {
188 name: 'Joe Code',
189 email: 'coder@example.com'
190 }
191}, callback);
192```
193
194
195#### <a id="optionsclone">options.clone</a>
196 * type: `string`
197 * default: temporary directory inside the `gh-pages` directory
198
199Path 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.
200
201Example use of the `clone` option:
202
203```js
204/**
205 * If you already have a temp directory, and want the repository cloned there,
206 * use the `clone` option as below. To avoid re-cloning every time the task is
207 * run, this should be a directory that sticks around for a while.
208 */
209ghpages.publish(path.join(__dirname, 'build'), {
210 clone: 'path/to/tmp/dir'
211}, callback);
212```
213
214
215#### <a id="optionspush">options.push</a>
216 * type: `boolean`
217 * default: `true`
218
219Push branch to remote. To commit only (with no push) set to `false`.
220
221Example use of the `push` option:
222
223```js
224ghpages.publish(path.join(__dirname, 'build'), { push: false }, callback);
225```
226
227
228#### <a id="optionssilent">options.silent</a>
229 * type: `boolean`
230 * default: `false`
231
232Suppress 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.
233
234Example use of the `silent` option:
235
236```js
237/**
238 * This configuration will suppress logging and sanitize error messages.
239 */
240ghpages.publish(path.join(__dirname, 'build'), {
241 repo: 'https://' + process.env.GH_TOKEN + '@github.com/user/private-repo.git',
242 silent: true
243}, callback);
244```
245
246
247#### <a id="optionslogger">options.logger</a>
248 * type: `function(string)`
249 * default: `function(){}`
250
251Logger function. The default logging function is a no-op, allowing you to provide a custom logging implementation.
252
253Example use of the `logger` option:
254
255```js
256/**
257 * This configuration will log to the console
258 */
259ghpages.publish(path.join(__dirname, 'build'), {
260 logger: function(message) {
261 console.log(message);
262 }
263}, callback);
264```
265
266
267#### <a id="optionsgit">options.git</a>
268 * type: `string`
269 * default: `'git'`
270
271Your `git` executable.
272
273Example use of the `git` option:
274
275```js
276/**
277 * If `git` is not on your path, provide the path as shown below.
278 */
279ghpages.publish(path.join(__dirname, 'build'), {
280 git: '/path/to/git'
281}, callback);
282```
283
284## Command Line Utility
285
286Installing the package creates a `gh-pages` command line utility. Run `gh-pages --help` to see a list of supported options.
287
288With a local install of `gh-pages`, you can set up a package script with something like the following:
289
290```shell
291"scripts": {
292 "deploy": "gh-pages -d dist"
293}
294```
295
296And then to publish everything from your `dist` folder to your `gh-pages` branch, you'd run this:
297
298```shell
299npm run deploy
300```
301
302## Dependencies
303
304Note 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](https://github.com/tschaub/gh-pages/issues).
305
306[![Current Status](https://secure.travis-ci.org/tschaub/gh-pages.png?branch=master)](https://travis-ci.org/tschaub/gh-pages)