Skip to content

Commit 4e51378

Browse files
committed
modify things for deploy
1 parent 3193611 commit 4e51378

File tree

8 files changed

+103
-75
lines changed

8 files changed

+103
-75
lines changed

.github/composite/deploy-cloudflare/action.yaml

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ runs:
5959
GITBOOK_ASSETS_PREFIX: ${{ inputs.opItem }}/GITBOOK_ASSETS_PREFIX
6060
GITBOOK_FONTS_URL: ${{ inputs.opItem }}/GITBOOK_FONTS_URL
6161
- name: Build worker
62-
run: bun run turbo build:v2:cloudflare
62+
run: bun run turbo build:v2:container
6363
env:
6464
GITBOOK_RUNTIME: cloudflare
6565
shell: bash
@@ -70,32 +70,32 @@ runs:
7070
apiToken: ${{ inputs.apiToken }}
7171
accountId: ${{ inputs.accountId }}
7272
workingDirectory: ./
73-
wranglerVersion: '4.10.0'
73+
wranglerVersion: '4.21.0'
7474
environment: ${{ inputs.environment }}
7575
command: deploy --config ./packages/gitbook-v2/openNext/customWorkers/doWrangler.jsonc
7676

77-
- id: upload_server
78-
name: Upload server to Cloudflare
79-
uses: cloudflare/[email protected]
80-
with:
81-
apiToken: ${{ inputs.apiToken }}
82-
accountId: ${{ inputs.accountId }}
83-
workingDirectory: ./
84-
wranglerVersion: '4.10.0'
85-
environment: ${{ inputs.environment }}
86-
command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc
77+
# - id: upload_server
78+
# name: Upload server to Cloudflare
79+
# uses: cloudflare/[email protected]
80+
# with:
81+
# apiToken: ${{ inputs.apiToken }}
82+
# accountId: ${{ inputs.accountId }}
83+
# workingDirectory: ./
84+
# wranglerVersion: '4.10.0'
85+
# environment: ${{ inputs.environment }}
86+
# command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc
8787

88-
- name: Extract server version worker ID
89-
shell: bash
90-
id: extract_server_version_id
91-
run: |
92-
version_id=$(echo '${{ steps.upload_server.outputs.command-output }}' | grep "Worker Version ID" | awk '{print $4}')
93-
echo "version_id=$version_id" >> $GITHUB_OUTPUT
88+
# - name: Extract server version worker ID
89+
# shell: bash
90+
# id: extract_server_version_id
91+
# run: |
92+
# version_id=$(echo '${{ steps.upload_server.outputs.command-output }}' | grep "Worker Version ID" | awk '{print $4}')
93+
# echo "version_id=$version_id" >> $GITHUB_OUTPUT
9494

95-
- name: Run updateWrangler scripts
96-
shell: bash
97-
run: |
98-
bun run ./packages/gitbook-v2/openNext/customWorkers/script/updateWrangler.ts ${{ steps.extract_server_version_id.outputs.version_id }}
95+
# - name: Run updateWrangler scripts
96+
# shell: bash
97+
# run: |
98+
# bun run ./packages/gitbook-v2/openNext/customWorkers/script/updateWrangler.ts ${{ steps.extract_server_version_id.outputs.version_id }}
9999

100100
- id: upload_middleware
101101
name: Upload middleware to Cloudflare
@@ -104,35 +104,44 @@ runs:
104104
apiToken: ${{ inputs.apiToken }}
105105
accountId: ${{ inputs.accountId }}
106106
workingDirectory: ./
107-
wranglerVersion: '4.10.0'
107+
wranglerVersion: '4.21.0'
108108
environment: ${{ inputs.environment }}
109109
command: ${{ format('versions upload --tag {0} --message "{1}"', inputs.commitTag, inputs.commitMessage) }} --config ./packages/gitbook-v2/openNext/customWorkers/middlewareWrangler.jsonc
110110

