Distinguish between release and test versions

Hi there!

I’m currently in the process of building my personal website, which will contain a blog section. Within that blog section, I want to allow people to comment using Disqus, which is fairly easy to achieve with Zola.

The problem is that because of the way Disqus works, you only want it to be on the released site, not on local test builds. (I assume it is similar for things like Google Analytics, but I don’t really know.) For this reason, it’d be great, if one could tell the zola build command whether or not it is building the release or the development version.

This could be implemented similarly to the base_url as an option in the config.toml with an optional --release flag to override the value from the file. The value could be a bool or a string. The former would be a fairly simple system while the later would allow users much more flexibility (perhaps a company internal and a public version). As default values, I’d suggest false or something like “test” or “debug”.

A notable workaround to this feature, that works with the current version is setting an environment variable and checking it with teras get_env() built-in function.

If you are open to a feature like this, I’d be willing to implement it myself. However, I currently don’t have the time to do so, I could start in about two months.

What do you think?

Hi, could you maybe use config.base_url to check if you’re in a release build?

{% if config.base_url = "https://mydomain.net" %}
{# Disqus related stuff #}
{% endif %}

Your local test builds should use a different base_url.

I also thought of that but forgot to mention it in the original post.

The problem you have using this approach is that then you have hardcoded the base_url in multiple positions. If you want to move the site to another base_url you have to update every single occurrence, so basically a violation of the dry principle.

Alternatively, you could store the release base_url in the extra section of the configuration, but you’d still have the value in two spots.

One other option I forgot to mention is to store the information on whether the build is a release in the configuration. However, this approach has another problem: if you want to build the site using CI, you’d have to manipulate the config before running Zola, which in my opinion is worse than using environment variables.

1 Like

I think Tera get_env is the best bet there. I don’t think there should be different zola build mode built-in

How about having -c param for zola build/serve that configures the path to the config file? That would be simple and elegant i believe

That already exists.

However, I think this is still worse than environment variables, because then you have to remember to update both files, which is a new source of possible errors.

Nice catch, did not see it in zola build --help

What about this in your build script?

sed 's/base_url = ".*"/base_url = "http:\/\/otherbase.url"/' config.toml > config.new.toml

Note sed requires escaping slashes

It’s in zola --help because it’s a parameter of the program, not the subcommand (hence the importance of the position of the argument).

While this might work, it’s way easier for me to use environment variables. This approach would require me to add an extra build step, which is not really better than adding an environment variable. (And in my opinion, the solution with the environment variable is way more readable in the CI config.)

Hugo has a --environment option. in serve mode, it default to ‘development’ and in build mode, default to ‘production’. It would be convenient if zola has this feature.

We already have something like that (zola/mod.rs at master · getzola/zola · GitHub) but it isn’t exposed to the end user

seems that expososing to end user can be easily done. would you do this?

You can use Tera’s get-env function.

In my blog I have included the following:

{% if get_env(name="env", default="dev") == "prod" %}
...
{% endif %}

This checks the env variable to see if it is equal to prod and only then includes the stuff inside the if block.

I am using Github Actions to build the site and I can easily set the env variable like so:

    - name: Build and deploy
      uses: shalzz/zola-deploy-action@master
      env:
        env: prod
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Hope this helps :slight_smile: