does CF_PAGES_COMMIT_SHA and CF_PAGES_
does CF_PAGES_COMMIT_SHA and CF_PAGES_BRANCH are missing from workers ? (i'm using the new git integration)
3 Replies
On pages, the build is (usually) running on cloudflare directly after noticing changes from a linked git repo. On workers, you are usually deploying yourself from your own machine or CI. So you must inject this info yourself if you need it.
I'd suggest injecting it at build time using the
--define
cli flag. You would access it using names that will be replaced by esbuild. These flags can be passed in to wrangler dev
and wrangler deploy
wrangler dev --define GIT_BRANCH:\"$(git branch --show-current)\" --define GIT_SHA:\"$(git rev-parse --short HEAD)\"
Note the extra quotes since it is actualyl replacing the full string
in your code you would then jsut access it as if it was a defined const.
console.log('current branch is ', GIT_BRANCH)
oh - I just noticed you mentioned the git integration 🤔 I'm not familiar but taking a look
ok so it looks like all they are doing is pulling down your code and you can override a deploy command (and a custom build if necessary)
in this case, I just set the deploy command in the build settings to do the same injection
npx wrangler deploy --define GIT_BRANCH:\"$(git branch --show-current)\" --define GIT_SHA:\"$(git rev-parse --short HEAD)\"
verified that it works
I'm also working on a tool that really simplifies dealing with config in workers in general - will share it here when it is released next week!@theo thank you. this did work
Definitely feels like something. That the platform should populate at least into the build env - although currently you’d still have to use define to pass them through into your code