Skip to content

Commit

Permalink
feat(deploy): allow gh-pages deploy to user/org page
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed May 15, 2016
1 parent 196855f commit d6b78ea
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The generated project has dependencies that require **Node 4 or greater**.
* [Running Unit Tests](#running-unit-tests)
* [Running End-to-End Tests](#running-end-to-end-tests)
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
* [Linting and formatting code](#linting-and-formatting-code)
* [Support for offline applications](#support-for-offline-applications)
* [Commands autocompletion](#commands-autocompletion)
* [CSS preprocessor integration](#css-preprocessor-integration)
Expand Down Expand Up @@ -163,7 +164,7 @@ ng github-pages:deploy --message "Optional commit message"
This will do the following:

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

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:

```
ng github-pages:deploy --user-page --message "Optional commit message"
```

This command pushes the app to the `master` branch on the github repo instead
of pushing to `gh-pages`, since user and organization pages require this.


### Linting and formatting code

You can lint or format your app code by running `ng lint` or `ng format` respectively.
Expand Down
29 changes: 19 additions & 10 deletions addon/ng2/commands/github-pages-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ module.exports = Command.extend({
default: 'production',
description: 'The Angular environment to create a build for'
}, {
name: 'branch',
type: String,
default: 'gh-pages',
description: 'The git branch to push your pages to'
name: 'user-page',
type: Boolean,
default: false,
description: 'Deploy as a user/org page'
}, {
name: 'skip-build',
type: Boolean,
Expand All @@ -62,6 +62,8 @@ module.exports = Command.extend({
};
var projectName = this.project.pkg.name;

let ghPagesBranch = 'gh-pages';
let destinationBranch = options.userPage ? 'master' : ghPagesBranch;
let initialBranch;

// declared here so that tests can stub exec
Expand Down Expand Up @@ -129,22 +131,29 @@ module.exports = Command.extend({
.then(function(stdout) {
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
return createGithubRepoTask.run(createGithubRepoOptions)
.then(() => execPromise(`git push -u origin ${initialBranch}`));
.then(() => {
// only push starting branch if it's not the destinationBranch
// this happens commonly when using github user pages, since
// they require the destination branch to be 'master'
if (destinationBranch !== initialBranch) {
execPromise(`git push -u origin ${initialBranch}`);
}
});
}
});
}

function checkoutGhPages() {
return execPromise(`git checkout ${options.branch}`)
return execPromise(`git checkout ${ghPagesBranch}`)
.catch(createGhPagesBranch)
}

function createGhPagesBranch() {
return execPromise(`git checkout --orphan ${options.branch}`)
return execPromise(`git checkout --orphan ${ghPagesBranch}`)
.then(() => execPromise('git rm --cached -r .', execOptions))
.then(() => execPromise('git add .gitignore', execOptions))
.then(() => execPromise('git clean -f -d', execOptions))
.then(() => execPromise(`git commit -m \"initial ${options.branch} commit\"`));
.then(() => execPromise(`git commit -m \"initial ${ghPagesBranch} commit\"`));
}

function copyFiles() {
Expand Down Expand Up @@ -175,8 +184,8 @@ module.exports = Command.extend({
return execPromise(`git checkout ${initialBranch}`);
}

function pushToGitRepo(committed) {
return execPromise(`git push origin ${options.branch}`);
function pushToGitRepo() {
return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`);
}

function printProjectUrl() {
Expand Down
27 changes: 14 additions & 13 deletions tests/acceptance/github-pages-deploy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
let project = 'foo',
initialBranch = 'master',
branch = 'gh-pages',
ghPagesBranch = 'gh-pages',
message = 'new gh-pages version',
remote = 'origin [email protected]:username/project.git (fetch)';

Expand Down Expand Up @@ -71,11 +72,11 @@ describe('Acceptance: ng github-pages:deploy', function() {
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecSuccess(`git checkout ${branch}`)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${branch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);

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

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

execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecSuccess(`git checkout ${branch}`)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${branch}`)
.addExecSuccess(`git push origin ${userPageBranch}:${userPageBranch}`)
.addExecSuccess('git remote -v', remote);

return ng(['github-pages:deploy', '--skip-build', `--message=${message}`,
`--branch=${branch}`])
'--user-page'])
.then(() => {
let indexHtml = path.join(process.cwd(), 'index.html');
return fsReadFile(indexHtml, 'utf8');
Expand All @@ -113,16 +114,16 @@ describe('Acceptance: ng github-pages:deploy', function() {
execStub.addExecSuccess('git status --porcelain')
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
.addExecSuccess('git remote -v', remote)
.addExecError(`git checkout ${branch}`)
.addExecSuccess(`git checkout --orphan ${branch}`)
.addExecError(`git checkout ${ghPagesBranch}`)
.addExecSuccess(`git checkout --orphan ${ghPagesBranch}`)
.addExecSuccess('git rm --cached -r .')
.addExecSuccess('git add .gitignore')
.addExecSuccess('git clean -f -d')
.addExecSuccess(`git commit -m \"initial ${branch} commit\"`)
.addExecSuccess(`git commit -m \"initial ${ghPagesBranch} commit\"`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${branch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);

return ng(['github-pages:deploy', '--skip-build'])
Expand All @@ -143,11 +144,11 @@ describe('Acceptance: ng github-pages:deploy', function() {
.addExecSuccess('git remote -v', noRemote)
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
.addExecSuccess(`git push -u origin ${initialBranch}`)
.addExecSuccess(`git checkout ${branch}`)
.addExecSuccess(`git checkout ${ghPagesBranch}`)
.addExecSuccess('git add .')
.addExecSuccess(`git commit -m "${message}"`)
.addExecSuccess(`git checkout ${initialBranch}`)
.addExecSuccess(`git push origin ${branch}`)
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
.addExecSuccess('git remote -v', remote);

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

0 comments on commit d6b78ea

Please sign in to comment.