0

I am trying to export an env variable in make to use in on the next lines. I am doing as suggested here Setting environment variables in a makefile.

eks-apps:
    export KUBECONFIG=$(CURDIR)/terraform/kubernetes/cluster/$(shell ls terraform/kubernetes/cluster/ | grep kubeconfig)
    kubectl get all

But its not using that kubeconfig in the kubectl command. What am I missing?

The Fool
  • 16,715
  • 5
  • 52
  • 86

1 Answers1

2

Every line in a recipe will be executed in a new shell. As this you have to use a single shell for all your commands:

eks-apps:
    ( \
    export KUBECONFIG=$(CURDIR)/terraform/kubernetes/cluster/$(shell ls terraform/kubernetes/cluster/ | grep kubeconfig); \
    kubectl get all \
    )

From the answer you are linked to:

Please note: this implies that setting shell variables and invoking shell commands such as cd that set a context local to each process will not affect the following lines in the recipe.2 If you want to use cd to affect the next statement, put both statements in a single recipe line

Klaus
  • 24,205
  • 7
  • 58
  • 113
  • But this is explicitly what the linked answer is claiming is not required. – The Fool Dec 24 '21 at 11:57
  • Ah no, I i think I misread that. You are right. – The Fool Dec 24 '21 at 11:58
  • This will work fine but a few notes: You don't need the `( ... )` around the command because every line already runs in a shell, and you should probably prefer using `&&` between your commands instead of `;` so that if the first line fails it won't try to run the next one. – MadScientist Dec 24 '21 at 20:31