How to use Argo CD to pass environment variables to deploy in kubernetes

2/18/2022

We are using argo cd and kubernetes.

And I want to use environmental variables in the yaml file.

For example,

apiVersion: v1
kind: Service
metadata:
  name: guestbook-ui
  annotations:
spec:
  ports:
    - port: $PORT
      targetPort: $TARGET_PORT
  selector:
    app: guestbook-ui

I want to set the value of the environmental variable (PORT and TARGET_PORT) when deploying it to Argo CD.

What should I do?

-- Junseok Lee
argocd
development-environment
kubernetes

1 Answer

2/18/2022

I'd recommend converting your raw YAML to a Helm chart and templating the relevant fields.

Argo CD has an example Helm app with a service similar to yours.

You could define a service like this:

apiVersion: v1
kind: Service
metadata:
  name: guestbook-ui
  annotations:
spec:
  ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.targetPort }}
  selector:
    app: guestbook-ui

And then define your port and targetPort parameters in Argo CD.

-- crenshaw-dev
Source: StackOverflow