hermet hermet SSR. Islands. Rust.

Deploying

Making your Worker available on Cloudflare is called deploying. hermet deploy sends data directly to the Cloudflare API. This needs neither Node.js nor Wrangler.

Check before you deploy

Command What it checks
hermet check Checks the configuration, templates, CSS, and Client Island JavaScript. To also compile the site's own Rust code, add --full.
hermet check --full Runs cargo check in addition to the usual checks. It also catches misspelled template fields and similar mistakes.
hermet check --deploy In addition to the usual checks, checks what is needed to deploy a Worker that matches the configuration. The check runs on your machine.
hermet check --mode <name> Checks the configuration of the given mode, and also runs the checks before a deploy. If you leave out the mode, it evaluates the registered modes.
hermet check --format json Returns diagnostic codes, file names, mode names and so on as JSON. When the config key or the fix is known, it is included too.

Exit codes: 0 for no problems, 1 for problems found, 2 for invalid arguments and the like.

How a deploy runs

# Set CLOUDFLARE_API_TOKEN first, then run
hermet check --deploy          # check that it can be deployed
hermet deploy                  # build and deploy; prints the workers.dev URL on success
  1. Runs the same checks as hermet check --deploy. If it finds a problem, it stops before writing anything to Cloudflare
  2. Builds (a site with static generation also renders its pages here)
  3. Checks the public files (.hermet/public)
  4. Uploads the static files (only those that Cloudflare does not have yet)
  5. Uploads the Worker
  6. Sets the cron triggers
  7. Sets up workers.dev and the preview URLs

The account to deploy to is chosen in this order: the cloudflare.rs setting Config::account_id, the environment variable CLOUDFLARE_ACCOUNT_ID, then the only account that the token can see. If no single account can be found, the deploy stops.

If it fails partway, look at the printed result to see which steps were applied. Fix the cause, then run the same command again.

Separate environments (production and development)

  • In cloudflare.rs, write one function per mode. default is the configuration used when you deploy without a mode. If your site uses default for production, add a development configuration such as .mode("dev", dev) and give it a different Worker name. check, build, dev, deploy, secret put and d1 migrate all take --mode <name> to choose the mode.
  • Nothing is inherited between modes. Put the shared parts in an ordinary Rust function and call it from both modes.
  • Only the function of the chosen mode runs. When you use a development mode, you do not need the production environment variables.
  • To take a value from an environment variable, use ctx.require_env("NAME")?. If the variable is not set, it stops and says which variable is missing. If you prepare separate D1 and KV for development, you do not damage production data.
  • hermet check without a mode reads every mode. It also checks that storage with the same name is not a different kind in different modes. If a mode cannot be read only because environment variables are missing, it warns that it did not read that mode.
  • Each build writes .hermet/wrangler/<mode>.json for that mode. It is the file you pass to wrangler dev to run the site locally (hermet dev does not use it).

Secrets

printf '%s' "$VALUE" | hermet secret put ADMIN_NAME
# to choose a mode
printf '%s' "$VALUE" | hermet secret put ADMIN_NAME --mode dev
  • Set secrets after the first deploy. Pass the value on standard input (a value written as an argument stays in your shell history).
  • Redeploying does not delete secrets. In code, read them from deps.bindings() with secret.
  • For local runs with hermet dev, write the values in .dev.vars in the site folder, one NAME=value per line. Do not commit this file to Git (hermet new lists it in the .gitignore it creates).

Create D1 tables (hermet d1 migrate)

hermet d1 migrate DB --plan   # shows which files it applies to which database; writes nothing
hermet d1 migrate DB          # applies the SQL files not applied yet, in number order
  • Put the SQL files, numbered, in db/migrations/DB/ (DB is the binding name you gave in cloudflare.rs).
  • It does not create the database itself. Create the database first in the Cloudflare dashboard or with the API, and write its ID in cloudflare.rs.
  • It uses neither Node.js nor Wrangler. It needs CLOUDFLARE_API_TOKEN.
  • Locally, hermet dev applies the same files to local storage when it starts, and when it detects a change and rebuilds (no token needed).

Separate build and deploy in CI

CI (continuous integration) runs builds and checks automatically, for example with GitHub Actions. If you put build and deploy in separate jobs, you can give the Cloudflare token to the deploy job only. The deploy job uses --no-build and deploys what was built earlier. The example below uses default. To choose another mode, pass the same --mode to both jobs.

# 1. Build job (no token; sends nothing to Cloudflare)
hermet build

# 2. Deploy job (with the token; does not build)
hermet deploy --no-build
  • hermet build writes the build record (.hermet/build-record.json) only when the checks, the build (static generation included) and the checks of the output all pass. It deletes the previous record when it starts, so a failed build leaves no record.
  • Before it sends anything to Cloudflare, hermet deploy --no-build compares what is on your machine with the record. If the mode, the configuration, the Worker files or the public files differ, it stops without sending anything. It sends exactly what it compared.
  • Hand over build/ and .hermet/build-record.json between the jobs. For a site with public files, also hand over .hermet/public/.
  • Give the deploy job the same environment variables as the build (the ones that cloudflare.rs reads with require_env). If a value differs, the configuration changes, and the comparison stops the deploy.
  • GitHub Actions upload-artifact skips folders whose names start with ., so set include-hidden-files: true.
Handling the API token

Pass the token in the environment variable CLOUDFLARE_API_TOKEN. In CI, store it as a secret and assign it to an environment variable of the deploy job. In Bash on your machine, you can set the value without showing what you type, as below. Do not save the token in a file or in Git.

read -rsp "Cloudflare API token: " CLOUDFLARE_API_TOKEN
export CLOUDFLARE_API_TOKEN
echo

Processes that hermet starts (loading cloudflare.rs, the site's build.rs, cargo check, worker-build, static generation) do not receive environment variables starting with CLOUDFLARE_, nor CF_API_TOKEN, CF_ACCOUNT_ID, CF_API_KEY or CF_EMAIL. This keeps the site's code from reading the token.