<p>Is there a way to set the values of some keys in a Knative <code>service.yaml</code> file using environment variables?</p>
<hr />
<p><strong>More detail</strong></p>
<p>I am trying to deploy a Knative service to a Kubernetes cluster using GitLab CI. Some of the variables in my <code>service.yaml</code> file depend on the project and environment of the GitLab CI pipeline. Is there a way I can seamlessly plug those values into my <code>service.yaml</code> file without resorting to hacks like <code>sed -i ...</code>?</p>
<p>For example, given the following script, I want the <code>$(KUBE_NAMESPACE)</code>, <code>$(CI_ENVIRONMENT_SLUG)</code>, and <code>$(CI_PROJECT_PATH_SLUG)</code> values to be replaced by accordingly-named environment variables.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: design
  namespace: &quot;$(KUBE_NAMESPACE)&quot;
spec:
  template:
    metadata:
      name: design-v1
      annotations:
        app.gitlab.com/env: &quot;$(CI_ENVIRONMENT_SLUG)&quot;
        app.gitlab.com/app: &quot;$(CI_PROJECT_PATH_SLUG)&quot;
    spec:
      containers:
        - name: user-container
          image: ...
      timeoutSeconds: 600
      containerConcurrency: 8
</code></pre>

<p>I don't think there is a great way to expand environment variables inside of an existing yaml, but if you don't want to use sed, you might be able to use <a href="https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html" rel="nofollow noreferrer">envsubst</a>:</p>
<pre><code>envsubst &lt; original.yaml &gt; modified.yaml
</code></pre>
<p>You would just run this command before you use the yaml to expand the environment variables contained within it.</p>
<p>Also I think you'll need your variables to use curly braces, instead of parentheses, like this: <code>${KUBE_NAMESPACE}</code>.</p>
<p>EDIT: You might also be able to use this inline like this: <code>kubectl apply -f &lt;(envsubst &lt; service.yaml)</code></p>
