<p>I would like to specify dependent environment variables on a Cloud Run service.</p>
<p>If the environment variables have been defined in a <code>.env</code> file it would look like this</p>
<pre><code>DATABASE_NAME=my-database
DATABASE_USER=root
DATABASE_PASSWORD=P4SSw0rd!
DATABASE_PORT=5432
DATABASE_HOST=&quot;/socket/my-database-socket&quot;
DATABASE_URL=&quot;user=${DATABASE_USER} password=${DATABASE_PASSWORD} dbname=${DATABASE_NAME} host=${DATABASE_HOST}&quot;
</code></pre>
<p>In this example, <code>DATABASE_URL</code> depends on every other environment variables.</p>
<p>To deploy the service I run the following command:</p>
<pre><code>gcloud run deploy my-service \
--image gcr.io/my-project/my-image:latest \
--region europe-west1 \
--port 80 \
--platform managed \
--allow-unauthenticated \
--set-env-vars 'DATABASE_NAME=my-database' \
--set-env-vars 'DATABASE_USER=root' \
--set-env-vars 'DATABASE_PASSWORD=P4SSw0rd!' \
--set-env-vars 'DATABASE_PORT=5432' \
--set-env-vars 'DATABASE_HOST=&quot;/socket/my-database-socket&quot;' \
--set-env-vars 'DATABASE_URL=&quot;user=$(DATABASE_USER) password=$(DATABASE_PASSWORD) dbname=$(DATABASE_NAME) host=$(DATABASE_HOST)&quot;'
</code></pre>
<p>Here is the created YAML definition of the service (some values are omitted)</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: my-service
spec:
  template:
    metadata:
      name: ...
    spec:
      containerConcurrency: 80
      timeoutSeconds: 300
      containers:
      - image: ...
        ports:
        - name: http1
          containerPort: 80
        env:
        - name: DATABASE_NAME
          value: my-database
        - name: DATABASE_USER
          value: root
        - name: DATABASE_PASSWORD
          value: P4SSw0rd!
        - name: DATABASE_HOST
          value: /socket/my-database-socket
        - name: DATABASE_URL
          value: user=$(DATABASE_USER) password=$(DATABASE_PASSWORD) dbname=$(DATABASE_NAME) host=$(DATABASE_HOST)

</code></pre>
<p>The problem is that when the service is running, the env vars in <code>DATABASE_URL</code> seem not interpolated.</p>
<p>I read that <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-interdependent-environment-variables/" rel="nofollow noreferrer">Kubernetes supports dependent env vars</a> but I can't figure out how to make this run in Cloud Run.</p>
<p>I am wondering if it is supported in Cloud Run in the end.</p>

Benjamin Grandfond
<p>It's likely this may work in Knative open source (which uses Kubernetes to execute pods) but not on Google Cloud Run (fully hosted), which runs on a proprietary execution engine.</p>
