Apologies if this has been raised before but I couldn't find it in searching through historic issues. We are following a Github flow model where changes are deployed to a "feature branch" environment before merging into master whereupon changes are deployed to production. We name these feature branch environments using the branch name a developer has chosen, so the feature branch environment becomes available at www-feature-abc.ournamespace.workers.dev.
We would very much like to check in our wrangler.toml file but currently can't do so because we don't know the name of the environment ahead of time. Currently we have a script that runs before running wrangler publish that regenerates the wrangler.toml, it's quick and dirty but it works:
// scripts/configure.js
const projectName = process.env.CF_PROJECT_NAME
const wranglerConfig = `
name = "${projectName}"
type = "webpack"
account_id = "xxx"
workers_dev = true
kv-namespaces = [ ... ]
[env.prod]
zone_id = "xxx"
route = "www.example.com/*"
kv-namespaces = [ ... ]
`
fs.writeFileSync('wrangler.toml', wranglerConfig)
Deploying a feature branch environment:
export CF_PROJECT_NAME=www-$(echo $CIRCLE_BRANCH | sed s'/[/.]/-/g' | tr '[:upper:]' '[:lower:]')
node scripts/configure.js
npx wrangler publish
Deploying to prod:
export CF_PROJECT_NAME=www
node scripts/configure.js
npx wrangler publish --env prod
Toml file interpolates environment variables:
name = "whatever"
type = "webpack"
account_id = "xxx"
[env.preview]
workers_dev = true
# Name is derived from BRANCH_NAME_SLUG env var
name = "${BRANCH_NAME_SLUG}"
kv-namespaces = [ ]
# Works for env vars too
vars = { FOO = "${BAR}" }
The two env vars BRANCH_NAME_SLUG and BAR would be read and inserted into the files contents as string literals. Missing environment variables would simply be inserted as empty strings but result in a warning in the build output in case that is unexpected.
Being able to specify a toml file would help. We could use a separate generated toml file for feature branch environments then check in the toml file for the static environments https://github.com/cloudflare/wrangler/issues/1064. We'd still have to do the generation ourselves though.
Hey so good news! We already let you specify any string in the toml as an environment variable with the CF_ prefix. So, for name it would be CF_NAME = "prod". That should be a pretty workable mid-term solution for ya.
This is definitely an interesting proposal for something more long term and might even help with something like #1282 and/or #1229
cc @ashleymichal @ispivey @rita3ko - this is an interesting one
I stumbled on this issue when trying to decide whether to check in the wrangler.toml in an open source project. My goal is to have the Wrangler Github Action publish the worker each time the source code updates.
After reading this community post: Is it safe to publish wrangler.toml. I feel there is risk in exposing account_id and zone_id in a public repo. Would you recommend using the CF_VAR_NAME to pass in the account and zone ids at Github Action runtime? Or is there a better way to safely parameterize the Github Action?
Hey @chuanqisun - there is no issue with putting your account and zone id in version control, those IDs are safe to be public.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
closing this as fixed by #1350
Most helpful comment
Hey so good news! We already let you specify any string in the toml as an environment variable with the
CF_prefix. So, for name it would beCF_NAME = "prod". That should be a pretty workable mid-term solution for ya.This is definitely an interesting proposal for something more long term and might even help with something like #1282 and/or #1229
cc @ashleymichal @ispivey @rita3ko - this is an interesting one