The env.sh
script simplifies the process of building your site based on a specific environment (--environment
) passed with the hugo
build command as an argument. This script also includes some guardrails to ensure the correct configuration file for that environment.
Hugo environments
You can use the hugo.Environment
variable to access the current environment in your templates. This is useful for conditionally loading assets or content based on the environment. See the Hugo documentation for more information.
Before you start #
- You should fill out the
config/
directory with the appropriate configuration files for each environment if you want to use this script.
Usage #
sh tools/env.sh offline
# Runs the following:
hugo server --config config/offline.yaml --environment offline
How it works #
- For
production
anddevelopment
environments, the script will use the default top-levelhugo.yaml
configuration file. - For all other environments, the script will use the
config/
directory to find the appropriate configuration file.
Default environments #
You can set the environments by updating the ENVIRONMENTS
array in the script.
development
production
offline
enterprise
opensource
Source code #
#!/bin/bash
# Configurable options
ENVIRONMENTS="production, development, offline, enterprise, opensource" # Add or remove environments as needed
# Usage message
USAGE="Usage: sh environment.sh [${ENVIRONMENTS}]"
# Check if an argument is provided
if [ $# -eq 0 ]; then
echo "No environment specified. Only use this command to start the hugo server with a specific config/{environment}.yaml file."
echo "-- ${USAGE}"
echo "-- To build all documentation, use the standard hugo cli commands (hugo, hugo server) and top-level hugo.yaml file"
exit 1
fi
# Check if the provided argument is a valid environment
if ! [[ "${ENVIRONMENTS}" =~ (^|[[:space:],])"$1"($|[[:space:],]) ]]; then
echo "Invalid environment specified."
echo "-- ${USAGE}"
exit 1
fi
# If $1 is all or ALL, do not set the HUGO_ORG environment variable
if [[ $1 == "all" ]] || [[ $1 == "ALL" ]]; then
echo "Starting Hugo server for all Docs"
hugo server
exit 0
elif [[ $1 == "offline" ]]; then
echo "Starting Hugo server for offline Docs"
hugo server --config config/offline.yaml --environment offline
exit 0
elif [[ $1 == "enterprise" ]]; then
echo "Starting Hugo server for enterprise Docs"
hugo server --config config/enterprise.yaml --environment enterprise
exit 0
elif [[ $1 == "opensource" ]]; then
echo "Starting Hugo server for opensource Docs"
hugo server --config config/open-source.yaml --environment opensource
exit 0
elif [[ $1 == "production" ]]; then
echo "Starting Hugo server for production Docs"
hugo server --environment production
exit 0
elif [[ $1 == "development" ]]; then
echo "Starting Hugo server for development Docs"
hugo server --environment development
exit 0
else
# Set the HUGO_ORG environment variable for other valid environments
export HUGO_PRODUCT=$1
export HUGO_TITLE="$1 Docs"
echo "Starting Hugo server for $HUGO_PRODUCT Docs"
hugo server
fi