If else with env variables and files


I would like to have an if else statement based on environment variables and files.

Caddy version 2 on Docker (alpine)

So the code I’d like to be something like this:

if {env.CADDY_CONFIG_FILE} is "something1" {
   http://, https://
   tls internal {
} elif {env.CADDY_CONFIG_FILE} is "something2" {
} elif {env.CADDY_CONFIG_FILE} is "somethinge" {
  {{if eq . "f1.key"}} [how can an add an and here ... as i'd like to test if f1.pem file exists as well]
     tls f1.key f1.pem
    http://, https://
    tls internal {

Is this possible?
How can I add an if "1" AND "2" then as I'd like to check if f1 pem and key exists?


You’ll need to use an external templating tool to do that. There’s no config conditionals in the Caddyfile.

FYI, keep in mind that {env.*} syntax is evaluated at runtime, whereas {$ENV_VAR} syntax is evaluated as config adapt time (i.e. when Caddy transforms your Caddyfile into a JSON config that it actually runs with).

The env var values can be actual Caddyfile config tokens, and multi-line should be supported. So you can just set the environment variables to be the actual config bits you need, if that’s simpler for you.

But otherwise, use some templating library/tool of your choice instead.


This topic was automatically closed after 30 days. New replies are no longer allowed.