Worker "version" as environment variable

In the audit log, I see this when I publish a worker:

{
  "Script tag": "0e2763cbf4f59ab2ccb5ce734a940632",
  "Version tag": "d318734b512b43591c2393450ec1df24"
}

I’d love to have one or both of those available as environment variables in my worker. That way, I can include it as a header and when tracking exceptions (e.g. to Bugsnag or New Relic).

As it is, I can use a custom environment variable, but I have to remember to update it every time I publish a script.

Hey,

I would recommend just using your own, if you wanted to find which version a hash is it would be a pain. Going through all the audit log entries doesn’t sound fun.

I’d recommend instead adding the commit hash into the wrangler.toml when you deploy in your CI/CD pipeline. That way you have an identifiable thing you can use to find the version of the Worker.

Something like this:

commit_hash=$(git rev-parse HEAD)
echo "[vars]" >> wrangler.toml
echo "COMMIT_HASH = $commit_hash" >> wrangler.toml

and then in your Worker COMMIT_HASH or env.COMMIT_HASH depending which format you use.

1 Like

As I only use version, I import in the headers this way:

import { version } from "../package.json";

The bundle with WebPack

2 Likes

commit_hash=$(git rev-parse HEAD)

That won’t work when trying out uncommitted changes, but it would if we also put a date "+%Y-%m-%dT%H:%M:%S" at the front. Great suggestion!

And since I’m using Webpack, I can use EnvironmentPlugin to define them without modifying the wrangler.toml.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.