Skip to content

Commit 3c82b77

Browse files
davinkevinhansl
authored andcommitted
feat(deploy:github-pages): support usage of gh-token for deployment from external env (#3121)
In order to being able to push to github from CI (Travis for example), we should be able to provide a token to the cli, as `--gh-token=XYZ`. This modification allow to use this token in the destination url. This also allow user to push to another github repository if it uses a different `--gh-username` from the one used for the checkout.
1 parent da1c197 commit 3c82b77

File tree

3 files changed

+150
-84
lines changed

3 files changed

+150
-84
lines changed

packages/angular-cli/commands/github-pages-deploy.run.ts

+53-25
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,20 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
113113

114114
function createGitHubRepoIfNeeded() {
115115
return execPromise('git remote -v')
116-
.then(function (stdout) {
117-
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
118-
return createGithubRepoTask.run(createGithubRepoOptions)
119-
.then(() => {
120-
// only push starting branch if it's not the destinationBranch
121-
// this happens commonly when using github user pages, since
122-
// they require the destination branch to be 'master'
123-
if (destinationBranch !== initialBranch) {
124-
execPromise(`git push -u origin ${initialBranch}`);
125-
}
126-
});
127-
}
128-
});
116+
.then(function(stdout) {
117+
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
118+
return createGithubRepoTask.run(createGithubRepoOptions)
119+
.then(() => generateRemoteUrl())
120+
.then((upstream: string) => {
121+
// only push starting branch if it's not the destinationBranch
122+
// this happens commonly when using github user pages, since
123+
// they require the destination branch to be 'master'
124+
if (destinationBranch !== initialBranch) {
125+
execPromise(`git push -u ${upstream} ${initialBranch}`);
126+
}
127+
});
128+
}
129+
});
129130
}
130131

131132
function checkoutGhPages() {
@@ -191,30 +192,57 @@ export default function githubPagesDeployRun(options: GithubPagesDeployOptions,
191192
}
192193

193194
function pushToGitRepo() {
194-
return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`)
195-
.catch((err) => returnStartingBranch()
196-
.catch(() => Promise.reject(err)));
195+
return generateRemoteUrl()
196+
.then(upstream => {
197+
return execPromise(`git push ${upstream} ${ghPagesBranch}:${destinationBranch}`);
198+
})
199+
.catch((err) => returnStartingBranch()
200+
.catch(() => Promise.reject(err) ));
197201
}
198202

199203
function printProjectUrl() {
200-
return execPromise('git remote -v')
201-
.then((stdout) => {
202-
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
203-
let userName = match[1].toLowerCase();
204-
let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
205-
ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
206-
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
207-
});
204+
return getUsernameFromGitOrigin()
205+
.then((userName) => {
206+
let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
207+
ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
208+
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
209+
});
208210
}
209211

210212
function failGracefully(error: Error) {
211213
if (error && (/git clean/.test(error.message) || /Permission denied/.test(error.message))) {
212214
ui.writeLine(error.message);
213215
let msg = 'There was a permissions error during git file operations, ' +
214-
'please close any open project files/folders and try again.';
216+
'please close any open project files/folders and try again.';
217+
msg += `\nYou might also need to return to the ${initialBranch} branch manually.`;
215218
return Promise.reject(new SilentError(msg.concat(branchErrMsg)));
216219
} else {
217220
return Promise.reject(error);
218221
}
219222
}
223+
224+
function generateRemoteUrl(): Promise<String> {
225+
if (createGithubRepoOptions.ghToken && createGithubRepoOptions.ghUsername) {
226+
return Promise.resolve(`https://${createGithubRepoOptions.ghToken}@github.com/` +
227+
`${createGithubRepoOptions.ghUsername}/${createGithubRepoOptions.projectName}.git`);
228+
}
229+
230+
if (createGithubRepoOptions.ghToken && !createGithubRepoOptions.ghUsername) {
231+
return getUsernameFromGitOrigin()
232+
.then(username => {
233+
return Promise.resolve(`https://${createGithubRepoOptions.ghToken}@github.com/` +
234+
`${username}/${createGithubRepoOptions.projectName}.git`);
235+
});
236+
}
237+
238+
return Promise.resolve('origin');
239+
}
240+
241+
function getUsernameFromGitOrigin(): Promise<String> {
242+
return execPromise('git remote -v')
243+
.then((stdout) => {
244+
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
245+
return match[1].toLowerCase();
246+
});
247+
}
220248
}

packages/angular-cli/commands/github-pages-deploy.ts

+57-57
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,71 @@ const Command = require('../ember-cli/lib/models/command');
22
import { oneLine } from 'common-tags';
33

44
export interface GithubPagesDeployOptions {
5-
message?: string;
6-
target?: string;
7-
environment?: string;
8-
userPage?: boolean;
9-
skipBuild?: boolean;
10-
ghToken?: string;
11-
ghUsername?: string;
12-
baseHref?: string;
5+
message?: string;
6+
target?: string;
7+
environment?: string;
8+
userPage?: boolean;
9+
skipBuild?: boolean;
10+
ghToken?: string;
11+
ghUsername?: string;
12+
baseHref?: string;
1313
}
1414

