generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from zugdev/kv-binding-in-actions
chore: pull kv workflow into testing branches to execute
- Loading branch information
Showing
11 changed files
with
180 additions
and
35 deletions.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
WEBHOOK_PROXY_URL=https://smee.io/new | ||
APP_WEBHOOK_SECRET=xxxxxx | ||
APP_ID=123456 | ||
ENVIRONMENT=development | production |
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
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
4c675e0
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.
@zugdev Configuration is invalid.
Caution
work.ubq.fi/.github/.ubiquity-os.config.yml
Line 3 in 4c675e0
Caution
work.ubq.fi/.github/.ubiquity-os.config.yml
Line 5 in 4c675e0