Skip to content

feat(deploy): add custom-domain support for gh-pages deployment (#1781) #3391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions packages/angular-cli/commands/github-pages-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { oneLine } from 'common-tags';

const fsReadDir = <any>denodeify(fs.readdir);
const fsCopy = <any>denodeify(fse.copy);
const fsWriteFile = <any>denodeify(fse.writeFile);

interface GithubPagesDeployOptions {
message?: string;
Expand All @@ -24,6 +25,7 @@ interface GithubPagesDeployOptions {
ghToken?: string;
ghUsername?: string;
baseHref?: string;
customDomain?: string;
}

const githubPagesDeployCommand = Command.extend({
Expand Down Expand Up @@ -76,6 +78,11 @@ const githubPagesDeployCommand = Command.extend({
type: String,
default: null,
aliases: ['bh']
}, {
name: 'custom-domain',
type: String,
default: null,
description: 'Custom domain for Github Pages'
}],

run: function(options: GithubPagesDeployOptions, rawArgs: string[]) {
Expand Down Expand Up @@ -117,11 +124,15 @@ const githubPagesDeployCommand = Command.extend({

/**
* BaseHref tag setting logic:
* First, use --base-href flag value if provided.
* First, no value if --custom-domain is provided.
* Second, use --base-href flag value if provided.
* Else if --user-page is true, then keep baseHref default as declared in index.html.
* Otherwise auto-replace with `/${projectName}/`.
*/
const baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
let baseHref: String = null;
if (!options.customDomain) {
baseHref = baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
}

const buildOptions = {
target: options.target,
Expand Down Expand Up @@ -150,6 +161,7 @@ const githubPagesDeployCommand = Command.extend({
.then(cleanGhPagesBranch)
.then(copyFiles)
.then(createNotFoundPage)
.then(createCustomDomainFile)
.then(addAndCommit)
.then(returnStartingBranch)
.then(pushToGitRepo)
Expand Down Expand Up @@ -237,6 +249,15 @@ const githubPagesDeployCommand = Command.extend({
return fsCopy(indexHtml, notFoundPage);
}

function createCustomDomainFile() {
if (!options.customDomain) {
return;
}

const cnameFile = path.join(root, 'CNAME');
return fsWriteFile(cnameFile, options.customDomain);
}

function addAndCommit() {
return execPromise('git add .', execOptions)
.then(() => execPromise(`git commit -m "${options.message}"`))
Expand All @@ -256,7 +277,14 @@ const githubPagesDeployCommand = Command.extend({
.then((stdout) => {
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
let userName = match[1].toLowerCase();
let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
let url = '';

if (options.customDomain) {
url = `http://${options.customDomain}/`;
} else {
url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
}

ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
});
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-cli/utilities/completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ng_opts='b build completion doc e2e g generate get github-pages:deploy gh-pages:

build_opts='--aot --base-href --environment --output-path --suppress-sizes --target --watch --watcher -bh -dev -e -o -prod -t -w'
generate_opts='class component directive enum module pipe route service c cl d e m p r s --help'
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page -bh -e -t'
github_pages_deploy_opts='--base-href --environment --gh-token --gh-username --message --skip-build --target --user-page --custom-domain -bh -e -t'
help_opts='--json --verbose -v'
init_opts='--dry-run inline-style inline-template --link-cli --mobile --name --prefix --routing --skip-bower --skip-npm --source-dir --style --verbose -d -is -it -lc -n -p -sb -sd -sn -v'
new_opts='--directory --dry-run inline-style inline-template --link-cli --mobile --prefix --routing --skip-bower --skip-git --skip-npm --source-dir --style --verbose -d -dir -is -it -lc -p -sb -sd -sg -sn -v'
Expand Down