Dynamic global variable

Not included? You mean they are not in the same process? If so none of these will work. You can’t set a variable at runtime from a twig template. These do not persist back to the filesystem or cache.

You really will need to write a plugin to achieve this. Something that actually sets the variable then writes it back to the filesystem, or rethink your strategy because this is just not a standard thing to do.