Env Based Namespace Resolution for Dynamic Deployments

Hello Everybody.

Introduction

In recent weeks we have seen requests coming in from our customers on leveraging Harness variables to make the process of deployments to namespaces dynamic.
This article talks about how we can set a dynamic variable for the Namespace field in the Infra Definition of an Env so the deployments are always dynamic.

Summary

  • The preliminary requirement is to create a Pipeline which includes a Templatised Workflow where the Service and Env details aren’t defined until runtime and are defined at the Pipeline Stage level.

  • In this case the value of the namespace we want to deploy to depends on the Env itself with some appended value. Taking a look at the Workflow which is templatised, we can see that the variable Value for the Env shows up like ${Environment} which is a Harness generate variable

  • It would be simpler to just paste the variable ${Environment}-test in the Namespace field of the Infra Definition as this resolved to the Env we select during runtime at the workflow level, but the issue here is that the variable ${Environment} resolves to the ID of the Env and not the name so it will not work as expected.

In order to support this, we need to pass the value of the namespace during Initialisation step for the Pipeline to pass the Env name and populate the namespace accordingly :

  • We need to start by creating a new Workflow in the application which is Templatised and has a shell script step. As part of this shell script step we will be adding the below details in it where we are creating a var called name and the value for that is the ${env.name}

  • Next we go to the Env Internal and in the Infra Def provide the value of the context variable as ${context.client.name}

  • Then at the Pipeline level we will add a new stage which is the Initialise stage that will act as the first stage and this Stage will execute the Workflow we created earlier with the shell script

This way, when you run the Pipeline after selecting the Env Name, the name of the client will resolve at the Namespace level with the shell script and give you the desired result.

1 Like