Skip to content

Commit a66bac1

Browse files
CF workers overview docs (#224)
* CF workers overview docs * finish up cloudflare docs --------- Co-authored-by: Theo Ephraim <[email protected]>
1 parent b34ffce commit a66bac1

File tree

3 files changed

+185
-15
lines changed

3 files changed

+185
-15
lines changed

packages/varlock-website/astro.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default defineConfig({
9292
{ label: 'Plugins', slug: 'guides/plugins' },
9393
{ label: 'Migrate from dotenv', slug: 'guides/migrate-from-dotenv' },
9494
{ label: 'Telemetry', slug: 'guides/telemetry' },
95-
{ label: 'MCP', slug: 'guides/mcp', badge: 'new' },
95+
{ label: 'MCP', slug: 'guides/mcp' },
9696
{ label: 'AI Tools', slug: 'guides/ai-tools' },
9797
],
9898
},
@@ -101,11 +101,12 @@ export default defineConfig({
101101
items: [
102102
{ label: 'Overview', slug: 'integrations/overview' },
103103
{ label: 'JavaScript / Node.js', slug: 'integrations/javascript' },
104-
{ label: 'Bun', slug: 'integrations/bun' },
104+
{ label: 'Bun', slug: 'integrations/bun', badge: 'new' },
105105
{ label: 'Next.js', slug: 'integrations/nextjs' },
106106
{ label: 'Vite-based', slug: 'integrations/vite' },
107107
{ label: 'Astro', slug: 'integrations/astro' },
108108
{ label: 'Other languages', slug: 'integrations/other-languages' },
109+
{ label: 'Cloudflare Workers', slug: 'integrations/cloudflare', badge: 'new' },
109110
{ label: 'Docker', slug: 'guides/docker' },
110111
{ label: 'GitHub Actions', slug: 'integrations/github-action' },
111112
],
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: Cloudflare Workers
3+
description: How to integrate varlock with Cloudflare Workers and Wrangler for secure, type-safe environment management
4+
---
5+
import SyncedCodeTabs from '@/components/SyncedCodeTabs.astro'
6+
import { TabItem, Tabs, Steps } from "@astrojs/starlight/components";
7+
import Badge from '@/components/Badge.astro';
8+
import ExecCommandWidget from '@/components/ExecCommandWidget.astro';
9+
import InstallJsDepsWidget from '@/components/InstallJsDepsWidget.astro';
10+
11+
Varlock provides a robust solution for managing environment variables in Cloudflare Workers, offering validation, type safety, and security features that go beyond Cloudflare's built-in environment variable handling.
12+
13+
## Two approaches
14+
15+
There are two main ways to use varlock with Cloudflare Workers:
16+
17+
1. **With Vite plugin** (recommended) - Use the [Varlock Vite integration](/integrations/vite/) alongside the [Cloudflare Workers Vite plugin](https://developers.cloudflare.com/workers/vite-plugin/)
18+
2. **Without Vite** - Use Wrangler's `vars` and `secrets` directly
19+
20+
## Approach 1: Using the Vite plugin (recommended)
21+
22+
Using the [Cloudflare Workers Vite plugin](https://developers.cloudflare.com/workers/vite-plugin/) allows more flexiblity in the bundling process. We can then use the [Varlock Vite plugin](/integrations/vite/) to bundle resolved environment variables into your built code, making it safe and straightforward to use.
23+
24+
Even though it may feel a bit strange to use Vite on a backend-only project, it is the [recommended approach](https://developers.cloudflare.com/workers/development-testing/wrangler-vs-vite/#when-to-use-the-cloudflare-vite-plugin) by Cloudflare when you need more flexibility in your build process.
25+
26+
:::caution[Bundled secrets]
27+
**Using this method, your sensitive values will never be exposed to any client-side code.**
28+
29+
However, within the Cloudflare dashboard, your team members can view the bundled source code, and you must be mindful of sending source maps to external services.
30+
:::
31+
32+
### Setup
33+
34+
<Steps>
35+
36+
1. **Install varlock and the Vite integration package**
37+
<InstallJsDepsWidget packages="@varlock/vite-integration varlock" />
38+
39+
1. **Run `varlock init` to set up your `.env.schema` file**
40+
41+
This will guide you through setting up your `.env.schema` file, based on your existing `.env` file(s). Make sure to review it carefully.
42+
43+
<ExecCommandWidget command="varlock init" showBinary={false} />
44+
45+
1. **Enable the Vite config plugin**
46+
47+
Add the Varlock Vite plugin alongside the Cloudflare Workers Vite plugin to your `vite.config.*` file:
48+
49+
```diff lang="ts" title="vite.config.ts"
50+
import { defineConfig } from 'vite';
51+
+import { varlockVitePlugin } from '@varlock/vite-integration';
52+
53+
export default defineConfig({
54+
plugins: [
55+
+ varlockVitePlugin(),
56+
cloudflare(),
57+
// other plugins ...
58+
],
59+
});
60+
```
61+
62+
The varlock plugin will automatically detect Cloudflare Workers and use the `resolved-env` mode, which injects the fully resolved environment data into your built code.
63+
64+
</Steps>
65+
66+
### Update package.json scripts
67+
68+
If you were not already using the Vite plugin, you'll also need to update your `package.json` scripts:
69+
70+
```diff lang="json" title="package.json"
71+
{
72+
"scripts": {
73+
"dev": "vite dev",
74+
"build": "vite build",
75+
"preview": "npm run build && vite preview",
76+
"deploy": "npm run build && wrangler deploy"
77+
}
78+
}
79+
```
80+
81+
You can see more details in the [Cloudflare's Vite getting started guide](https://developers.cloudflare.com/workers/vite-plugin/get-started/).
82+
83+
84+
----
85+
86+
## Approach 2: Using Wrangler vars and secrets (without Vite)
87+
88+
For local development, you can use Wrangler's `--var` flag to pass the entire resolved env:
89+
90+
```diff lang="json" title="package.json"
91+
{
92+
"scripts": {
93+
- "dev": "wrangler dev",
94+
+ "dev": "wrangler dev --var \"__VARLOCK_ENV:$(varlock load --format json-full)\"",
95+
}
96+
}
97+
```
98+
99+
For deployments we can use the same method. Note that you may need to set your current env, either within the deploy command or infer it from the CI environemnt (see below).
100+
101+
```diff lang="json" title="package.json"
102+
{
103+
"scripts": {
104+
- "deploy": "wrangler deploy",
105+
+ "deploy": "wrangler deploy --var \"__VARLOCK_ENV:$(varlock load --format json-full)\"",
106+
}
107+
}
108+
```
109+
110+
:::caution[Team visibility]
111+
For deployments, when using `--var`, the environment variables will be visible in the Cloudflare dashboard to your team members. For higher security, consider using the secrets approach below.
112+
:::
113+
114+
### Using Cloudflare secrets
115+
116+
To attach the resolved env blob as a secret, you must use a 3-step deployment process using Wrangler's [versions commands](https://developers.cloudflare.com/workers/wrangler/commands/#versions):
117+
118+
1. Upload new deployment version of your bundled code
119+
2. Create a second version with the secret attached
120+
3. Promote the latest version to be the active deployment
121+
122+
```bash
123+
# Step 1: Create a version that's not deployed immediately
124+
# note the empty __VARLOCK_ENV so it will not reuse existing config
125+
npx wrangler versions upload --var "__VARLOCK_ENV:{}"
126+
127+
# Step 2: Attach a new secret containing the resolved env as a single JSON object
128+
echo "$(APP_ENV=prod npx varlock load --format json-full)" | npx wrangler versions secret put __VARLOCK_ENV
129+
130+
# Step 3: Activate the deployment
131+
npx wrangler versions deploy --version-id=$(npx wrangler versions list | grep -oE 'Version ID:[[:space:]]*[a-f0-9-]+' | tail -n1 | sed 's/Version ID:[[:space:]]*//') --percentage=100 --yes
132+
```
133+
134+
:::tip[Future improvement]
135+
Cloudflare is working on the ability to push secrets atomically with the deploy command. See [this pull request](https://github.com/cloudflare/workers-sdk/pull/10896) for updates. Feel free to comment and tell them it is important!
136+
:::
137+
138+
## Accessing environment variables
139+
140+
Because we are not re-emitting all env vars into the Cloudflare's vars/secrets, you must using varlock's `ENV` object instead of Cloudflare's built-in environment variable access:
141+
142+
```ts title="src/index.ts"
143+
// ❌ Do not use Cloudflare's built-in env
144+
import { env } from "cloudflare:workers";
145+
console.log(env.API_KEY);
146+
147+
// ✅ Recommended - uses varlock's ENV
148+
import { ENV } from 'varlock/env';
149+
console.log(ENV.API_KEY);
150+
```
151+
152+
---
153+
154+
## Managing Multiple Environments
155+
156+
Varlock can load multiple _environment-specific_ `.env` files (e.g., `.env.development`, `.env.preview`, `.env.production`) by using the [`@currentEnv` root decorator](/reference/root-decorators/#currentenv).
157+
158+
If you are using Cloudflare's CI, you can use the current branch name (`WORKERS_CI_BRANCH`) to determine the environment:
159+
160+
```env-spec title=".env.schema"
161+
# @currentEnv=$APP_ENV
162+
# ---
163+
WORKERS_CI_BRANCH=
164+
# @type=enum(development, preview, production, test)
165+
APP_ENV=remap($WORKERS_CI_BRANCH, production="main", preview=regex(.*), development=undefined)
166+
```
167+
168+
For more information, see the [environments guide](/guides/environments).

packages/varlock-website/src/content/docs/integrations/overview.mdx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ description: How Varlock integrates with popular frameworks, bundlers, and runti
66
Varlock ships with official integrations that wire the CLI, runtime helpers, and framework-specific plugins together so your configuration is validated, type-safe, and protected everywhere it runs.
77

88
Integrations allow you to:
9-
- load and validate `.env` files automatically during dev and use them in CI/CD and production.
10-
- inject config into your code at build or request time.
11-
- enable runtime protections such as leak prevention, and log redaction.
9+
- load and validate `.env` files automatically during dev and use them in CI/CD and production
10+
- inject config into your code at build or request time
11+
- enable runtime protections such as leak prevention, and log redaction
1212

1313
## Official integrations
14-
- [JavaScript / Node.js](/integrations/javascript/) — use `varlock` in custom toolchains, scripts, and servers.
15-
- [Next.js](/integrations/nextjs/) — drop-in replacement for `@next/env`.
16-
- [Vite](/integrations/vite/) — Vite plugin that validates and replaces at build time.
17-
- [Qwik](/integrations/vite/) — Use the Vite integration.
18-
- [React Router](/integrations/vite/) — Use the Vite integration.
19-
- [Cloudflare Workers](/integrations/vite/) — Use the Vite intregration + Cloudflare Vite plugin.
20-
- [Astro](/integrations/astro/) — Astro integration built on top of our Vite plugin.
21-
- [GitHub Actions](/integrations/github-action/) — Validate your `.env.schema` in GitHub Actions workflows
22-
- [Other languages](/integrations/other-languages/) — guidance for piping varlock output into non-JS runtimes.
23-
- [Docker](/guides/docker/) — A Docker wrapper around the varlock CLI including examples.
14+
- [JavaScript / Node.js](/integrations/javascript/) — use `varlock` in custom toolchains, scripts, and servers
15+
- [Bun](/integrations/bun/) — instructions to set up Varlock with Bun
16+
- [Next.js](/integrations/nextjs/) — drop-in replacement for `@next/env`
17+
- [Vite](/integrations/vite/) — Vite plugin that validates and replaces at build time
18+
- [Qwik](/integrations/vite/) — use the Vite integration
19+
- [React Router](/integrations/vite/) — use the Vite integration
20+
- [Cloudflare Workers](/integrations/cloudflare/) — use the Vite integration or Wrangler vars/secrets directly
21+
- [Astro](/integrations/astro/) — Astro integration built on top of our Vite plugin
22+
- [GitHub Actions](/integrations/github-action/) — validate your `.env.schema` in GitHub Actions workflows
23+
- [Other languages](/integrations/other-languages/) — guidance for piping varlock output into non-JS runtimes
24+
- [Docker](/guides/docker/) — a Docker wrapper around the varlock CLI including examples
2425

2526

2627
## Coming soon

0 commit comments

Comments
 (0)