111-
- name: Extract middleware version worker ID
112-
shell: bash
113-
id: extract_middleware_version_id
114-
run: |
115-
version_id=$(echo '${{ steps.upload_middleware.outputs.command-output }}' | grep "Worker Version ID" | awk '{print $4}')
116-
echo "version_id=$version_id" >> $GITHUB_OUTPUT
117-
118-
- name: Deploy server and middleware to Cloudflare
119-
if: ${{ inputs.deploy == 'true' }}
120-
uses: ./.github/actions/gradual-deploy-cloudflare
111+
- id: deploy_container
112+
name: Deploy container to Cloudflare
113+
uses: cloudflare/[email protected]
121114
with:
122115
apiToken: ${{ inputs.apiToken }}
123116
accountId: ${{ inputs.accountId }}
124-
opServiceAccount: ${{ inputs.opServiceAccount }}
125-
opItem: ${{ inputs.opItem }}
126-
environment: ${{ inputs.environment }}
127-
serverVersionId: ${{ steps.extract_server_version_id.outputs.version_id }}
128-
middlewareVersionId: ${{ steps.extract_middleware_version_id.outputs.version_id }}
129-
deploy: ${{ inputs.deploy }}
117+
workingDirectory: ./
118+
wranglerVersion: '4.21.0'
119+
command: deploy --config ./packages/gitbook-v2/openNext/customWorkers/containerWrangler.jsonc
120+
121+
# - name: Extract middleware version worker ID
122+
# shell: bash
123+
# id: extract_middleware_version_id
124+
# run: |
125+
# version_id=$(echo '${{ steps.upload_middleware.outputs.command-output }}' | grep "Worker Version ID" | awk '{print $4}')
126+
# echo "version_id=$version_id" >> $GITHUB_OUTPUT
127+
128+
# - name: Deploy server and middleware to Cloudflare
129+
# if: ${{ inputs.deploy == 'true' }}
130+
# uses: ./.github/actions/gradual-deploy-cloudflare
131+
# with:
132+
# apiToken: ${{ inputs.apiToken }}
133+
# accountId: ${{ inputs.accountId }}
134+
# opServiceAccount: ${{ inputs.opServiceAccount }}
135+
# opItem: ${{ inputs.opItem }}
136+
# environment: ${{ inputs.environment }}
137+
# serverVersionId: ${{ steps.extract_server_version_id.outputs.version_id }}
138+
# middlewareVersionId: ${{ steps.extract_middleware_version_id.outputs.version_id }}
139+
# deploy: ${{ inputs.deploy }}
130140

131141

132142
- name: Outputs
133143
shell: bash
134144
env:
135145
DEPLOYMENT_URL: ${{ steps.upload_middleware.outputs.deployment-url }}
136146
run: |
137-
echo "URL: ${{ steps.upload_middleware.outputs.deployment-url }}"
138-
echo "Output server: ${{ steps.upload_server.outputs.command-output }}"
147+
echo "URL: ${{ steps.upload_middleware.outputs.deployment-url }}"

packages/gitbook-v2/open-next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
proxyExternalRequest: 'node',
99
generateDockerfile: true,
1010
queue: 'dummy',
11-
incrementalCache: () => import('./openNext/noOpCache').then((m) => m.default),
11+
incrementalCache: () => import('./openNext/serverCache').then((m) => m.default),
1212
tagCache: 'dummy',
1313
},
1414
},