1515
const githubPagesDeployCommand = Command.extend({
16-
name: 'github-pages:deploy',
17-
aliases: ['gh-pages:deploy'],
18-
description: oneLine`
16+
name: 'github-pages:deploy',
17+
aliases: ['gh-pages:deploy'],
18+
description: oneLine`
1919
Build the test app for production, commit it into a git branch,
2020
setup GitHub repo and push to it
2121
`,
22-
works: 'insideProject',
22+
works: 'insideProject',
2323

24-
availableOptions: [
25-
{
26-
name: 'message',
27-
type: String,
28-
default: 'new gh-pages version',
29-
description: 'The commit message to include with the build, must be wrapped in quotes.'
30-
}, {
31-
name: 'target',
32-
type: String,
33-
default: 'production',
34-
aliases: ['t', { 'dev': 'development' }, { 'prod': 'production' }]
35-
}, {
36-
name: 'environment',
37-
type: String,
38-
default: '',
39-
aliases: ['e']
40-
}, {
41-
name: 'user-page',
42-
type: Boolean,
43-
default: false,
44-
description: 'Deploy as a user/org page'
45-
}, {
46-
name: 'skip-build',
47-
type: Boolean,
48-
default: false,
49-
description: 'Skip building the project before deploying'
50-
}, {
51-
name: 'gh-token',
52-
type: String,
53-
default: '',
54-
description: 'Github token'
55-
}, {
56-
name: 'gh-username',
57-
type: String,
58-
default: '',
59-
description: 'Github username'
60-
}, {
61-
name: 'base-href',
62-
type: String,
63-
default: null,
64-
aliases: ['bh']
65-
}],
24+
availableOptions: [
25+
{
26+
name: 'message',
27+
type: String,
28+
default: 'new gh-pages version',
29+
description: 'The commit message to include with the build, must be wrapped in quotes.'
30+
}, {
31+
name: 'target',
32+
type: String,
33+
default: 'production',
34+
aliases: ['t', { 'dev': 'development' }, { 'prod': 'production' }]
35+
}, {
36+
name: 'environment',
37+
type: String,
38+
default: '',
39+
aliases: ['e']
40+
}, {
41+
name: 'user-page',
42+
type: Boolean,
43+
default: false,
44+
description: 'Deploy as a user/org page'
45+
}, {
46+
name: 'skip-build',
47+
type: Boolean,
48+
default: false,
49+
description: 'Skip building the project before deploying'
50+
}, {
51+
name: 'gh-token',
52+
type: String,
53+
default: '',
54+
description: 'Github token'
55+
}, {
56+
name: 'gh-username',
57+
type: String,
58+
default: '',
59+
description: 'Github username'
60+
}, {
61+
name: 'base-href',
62+
type: String,
63+
default: null,
64+
aliases: ['bh']
65+
}],
6666

67-
run: function(options: GithubPagesDeployOptions, rawArgs: string[]) {
68-
return require('./github-pages-deploy.run').default.call(this, options, rawArgs);
69-
}
67+
run: function(options: GithubPagesDeployOptions, rawArgs: string[]) {
68+
return require('./github-pages-deploy.run').default.call(this, options, rawArgs);
69+
}
7070
});
7171

7272

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

+40-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,44 @@ describe('Acceptance: ng github-pages:deploy', function() {
7777
return ng(['github-pages:deploy', '--skip-build']);
7878
});
7979

80+
it('should deploy with token and username', function () {
81+
let token = 'token',
82+
username = 'bar';
83+
84+
execStub.addExecSuccess('git status --porcelain')
85+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
86+
.addExecSuccess('git remote -v', remote)
87+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
88+
.addExecSuccess('git ls-files')
89+
.addExecSuccess('git rm -r ')
90+
.addExecSuccess('git add .')
91+
.addExecSuccess(`git commit -m "${message}"`)
92+
.addExecSuccess(`git checkout ${initialBranch}`)
93+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
94+
.addExecSuccess('git remote -v', remote);
95+
96+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`, `--gh-username=${username}`]);
97+
})
98+
99+
it('should deploy with token only', function () {
100+
let token = 'token';
101+
102+
execStub.addExecSuccess('git status --porcelain')
103+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
104+
.addExecSuccess('git remote -v', remote)
105+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
106+
.addExecSuccess('git ls-files')
107+
.addExecSuccess('git rm -r ')
108+
.addExecSuccess('git add .')
109+
.addExecSuccess(`git commit -m "${message}"`)
110+
.addExecSuccess(`git checkout ${initialBranch}`)
111+
.addExecSuccess('git remote -v', remote)
112+
.addExecSuccess(`git push https://${token}@github.com/username/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
113+
.addExecSuccess('git remote -v', remote);
114+
115+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`]);
116+
});
117+
80118
it('should deploy with changed defaults', function() {
81119
let userPageBranch = 'master',
82120
message = 'not new gh-pages version';
@@ -126,14 +164,14 @@ describe('Acceptance: ng github-pages:deploy', function() {
126164
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
127165
.addExecSuccess('git remote -v', noRemote)
128166
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
129-
.addExecSuccess(`git push -u origin ${initialBranch}`)
167+
.addExecSuccess(`git push -u https://${token}@github.com/${username}/${project}.git ${initialBranch}`)
130168
.addExecSuccess(`git checkout ${ghPagesBranch}`)
131169
.addExecSuccess('git ls-files')
132170
.addExecSuccess('git rm -r ')
133171
.addExecSuccess('git add .')
134172
.addExecSuccess(`git commit -m "${message}"`)
135173
.addExecSuccess(`git checkout ${initialBranch}`)
136-
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
174+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
137175
.addExecSuccess('git remote -v', remote);
138176

139177
var httpsStub = sinon.stub(https, 'request', httpsRequestStubFunc);

0 commit comments

Comments
 (0)