Environment Variables in Templates

Hi All,

I am looking at trying to make some dynamic templates with Caddy which will be run under Docker. Ideally I want to be able to set some runtime Environment variables when starting docker and have these available to be used by the templating language.

By the looks of it test/template support {{ .Env.ENVNAME }} but if i export a variable for example KUBERNETES_VERSION, i get an error 500 when trying to render the template with the value {{ .Env.KUBERNETES_VERSION }}

Is this an option as it would make dynamic files much easier especially when testing multiple versions like we do without hardcoding changes.

Also is there any reason the templates are restricted to just a few file type? For example I can not set it to a yaml file (yml) so i have to use txt instead.



I’ve got also a use case where I need environment variables within templates. Does that work?

I’ve added support to the templates module. Pull request awaiting approval.

If you’d like to try this out now run: git clone -b template_env https://github.com/devangels/caddy.git. Go into caddy sub-directory and find build.bash. Run it from a bash shell and it’ll build a new caddy binary executable in your current directory. You can then copy it out and test locally with that.



All merged in, you should get this in 0.9.1. :+1: Thanks Simon!

1 Like

No problem. Glad to help.

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