packages/gitbook-v2/openNext/customWorkers/container.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ export default {
1515
async fetch(
1616
request: Request,
1717
env: { ON_CONTAINER: DurableObjectNamespace<OpenNextContainer> }
18-
): Promise<Response> {
18+
) {
1919
const idOne = env.ON_CONTAINER.idFromName('foo');
20-
//@ts-ignore - Seems like types are broken for now
2120
const containerInstance = env.ON_CONTAINER.get(idOne);
2221

2322
await containerInstance.start({

packages/gitbook-v2/openNext/customWorkers/containerWrangler.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "container-worker",
2+
"name": "on-container-worker",
33
"main": "container.ts",
44
"compatibility_date": "2025-05-06",
55
"compatibility_flags": ["nodejs_compat"],
@@ -10,7 +10,7 @@
1010
{
1111
"class_name": "OpenNextContainer",
1212
"image": "../../.open-next/server-functions/default/Dockerfile",
13-
"max_instances": 3,
13+
"max_instances": 1,
1414
"name": "container-open-next"
1515
}
1616
],

packages/gitbook-v2/openNext/customWorkers/middleware.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export default class extends WorkerEntrypoint {
3232
);
3333

3434
const url = new URL(request.url);
35-
console.log('Middleware URL:', url.pathname);
3635
if (url.pathname.startsWith('/_internal/')) {
3736
// Incremental cache handling
3837
if (url.pathname.startsWith('/_internal/set')) {
@@ -66,7 +65,6 @@ export default class extends WorkerEntrypoint {
6665
if (this.env.STAGE === 'dev') {
6766
const modifiedUrl = new URL(reqOrResp.url);
6867
modifiedUrl.host = this.env.PREVIEW_HOSTNAME;
69-
console.log('Redirecting to preview hostname:', modifiedUrl.toString());
7068
const nextRequest = new Request(modifiedUrl, reqOrResp);
7169
nextRequest.headers.set('x-host', reqOrResp.host);
7270
return fetch(nextRequest, {
@@ -76,27 +74,40 @@ export default class extends WorkerEntrypoint {
7674
});
7775
}
7876

79-
if (this.env.STAGE !== 'preview') {
80-
// https://developers.cloudflare.com/workers/configuration/versions-and-deployments/gradual-deployments/#version-affinity
77+
if (this.env.STAGE === 'preview') {
78+
// We just send the request to the container worker
8179
reqOrResp.headers.set(
82-
'Cloudflare-Workers-Version-Overrides',
83-
`gitbook-open-v2-${this.env.STAGE}="${this.env.WORKER_VERSION_ID}"`
80+
'x-host',
81+
reqOrResp.headers.get('host') || this.env.PREVIEW_HOSTNAME
8482
);
85-
return this.env.DEFAULT_WORKER?.fetch(reqOrResp, {
83+
return this.env.CONTAINER_WORKER?.fetch(reqOrResp, {
8684
cf: {
8785
cacheEverything: false,
8886
},
8987
});
9088
}
91-
// If we are in preview mode, we need to send the request to the preview URL
92-
const modifiedUrl = new URL(reqOrResp.url);
93-
modifiedUrl.hostname = this.env.PREVIEW_HOSTNAME;
94-
const nextRequest = new Request(modifiedUrl, reqOrResp);
95-
return fetch(nextRequest, {
96-
cf: {
97-
cacheEverything: false,
98-
},
99-
});
89+
90+
// if (this.env.STAGE !== 'preview') {
91+
// // https://developers.cloudflare.com/workers/configuration/versions-and-deployments/gradual-deployments/#version-affinity
92+
// reqOrResp.headers.set(
93+
// 'Cloudflare-Workers-Version-Overrides',
94+
// `gitbook-open-v2-${this.env.STAGE}="${this.env.WORKER_VERSION_ID}"`
95+
// );
96+
// return this.env.DEFAULT_WORKER?.fetch(reqOrResp, {
97+
// cf: {
98+
// cacheEverything: false,
99+
// },
100+
// });
101+
// }
102+
// // If we are in preview mode, we need to send the request to the preview URL
103+
// const modifiedUrl = new URL(reqOrResp.url);
104+
// modifiedUrl.hostname = this.env.PREVIEW_HOSTNAME;
105+
// const nextRequest = new Request(modifiedUrl, reqOrResp);
106+
// return fetch(nextRequest, {
107+
// cf: {
108+
// cacheEverything: false,
109+
// },
110+
// });
100111
});
101112
}
102113
}

packages/gitbook-v2/openNext/customWorkers/middlewareWrangler.jsonc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
{
7070
"binding": "DEFAULT_WORKER",
7171
"service": "gitbook-open-v2-server-preview"
72+
},
73+
{
74+
"binding": "CONTAINER_WORKER",
75+
"service": "on-container-worker"
7276
}
7377
],
7478
"durable_objects": {

packages/gitbook-v2/openNext/noOpCache.ts renamed to packages/gitbook-v2/openNext/serverCache.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@ export default {
1010
key: string,
1111
cacheType?: CacheType
1212
): Promise<WithLastModified<CacheValue<CacheType>> | null> => {
13-
console.log(`NoOpCache: get called for key: ${key}, cacheType: ${cacheType}`);
14-
const resp = await fetch(`http://${process.env.HOST || 'localhost:8771'}/_internal/get`, {
15-
method: 'POST',
16-
headers: {
17-
'Content-Type': 'application/json',
18-
},
19-
body: JSON.stringify({ key, cacheType }),
20-
});
21-
console.log(`NoOpCache: get response for key: ${key}, cacheType: ${cacheType}`, resp.status);
22-
return resp.ok ? resp.json() : null;
13+
try {
14+
const resp = await fetch(
15+
`http://${process.env.HOST || 'localhost:8771'}/_internal/get`,
16+
{
17+
method: 'POST',
18+
headers: {
19+
'Content-Type': 'application/json',
20+
},
21+
body: JSON.stringify({ key, cacheType }),
22+
}
23+
);
24+
return resp.ok ? resp.json() : null;
25+
} catch (e) {
26+
console.error('Error fetching cache:', e);
27+
return null;
28+
}
2329
},
2430
set: async <CacheType extends CacheEntryType = 'cache'>(
2531
key: string,
2632
value: CacheValue<CacheType>,
2733
cacheType?: CacheType
2834
): Promise<void> => {
29-
console.log(`NoOpCache: set called for key: ${key}, cacheType: ${cacheType}`);
3035
await fetch(`http://${process.env.HOST || 'localhost:8771'}/_internal/set`, {
3136
method: 'POST',
3237
headers: {
@@ -38,5 +43,5 @@ export default {
3843
delete: (_key: string): Promise<void> => {
3944
return Promise.resolve();
4045
},
41-
name: '',
46+
name: 'ServerContainerCache',
4247
} satisfies IncrementalCache;

packages/gitbook-v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"dev:v2:cloudflare": "wrangler dev --port 8771 --env preview",
3737
"dev:v2:cf:middleware": "wrangler dev --port 8771 --inspector-port 9230 --env dev --config ./openNext/customWorkers/middlewareWrangler.jsonc",
3838
"dev:v2:cf:server": "wrangler dev --port 8772 --env dev --config ./openNext/customWorkers/defaultWrangler.jsonc",
39-
"dev:v2:container": "node ./.open-next/server-functions/default/index.js",
39+
"dev:v2:container": "node ./.open-next/server-functions/default/index.mjs",
4040
"unit": "bun test",
4141
"typecheck": "tsc --noEmit"
4242
}

0 commit comments

Comments
 (0)