Lightweight templating with envsubst

Sometimes you need to substitute values in a structured text file in your CI pipeline based on some dynamic values e.g. environment variables.

There are many ways to do this, but one lightweight approach is to use the util envsubst from the GNU gettext package.

envsubst may already be installed on your machine or CI runner.

Example

Let’s say I want to provide runtime config for a web frontend. I’ll do this by importing a .js file that adds the config to the window.

My config is going to be different in each stage of my deployment pipeline, so I need to generate this file based on environment variables.

Here is my javascript template:

// config-template.js
window.myAppConfig = {
  apiUrl: '${MY_API_URL}',
  authUrl: '${MY_AUTH_URL}',
}

envsubst will subsitute the ${MY_API_URL} and ${MY_AUTH_URL} markers with matching environment variables.

export MY_API_URL=https://example.com export MY_AUTH_URL=https://auth.example.com
envsubst < config-template.js

Outputting the following:

window.myAppConfig = {
  apiUrl: 'https://example.com',
  authUrl: 'https://auth.example.com',
}

Install

In case envsubst isn’t already installed on your machine/CI pipeline, install it.

Ubuntu:

apt-get update
apt-get install gettext

macOS:

brew install gettext