title: Substitute environment variable in all files when kubectl apply-ing
body: <p>Let's say we have a number of Kubernetes configuration files in a folder <code>kubernetes</code> and we want to apply them all:</p>
  <pre><code>kubectl apply -f kubernetes -n MyNamespace
  </code></pre>
  <p>Some of these files contain environment variables which need to be substituted first (no <a href="https://github.com/kubernetes/kubernetes/issues/23896" rel="nofollow noreferrer">templating</a> in Kubernetes). For instance, several of the deployment yamls contain something like:</p>
  <pre><code>image: myregistry.com/myrepo:$TAG
  </code></pre>
  <p>For a single yaml file, this can be <a href="https://serverfault.com/questions/791715/using-environment-variables-in-kubernetes-deployment-spec">done</a> e.g. by using envsubst like this:</p>
  <pre><code>envsubst &lt; deploy.yml | kubectl apply -f -
  </code></pre>
  <p>What's the best way to do these substitutions for all the yaml files?</p>
  <p>(Looping over the files in the folder and calling <code>envsubst</code> as above is one option, but I suspect that it would be preferrable to pass the entire folder to <code>kubectl</code> and not individual files)</p>