Skip to content

Commit 40ee558

Browse files
committed
Move to WFP for preview deployments
1 parent a375861 commit 40ee558

File tree

6 files changed

+60
-355
lines changed

6 files changed

+60
-355
lines changed

.github/workflows/publish-preview.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ jobs:
3232
name: Build
3333
env:
3434
NODE_OPTIONS: --max-old-space-size=4096
35-
- run: npx wrangler versions upload
36-
name: Deploy to Cloudflare Workers
35+
- name: Deploy to Cloudflare Workers
3736
env:
3837
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
39-
WRANGLER_OUTPUT_FILE_PATH: wrangler-logs.ndjson
38+
run: |
39+
SHORT_SHA=$(git rev-parse --short=8 HEAD)
40+
BRANCH_SLUG=$(git rev-parse --abbrev-ref HEAD | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr A-Z a-z)
41+
42+
npx wrangler deploy --dispatch-namespace preview-deployments --name $SHORT_SHA
43+
npx wrangler deploy --dispatch-namespace preview-deployments --name $BRANCH_SLUG
4044
- name: Post preview URL on PR
4145
env:
4246
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

bin/post-preview-url-comment/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ export const GITHUB_ACTIONS_BOT_ID = 41898282;
22
export const DOCS_BASE_URL = "https://developers.cloudflare.com";
33
export const CONTENT_BASE_PATH = "src/content";
44
export const PREVIEW_URL_REGEX = /^\*\*Preview URL:\*\* (.*)$/m;
5-
export const WRANGLER_LOGS_PATH = "wrangler-logs.ndjson";

bin/post-preview-url-comment/index.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import * as core from "@actions/core";
22
import * as github from "@actions/github";
33

4-
import { readFile } from "node:fs/promises";
5-
64
import {
75
CONTENT_BASE_PATH,
86
DOCS_BASE_URL,
97
GITHUB_ACTIONS_BOT_ID,
108
PREVIEW_URL_REGEX,
11-
WRANGLER_LOGS_PATH,
129
} from "./constants";
1310

14-
import { filenameToPath } from "./util";
11+
import { filenameToPath, slug } from "./util";
1512

1613
async function run(): Promise<void> {
1714
try {
18-
if (!process.env.GITHUB_TOKEN) {
19-
core.setFailed(`Could not find GITHUB_TOKEN in env`);
15+
if (!process.env.GITHUB_TOKEN || !process.env.GITHUB_REF_NAME) {
16+
core.setFailed(`Could not find GITHUB_TOKEN or GITHUB_REF_NAME in env`);
2017
process.exit();
2118
}
2219

@@ -53,22 +50,12 @@ async function run(): Promise<void> {
5350
PREVIEW_URL_REGEX.test(comment.body ?? ""),
5451
);
5552

56-
const previewUrl: string = (
57-
await readFile(WRANGLER_LOGS_PATH, { encoding: "utf-8" })
58-
)
59-
.split("\n")
60-
.filter(Boolean)
61-
.map((json) => JSON.parse(json))
62-
.filter((json) => json.type === "version-upload")
63-
.map((json) => json.preview_url)
64-
.at(0);
65-
66-
if (!previewUrl) {
67-
core.setFailed(`Found no version-upload at ${WRANGLER_LOGS_PATH}`);
68-
process.exit();
69-
}
53+
const previewUrl = {
54+
branch: `https://${slug(process.env.GITHUB_REF_NAME)}.preview.developers.cloudflare.com`,
55+
commit: `https://${ctx.sha.slice(0, 8)}.preview.developers.cloudflare.com`,
56+
};
7057

71-
core.debug(previewUrl);
58+
core.debug(JSON.stringify(previewUrl));
7259

7360
const changedFiles = files
7461
.filter(
@@ -81,14 +68,14 @@ async function run(): Promise<void> {
8168
.slice(0, 15) // Limit to 15 entries
8269
.map(({ filename }) => {
8370
const original = `${DOCS_BASE_URL}/${filenameToPath(filename)}`;
84-
const preview = `${previewUrl}/${filenameToPath(filename)}`;
71+
const preview = `${previewUrl.branch}/${filenameToPath(filename)}`;
8572

8673
core.debug([filename, original, preview].toString());
8774

8875
return { original, preview };
8976
});
9077

91-
let comment = `**Preview URL:** ${previewUrl}`;
78+
let comment = `**Preview URL:** ${previewUrl.branch}`;
9279
if (changedFiles.length !== 0) {
9380
comment = comment.concat(
9481
`**Files with changes (up to 15)**\n\n| Original Link | Updated Link |\n| --- | --- |\n${changedFiles

bin/post-preview-url-comment/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ export const filenameToPath = (filename: string) => {
2929
.replace(/\/index$/, "")
3030
.concat("/");
3131
};
32+
33+
export { slug };

0 commit comments

Comments
 (0)