Skip to content

Commit d6b78ea

Browse files
committed
feat(deploy): allow gh-pages deploy to user/org page
1 parent 196855f commit d6b78ea

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The generated project has dependencies that require **Node 4 or greater**.
3131
* [Running Unit Tests](#running-unit-tests)
3232
* [Running End-to-End Tests](#running-end-to-end-tests)
3333
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
34+
* [Linting and formatting code](#linting-and-formatting-code)
3435
* [Support for offline applications](#support-for-offline-applications)
3536
* [Commands autocompletion](#commands-autocompletion)
3637
* [CSS preprocessor integration](#css-preprocessor-integration)
@@ -163,7 +164,7 @@ ng github-pages:deploy --message "Optional commit message"
163164
This will do the following:
164165

165166
- creates GitHub repo for the current project if one doesn't exist
166-
- rebuilds the app at the current `HEAD`
167+
- rebuilds the app in production mode at the current `HEAD`
167168
- creates a local `gh-pages` branch if one doesn't exist
168169
- moves your app to the `gh-pages` branch and creates a commit
169170
- edit the base tag in index.html to support github pages
@@ -174,6 +175,16 @@ Creating the repo requires a token from github, and the remaining functionality
174175
relies on ssh authentication for all git operations that communicate with github.com.
175176
To simplify the authentication, be sure to [setup your ssh keys](https://help.github.com/articles/generating-ssh-keys/).
176177

178+
If you are deploying a [user or organization page](https://help.github.com/articles/user-organization-and-project-pages/), you can instead use the following command:
179+
180+
```
181+
ng github-pages:deploy --user-page --message "Optional commit message"
182+
```
183+
184+
This command pushes the app to the `master` branch on the github repo instead
185+
of pushing to `gh-pages`, since user and organization pages require this.
186+
187+
177188
### Linting and formatting code
178189

179190
You can lint or format your app code by running `ng lint` or `ng format` respectively.

addon/ng2/commands/github-pages-deploy.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ module.exports = Command.extend({
3333
default: 'production',
3434
description: 'The Angular environment to create a build for'
3535
}, {
36-
name: 'branch',
37-
type: String,
38-
default: 'gh-pages',
39-
description: 'The git branch to push your pages to'
36+
name: 'user-page',
37+
type: Boolean,
38+
default: false,
39+
description: 'Deploy as a user/org page'
4040
}, {
4141
name: 'skip-build',
4242
type: Boolean,
@@ -62,6 +62,8 @@ module.exports = Command.extend({
6262
};
6363
var projectName = this.project.pkg.name;
6464

65+
let ghPagesBranch = 'gh-pages';
66+
let destinationBranch = options.userPage ? 'master' : ghPagesBranch;
6567
let initialBranch;
6668

6769
// declared here so that tests can stub exec
@@ -129,22 +131,29 @@ module.exports = Command.extend({
129131
.then(function(stdout) {
130132
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
131133
return createGithubRepoTask.run(createGithubRepoOptions)
132-
.then(() => execPromise(`git push -u origin ${initialBranch}`));
134+
.then(() => {
135+
// only push starting branch if it's not the destinationBranch
136+
// this happens commonly when using github user pages, since
137+
// they require the destination branch to be 'master'
138+
if (destinationBranch !== initialBranch) {
139+
execPromise(`git push -u origin ${initialBranch}`);
140+
}
141+
});
133142
}
134143
});
135144
}
136145

137146
function checkoutGhPages() {
138-
return execPromise(`git checkout ${options.branch}`)
147+
return execPromise(`git checkout ${ghPagesBranch}`)
139148
.catch(createGhPagesBranch)
140149
}
141150

142151
function createGhPagesBranch() {
143-
return execPromise(`git checkout --orphan ${options.branch}`)
152+
return execPromise(`git checkout --orphan ${ghPagesBranch}`)
144153
.then(() => execPromise('git rm --cached -r .', execOptions))
145154
.then(() => execPromise('git add .gitignore', execOptions))
146155
.then(() => execPromise('git clean -f -d', execOptions))
147-
.then(() => execPromise(`git commit -m \"initial ${options.branch} commit\"`));
156+
.then(() => execPromise(`git commit -m \"initial ${ghPagesBranch} commit\"`));
148157
}
149158

150159
function copyFiles() {
@@ -175,8 +184,8 @@ module.exports = Command.extend({
175184
return execPromise(`git checkout ${initialBranch}`);
176185
}
177186

178-
function pushToGitRepo(committed) {
179-
return execPromise(`git push origin ${options.branch}`);
187+
function pushToGitRepo() {
188+
return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`);
180189
}
181190

182191
function printProjectUrl() {

tests/acceptance/github-pages-deploy.spec.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
2323
let project = 'foo',
2424
initialBranch = 'master',
2525
branch = 'gh-pages',
26+
ghPagesBranch = 'gh-pages',
2627
message = 'new gh-pages version',
2728
remote = 'origin [email protected]:username/project.git (fetch)';
2829

@@ -71,11 +72,11 @@ describe('Acceptance: ng github-pages:deploy', function() {
7172
execStub.addExecSuccess('git status --porcelain')
7273
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
7374
.addExecSuccess('git remote -v', remote)
74-
.addExecSuccess(`git checkout ${branch}`)
75+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
7576
.addExecSuccess('git add .')
7677
.addExecSuccess(`git commit -m "${message}"`)
7778
.addExecSuccess(`git checkout ${initialBranch}`)
78-
.addExecSuccess(`git push origin ${branch}`)
79+
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
7980
.addExecSuccess('git remote -v', remote);
8081

8182
return ng(['github-pages:deploy', '--skip-build'])
@@ -87,21 +88,21 @@ describe('Acceptance: ng github-pages:deploy', function() {
8788
});
8889

8990
it('should deploy with changed defaults', function() {
90-
let branch = 'not-gh-pages',
91+
let userPageBranch = 'master',
9192
message = 'not new gh-pages version';
9293

9394
execStub.addExecSuccess('git status --porcelain')
9495
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
9596
.addExecSuccess('git remote -v', remote)
96-
.addExecSuccess(`git checkout ${branch}`)
97+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
9798
.addExecSuccess('git add .')
9899
.addExecSuccess(`git commit -m "${message}"`)
99100
.addExecSuccess(`git checkout ${initialBranch}`)
100-
.addExecSuccess(`git push origin ${branch}`)
101+
.addExecSuccess(`git push origin ${userPageBranch}:${userPageBranch}`)
101102
.addExecSuccess('git remote -v', remote);
102103

103104
return ng(['github-pages:deploy', '--skip-build', `--message=${message}`,
104-
`--branch=${branch}`])
105+
'--user-page'])
105106
.then(() => {
106107
let indexHtml = path.join(process.cwd(), 'index.html');
107108
return fsReadFile(indexHtml, 'utf8');
@@ -113,16 +114,16 @@ describe('Acceptance: ng github-pages:deploy', function() {
113114
execStub.addExecSuccess('git status --porcelain')
114115
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
115116
.addExecSuccess('git remote -v', remote)
116-
.addExecError(`git checkout ${branch}`)
117-
.addExecSuccess(`git checkout --orphan ${branch}`)
117+
.addExecError(`git checkout ${ghPagesBranch}`)
118+
.addExecSuccess(`git checkout --orphan ${ghPagesBranch}`)
118119
.addExecSuccess('git rm --cached -r .')
119120
.addExecSuccess('git add .gitignore')
120121
.addExecSuccess('git clean -f -d')
121-
.addExecSuccess(`git commit -m \"initial ${branch} commit\"`)
122+
.addExecSuccess(`git commit -m \"initial ${ghPagesBranch} commit\"`)
122123
.addExecSuccess('git add .')
123124
.addExecSuccess(`git commit -m "${message}"`)
124125
.addExecSuccess(`git checkout ${initialBranch}`)
125-
.addExecSuccess(`git push origin ${branch}`)
126+
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
126127
.addExecSuccess('git remote -v', remote);
127128

128129
return ng(['github-pages:deploy', '--skip-build'])
@@ -143,11 +144,11 @@ describe('Acceptance: ng github-pages:deploy', function() {
143144
.addExecSuccess('git remote -v', noRemote)
144145
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
145146
.addExecSuccess(`git push -u origin ${initialBranch}`)
146-
.addExecSuccess(`git checkout ${branch}`)
147+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
147148
.addExecSuccess('git add .')
148149
.addExecSuccess(`git commit -m "${message}"`)
149150
.addExecSuccess(`git checkout ${initialBranch}`)
150-
.addExecSuccess(`git push origin ${branch}`)
151+
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
151152
.addExecSuccess('git remote -v', remote);
152153

153154
var httpsStub = sinon.stub(https, 'request', httpsRequestStubFunc);
@@ -246,7 +247,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
246247
execStub.addExecSuccess('git status --porcelain')
247248
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
248249
.addExecSuccess('git remote -v', remote)
249-
.addExecSuccess(`git checkout ${branch}`)
250+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
250251
.addExecSuccess('git add .')
251252
.addExecSuccess(`git commit -m "${message}"`)
252253
.addExecError(`git checkout ${initialBranch}`, 'error: cannot stat \'src/client\': Permission denied');

0 commit comments

Comments
 (0)