Skip to content

Commit ec73994

Browse files
committed
feat(github): support usage of gh-token for deployment from external env
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 f495bd8 commit ec73994

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

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

+34-7
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ const githubPagesDeployCommand = Command.extend({
179179
.then(function(stdout) {
180180
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
181181
return createGithubRepoTask.run(createGithubRepoOptions)
182-
.then(() => {
182+
.then(() => generateRemoteUrl())
183+
.then((upstream: string) => {
183184
// only push starting branch if it's not the destinationBranch
184185
// this happens commonly when using github user pages, since
185186
// they require the destination branch to be 'master'
186187
if (destinationBranch !== initialBranch) {
187-
execPromise(`git push -u origin ${initialBranch}`);
188+
execPromise(`git push -u ${upstream} ${initialBranch}`);
188189
}
189190
});
190191
}
@@ -232,14 +233,15 @@ const githubPagesDeployCommand = Command.extend({
232233
}
233234

234235
function pushToGitRepo() {
235-
return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`);
236+
return generateRemoteUrl()
237+
.then(upstream => {
238+
return execPromise(`git push ${upstream} ${ghPagesBranch}:${destinationBranch}`);
239+
});
236240
}
237241

238242
function printProjectUrl() {
239-
return execPromise('git remote -v')
240-
.then((stdout) => {
241-
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
242-
let userName = match[1].toLowerCase();
243+
return getUsernameFromGitOrigin()
244+
.then((userName) => {
243245
let url = `https://${userName}.github.io/${options.userPage ? '' : (projectName + '/')}`;
244246
ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
245247
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
@@ -257,6 +259,31 @@ const githubPagesDeployCommand = Command.extend({
257259
return Promise.reject(error);
258260
}
259261
}
262+
263+
function generateRemoteUrl(): Promise<String> {
264+
if (createGithubRepoOptions.ghToken && createGithubRepoOptions.ghUsername) {
265+
return Promise.resolve(`https://${createGithubRepoOptions.ghToken}@github.com/` +
266+
`${createGithubRepoOptions.ghUsername}/${createGithubRepoOptions.projectName}.git`);
267+
}
268+
269+
if (createGithubRepoOptions.ghToken && !createGithubRepoOptions.ghUsername) {
270+
return getUsernameFromGitOrigin()
271+
.then(username => {
272+
return Promise.resolve(`https://${createGithubRepoOptions.ghToken}@github.com/` +
273+
`${username}/${createGithubRepoOptions.projectName}.git`);
274+
});
275+
}
276+
277+
return Promise.resolve('origin');
278+
}
279+
280+
function getUsernameFromGitOrigin(): Promise<String> {
281+
return execPromise('git remote -v')
282+
.then((stdout) => {
283+
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
284+
return match[1].toLowerCase();
285+
});
286+
}
260287
}
261288
});
262289

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

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

78+
it('should deploy with token and username', function () {
79+
let token = 'token',
80+
username = 'bar';
81+
82+
execStub.addExecSuccess('git status --porcelain')
83+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
84+
.addExecSuccess('git remote -v', remote)
85+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
86+
.addExecSuccess('git add .')
87+
.addExecSuccess(`git commit -m "${message}"`)
88+
.addExecSuccess(`git checkout ${initialBranch}`)
89+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
90+
.addExecSuccess('git remote -v', remote);
91+
92+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`, `--gh-username=${username}`]);
93+
})
94+
95+
it('should deploy with token only', function () {
96+
let token = 'token';
97+
98+
execStub.addExecSuccess('git status --porcelain')
99+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
100+
.addExecSuccess('git remote -v', remote)
101+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
102+
.addExecSuccess('git add .')
103+
.addExecSuccess(`git commit -m "${message}"`)
104+
.addExecSuccess(`git checkout ${initialBranch}`)
105+
.addExecSuccess('git remote -v', remote)
106+
.addExecSuccess(`git push https://${token}@github.com/username/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
107+
.addExecSuccess('git remote -v', remote);
108+
109+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`]);
110+
});
111+
78112
it('should deploy with changed defaults', function() {
79113
let userPageBranch = 'master',
80114
message = 'not new gh-pages version';
@@ -120,12 +154,12 @@ describe('Acceptance: ng github-pages:deploy', function() {
120154
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
121155
.addExecSuccess('git remote -v', noRemote)
122156
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
123-
.addExecSuccess(`git push -u origin ${initialBranch}`)
157+
.addExecSuccess(`git push -u https://${token}@github.com/${username}/${project}.git ${initialBranch}`)
124158
.addExecSuccess(`git checkout ${ghPagesBranch}`)
125159
.addExecSuccess('git add .')
126160
.addExecSuccess(`git commit -m "${message}"`)
127161
.addExecSuccess(`git checkout ${initialBranch}`)
128-
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
162+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
129163
.addExecSuccess('git remote -v', remote);
130164

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

0 commit comments

Comments
 (0)