Home > database >  How to pass environment variable to kubernetes yaml deployment files using github actions?
How to pass environment variable to kubernetes yaml deployment files using github actions?

Time:11-30

Hi I am deploying kubernetes yaml files using github actions. For example I have below deployment yaml file

apiVersion: apps/v1
kind: Deployment 
metadata: #Dictionary
  name: cepgateway
  namespace: "#{KUBERNETES_NAMESPACE}#"

In github actions I have different jobs like deploytodev,deploytotest etc.

In deploytodev I have set env variable as below

Deploy-Dev: 
    runs-on: 'ubuntu-latest'
    environment: 'Dev'
    env:
      KUBERNETES_NAMESPACE: cep-dev

I have set KUBERNETES_NAMESPACE some value and trying to access in my deployment yaml as

namespace: "#{KUBERNETES_NAMESPACE}#"

This doesnt work and throwing error

from server for: "Kubernetes/ingress.yml": ingresses.networking.k8s.io "cep-ingress" is forbidden: User "system:serviceaccount:serviceaccounts:cep-ci-sa" cannot get resource "ingresses" in API group "networking.k8s.io" in the namespace "#{KUBERNETES_NAMESPACE}#"

May I know is this is correct way I am following? Can some one help me to fix this. Any help would be appreciated. Thanks

CodePudding user response:

you could use the envsubst command in combination with the kubectl apply step, like:

namespace.yaml

apiVersion: apps/v1
kind: Deployment 
metadata: #Dictionary
  name: cepgateway
  namespace: ${K8S_NAMESPACE}

then

export K8S_NAMESPACE="cep-dev"

so

envsubst < namespace.yaml

will print:

apiVersion: apps/v1
kind: Deployment
metadata: #Dictionary
  name: cepgateway
  namespace: cep-dev

Combining it in a GitHub Action will be something like:

jobs:
  Deploy-Dev:
    runs-on: 'ubuntu-latest'
    environment: 'Dev'
    env:
      KUBERNETES_NAMESPACE: cep-dev
    steps:
        run: |
          envsubst < namespace.yaml | kubectl apply -f -

  • Related