Skip to content

Commit 15eb4e8

Browse files
committed
improve workflow for non-github.com gh-pages
1 parent bbfba3e commit 15eb4e8

File tree

3 files changed

+40
-60
lines changed

3 files changed

+40
-60
lines changed

src/command/publish/deployment.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,30 @@ export async function chooseDeployment(
183183

184184
// collect unique origins
185185
const originCounts = depoyments.reduce((origins, deployment) => {
186-
const originUrl = new URL(deployment.target.url!).origin;
187-
const count = origins.get(originUrl) || 0;
188-
origins.set(originUrl, count + 1);
186+
try {
187+
const originUrl = new URL(deployment.target.url!).origin;
188+
const count = origins.get(originUrl) || 0;
189+
origins.set(originUrl, count + 1);
190+
} catch {
191+
// url may not be valid and that shouldn't cause an error
192+
}
189193
return origins;
190194
}, new Map<string, number>());
191195

192196
const options = depoyments
193197
.map((deployment) => {
194-
const targetOrigin = new URL(deployment.target.url!).origin;
195-
const url = (originCounts.get(targetOrigin) === 1 &&
196-
deployment.provider.listOriginOnly)
197-
? targetOrigin
198-
: deployment.target.url;
198+
let url = deployment.target.url;
199+
try {
200+
const targetOrigin = new URL(deployment.target.url!).origin;
201+
if (
202+
originCounts.get(targetOrigin) === 1 &&
203+
deployment.provider.listOriginOnly
204+
) {
205+
url = targetOrigin;
206+
}
207+
} catch {
208+
// url may not be valid and that shouldn't cause an error
209+
}
199210

200211
return {
201212
name: `${url} (${deployment.provider.description}${

src/publish/README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,9 @@ A record of your previous publishes will be stored in a `_publish.yml` file with
3535
3636
Account information is not stored in this file, so it is suitable for checking in to version control and being shared by multiple publishers.
3737
38-
If you provide the `--id` option (described below) then a publish record is not saved (as in that case you already have another means for tracking published content).
39-
40-
## GitHub Pages
41-
42-
Publising to GitHub Pages works a little differently than to other providers. Rather than interacting with a remote API and writing publish records to `_publish.yml`, a special `gh-pages` branch is created and the contents of your `_site` or `_book` directory is pushed to this branch (GitHub will automatically publish a website when it sees a `gh-pages` branch created).
43-
44-
Ideally, `quarto publish` should be aware of the URL that will be used to access the published site (so that it can correctly write social metadata, a sitemap, and RSS feeds, all of which require absolute URLs). This URL can be deduced automatically for github.com but for GitHub Enterprise you should include a `site-url` in your website or book configuration. For example:
38+
Note that GitHub Pages publishes are not stored in the `_publish.yml` file (they are tracked by virtue of the creation of a `gh-pages` branch).
4539

46-
```yaml
47-
website:
48-
title: "My Site"
49-
site-url: https://ghserver.example.com/myuser/mysite
50-
```
40+
If you provide the `--id` option (described below) then a publish record is not saved (as in that case you already have another means for tracking published content).
5141

5242
## Headless / CI Usage
5343

src/publish/gh-pages/gh-pages.ts

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Confirm } from "cliffy/prompt/confirm.ts";
1515
import { removeIfExists, which } from "../../core/path.ts";
1616
import { execProcess } from "../../core/process.ts";
1717
import { projectContext } from "../../project/project-context.ts";
18-
import { websiteBaseurl } from "../../project/types/website/website-config.ts";
1918

2019
import { kProjectOutputDir, ProjectContext } from "../../project/types.ts";
2120
import {
@@ -80,10 +79,10 @@ function removeToken(_token: AccountToken) {
8079

8180
async function publishRecord(dir: string): Promise<PublishRecord | undefined> {
8281
const ghContext = await gitHubContext(dir);
83-
if (ghContext.ghPages && ghContext.siteUrl) {
82+
if (ghContext.ghPages) {
8483
return {
8584
id: "gh-pages",
86-
url: ghContext.siteUrl,
85+
url: ghContext.siteUrl || ghContext.originUrl,
8786
};
8887
}
8988
}
@@ -113,7 +112,9 @@ async function publish(
113112
// confirm
114113
const confirmed = await Confirm.prompt({
115114
indent: "",
116-
message: `Publish site to ${ghContext.siteUrl} using gh-pages?`,
115+
message: `Publish site to ${
116+
ghContext.siteUrl || ghContext.originUrl
117+
} using gh-pages?`,
117118
default: true,
118119
});
119120
if (!confirmed) {
@@ -181,7 +182,7 @@ async function publish(
181182

182183
// wait for deployment if we are opening a browser
183184
let verified = false;
184-
if (options.browser && ghContext.siteUrl && ghContext.browse) {
185+
if (options.browser && ghContext.siteUrl) {
185186
await withSpinner({
186187
message:
187188
"Deploying gh-pages branch to website (this may take a few minutes)",
@@ -203,14 +204,14 @@ async function publish(
203204
});
204205
}
205206

206-
completeMessage(`Published: ${ghContext.siteUrl}`);
207+
completeMessage(`Published to ${ghContext.siteUrl || ghContext.originUrl}`);
207208
info("");
208209

209210
if (!verified) {
210-
info(
211-
"NOTE: GitHub Pages deployments normally take a few minutes\n" +
212-
"(your site updates will visible once the deploy completes)\n",
213-
);
211+
info(colors.yellow(
212+
"NOTE: GitHub Pages deployments normally take a few minutes (your site updates\n" +
213+
"will be visible once the deploy completes)\n",
214+
));
214215
}
215216

216217
return Promise.resolve([
@@ -384,53 +385,31 @@ async function gitHubContext(dir: string) {
384385
})).success;
385386

386387
// determine siteUrl
387-
const info = await siteInfo(dir, context.originUrl!);
388-
if (info) {
389-
context.siteUrl = info.url;
390-
context.browse = info.browse;
391-
}
388+
context.siteUrl = siteUrl(dir, context.originUrl!);
392389
}
393390
}
394391
}
395392

396393
return context;
397394
}
398395

399-
async function siteInfo(dir: string, originUrl: string) {
396+
function siteUrl(dir: string, originUrl: string) {
400397
const cname = join(dir, "CNAME");
401398
if (existsSync(cname)) {
402-
return {
403-
url: Deno.readTextFileSync(cname).trim(),
404-
browse: true,
405-
};
399+
return Deno.readTextFileSync(cname).trim();
406400
} else {
407-
// git and ssh protccols
401+
// pick apart origin url for github.com
408402
const match = originUrl?.match(
409403
/git@([^:]+):([^\/]+)\/([^.]+)\.git/,
410404
) || originUrl?.match(
411405
/https:\/\/([^\/]+)\/([^\/]+)\/([^.]+)\.git/,
412406
);
413407

414-
if (match) {
415-
const kGithubCom = "github.com";
416-
const kGithubIo = "github.io";
408+
const kGithubCom = "github.com";
409+
const kGithubIo = "github.io";
410+
if (match && match.includes(kGithubCom)) {
417411
const server = match[1].replace(kGithubCom, kGithubIo);
418-
return {
419-
url: `https://${match[2]}.${server}/${match[3]}/`,
420-
browse: server.includes(kGithubIo),
421-
};
422-
}
423-
424-
// otherwise see if its in config
425-
const project = await projectContext(dir);
426-
if (project) {
427-
const url = websiteBaseurl(project.config);
428-
if (url) {
429-
return {
430-
url,
431-
browse: true,
432-
};
433-
}
412+
return `https://${match[2]}.${server}/${match[3]}/`;
434413
}
435414
}
436415
}

0 commit comments

Comments
 (0)