generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
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
feat: add kv binding in actions for referral tracker #163
Open
zugdev
wants to merge
13
commits into
ubiquity:development
Choose a base branch
from
zugdev:kv-binding-in-actions
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1b62499
chore: yarn add toml
zugdev 9ddee03
feat: add a binding for the referral tracker
zugdev 9308fb7
chore: remove wrong settings from wrangler.toml
zugdev 1fedc4f
feat: use normal deploy job
zugdev 2f210ab
chore: yarn lock
zugdev 8d234e8
Merge branch 'development' of https://github.com/ubiquity/work.ubq.fi…
zugdev 292f91b
chore: lint
zugdev a7a84ab
chore: yarn lock
zugdev 2330a5a
chore: add env to wrangler.toml
zugdev 9cf32bc
chore: update wrangler.toml
zugdev 3bff790
feat: rm kv from toml
zugdev a3b1a31
chore: merge
zugdev 76a9cf6
feat: fix wrangler.toml config
zugdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
SUPABASE_URL="" | ||
SUPABASE_KEY="" | ||
VOYAGEAI_API_KEY="" | ||
VOYAGEAI_API_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ node_modules | |
.pnp.loader.mjs | ||
static/dist | ||
.env | ||
.dev.vars | ||
.wrangler/ | ||
|
||
cypress/screenshots | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// This file is a fork from: https://github.com/ubiquity-os/ubiquity-os-kernel | ||
|
||
/** | ||
* The purpose of the script is to ensure that the KV for the worker is properly set on deployment. | ||
* There is currently a bug that makes the environment reset on each deploy, because of a problem with Wrangler not | ||
* parsing the TOML configuration properly. See https://github.com/cloudflare/workers-sdk/issues/5634 | ||
* It seems to only work when the values are set at the root of the TOML, not withing the environments. | ||
* This scripts takes out the Production values for kv_namespaces and rewrites them at the root of the TOML file. | ||
*/ | ||
|
||
import { execSync } from "child_process"; | ||
import * as fs from "fs"; | ||
import * as toml from "toml"; | ||
// @ts-expect-error No typings exist for this package | ||
import * as tomlify from "tomlify-j0.4"; | ||
|
||
const tomlFilePath = "./wrangler.toml"; | ||
const wranglerToml: WranglerConfiguration = toml.parse(fs.readFileSync(tomlFilePath, "utf-8")); | ||
|
||
const NAMESPACE_TITLE = "kv"; | ||
const NAMESPACE_TITLE_WITH_PREFIX = `${wranglerToml.name}-${NAMESPACE_TITLE}`; | ||
const BINDING_NAME = "REFERRAL_TRACKING"; | ||
|
||
interface Namespace { | ||
id: string; | ||
title: string; | ||
} | ||
|
||
interface WranglerConfiguration { | ||
name: string; | ||
env: { | ||
production: { | ||
kv_namespaces?: { | ||
id: string; | ||
binding: string; | ||
}[]; | ||
}; | ||
dev: { | ||
kv_namespaces?: { | ||
id: string; | ||
binding: string; | ||
}[]; | ||
}; | ||
}; | ||
kv_namespaces: { | ||
id: string; | ||
binding: string; | ||
}[]; | ||
} | ||
|
||
function updateWranglerToml(namespaceId: string) { | ||
// Ensure kv_namespaces array exists | ||
if (!wranglerToml.kv_namespaces) { | ||
wranglerToml.kv_namespaces = []; | ||
} | ||
if (wranglerToml.env.production.kv_namespaces) { | ||
wranglerToml.kv_namespaces = wranglerToml.env.production.kv_namespaces; | ||
delete wranglerToml.env.production.kv_namespaces; | ||
} | ||
if (wranglerToml.env.dev.kv_namespaces) { | ||
delete wranglerToml.env.dev.kv_namespaces; | ||
} | ||
|
||
const existingNamespace = wranglerToml.kv_namespaces.find((o) => o.binding === BINDING_NAME); | ||
if (existingNamespace) { | ||
existingNamespace.id = namespaceId; | ||
} else { | ||
wranglerToml.kv_namespaces.push({ | ||
binding: BINDING_NAME, | ||
id: namespaceId, | ||
}); | ||
} | ||
|
||
fs.writeFileSync(tomlFilePath, tomlify.toToml(wranglerToml, { space: 1 })); | ||
} | ||
|
||
async function main() { | ||
// Check if the namespace exists or create a new one | ||
let namespaceId: string; | ||
try { | ||
const res = execSync(`wrangler kv namespace create ${NAMESPACE_TITLE}`).toString(); | ||
console.log(res); | ||
const newId = res.match(/id = \s*"([^"]+)"/)?.[1]; | ||
if (!newId) { | ||
throw new Error(`The new ID could not be found.`); | ||
} | ||
namespaceId = newId; | ||
console.log(`Namespace created with ID: ${namespaceId}`); | ||
} catch (error) { | ||
console.error(error); | ||
const listOutput = JSON.parse(execSync(`wrangler kv namespace list`).toString()) as Namespace[]; | ||
const existingNamespace = listOutput.find((o) => o.title === NAMESPACE_TITLE_WITH_PREFIX); | ||
if (!existingNamespace) { | ||
throw new Error(`Error creating namespace: ${error}`); | ||
} | ||
namespaceId = existingNamespace.id; | ||
console.log(`Namespace ${NAMESPACE_TITLE_WITH_PREFIX} already exists with ID: ${namespaceId}`); | ||
} | ||
|
||
updateWranglerToml(namespaceId); | ||
} | ||
|
||
main() | ||
.then(() => console.log("Successfully bound namespace.")) | ||
.catch((e) => { | ||
console.error("Error checking or creating namespace:\n", e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what we are trying to resolve, it should not be here, the deployment will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this was what we did at kernel
https://github.com/ubiquity-os/ubiquity-os-kernel/blob/development/wrangler.toml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We gotta have the production fields else update toml fails cause it doesnt find a field to change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The kernel does not use any of these, they actually should get removed.
Yes but I think the fields can be empty, like for plugins. Example: https://github.com/ubiquity-os-marketplace/command-start-stop/blob/development/wrangler.toml