Demystify deployment and environment coupling of code

As a good practice, we keep a separate deployment instance=( apart from main deployment=(, so that we can test our development on qa instance before moving that code to production instance

Adding a further level of safety, the code deployment to these different instances (qa and production) also goes from different git branches ( qa and master )

For new code addition, we create a branch=(feature-1) from qa and test it locally and if it works well on local, we merge feature-1 to qa branch, now this qa branch is used to deploy code to qa deployment instance hereafter referred to as qa environment, if this works well, we will move code from qa branch to master branch and use code from the master branch to deploy to production instance

In case of web development, both frontend and backend code follow the same code deployment approach, so we want frontend code on qa instance to connect with backend API from qa instance and similarly production frontend to call production backend API

This connection logic to the backend is part of frontend code

Thus the code that connects to backend API will be different for qa and production instance of frontend, that means we can’t promote qa branch to master branch through a simple pull request

We can solve this problem in frontend code by decoupling the backend API url from the frontend code logic and couple them through new deployment code at the time of deployment as per following coupling instruction

# deployment code == coupling instruction in plain English

1. if we want the code to run on qa environment, pick the qa credentials and build the code so that it runs well on qa

2. if we want the code to run on the production environment, pick the prod credentials and build the code so that it runs well on production

because credentials and code are decoupled and gets coupled only according to our coupling instructions defined above, we can safely do below two things

A1. promote the qabranch to master branch without worrying about overriding master credentials in the production branch, since they can coexist. ( and

A2. pull mastermaster branch to qa

Thus we have explained the deployment logic used to ease the ongoing development of software

Now if in case, we are not able to decouple our credentials from code, then we have to MANUALLY take care that our coupling logic is intact while doing points A1 and A2 described above

Following is an example of one scenario and our solution approach

We have repository=Repo1 which contains page business.html

now route=/url1 in another backend, repo=API_SERVER is configured such that it renders business.htmlin both environment qa and prod

Now our requirement is that the pagebusiness.html should submit the form to two different endpoints according to the environment qa or masterand that is where credentials are coupled to code.

Now instead of jumping to the final solution let improve bit by bit

Generally, the coupling is defined in build.js, that means, build.js for qa will contain credentials of qaand build.js formaster environment will contain credentials of master, thus we can’t progress the build.js from qa to master branch seamlessly

Improvement 1

The first improvement can be to create two separate js files and and both will co-exist, we will just replace the reference in HTML as per the environment

Again we can’t do actions A1 and A2 seamlessly, because we will have to take care of proper reference in the HTML file

Improvement 2

Again we can go for improvement by passing env as query parameter from the API_SERVER like this /url1?env=qa and in the HTML we can check the env value and load either or based on the value of env

like this

<script type="application/javascript">function loadScript(file) {var jsElm = document.createElement("script");jsElm.type = "application/javascript";jsElm.src = file;document.body.appendChild(jsElm);}let env ="&").filter(x=>x.startsWith('env='))[0]?.split('=')[1];let url;switch (env) {case 'qa':url = '../js/';break;default:url = '../js/';}loadScript(url)</script>

Now we can do actions A1 and A2 seamlessly basically we have again tried to decouple the code with credentials with the help of query parameters from API_SERVER, because API server can actually send different parameters based on environment, since in our case it has that decoupling that we talked

Improvement 3

As explained in the first line of the post, our host URL are different namely and so instead of relying on query parameter from API_SERVER, we can improve our coding logic as below

<script type="application/javascript">function loadScript(file) {var jsElm = document.createElement("script");jsElm.type = "application/javascript";jsElm.src = file;document.body.appendChild(jsElm);}const env = window.location.hostlet qaUrl = "../js/qa.main.js";let prodUrl = "../js/prod.main.js";let url;switch (env) {case "":url = qaUrl;break;case ""url = prodUrl;}loadScript(url)</script>

With this improvement, we have coupling logic in HTML itself.

Let me know your views.




I love learning

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Brief Look at Dynamic Related Lists

Using TDD to build a “click counter button” in TypeScript

11 Key Factors to Consider When Hiring React Developers

RxJs Subjects: A tutorial

How to use React Final Form to Manage Form State

Next.js Visual Regression Testing with Applitools and Cypress

Build a React application from scratch

Dynamic filtering with RxJs and Angular forms — A tutorial

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Jain

Akshay Jain

I love learning

More from Medium

4 Tips to help your remote employees in a Natural Disaster

Auth0 — Add default role on User creation and load role list on User LogIn

DevOps World: About Speed and Agility

Configuring Auth0 as Key Manager for WSO2 API Manager