Deploy Your MERN Stack App to Azure with GitHub Actions: A Step-by-Step Guide

π Cloud Enthusiast | Turning ideas into scalable solutions βοΈ | Passionate about shaping the future of tech with cloud computing π | Always exploring the next big thing in the digital sky β¨"
Introduction
The MERN stack β MongoDB, Express.js, React, and Node.js has become one of the most popular frameworks for building modern, full-stack web applications. While MERN makes development seamless, deploying a production-ready application can be challenging without the right automation practices.
Thatβs where Microsoft Azure and GitHub Actions come in. By combining the scalability of Azure with the power of GitHubβs CI/CD pipelines, you can automate the entire deployment process from pushing code to GitHub, to building, testing, and deploying your MERN app to the cloud.
In this guide, Iβll Walk you through how to deploy a full-stack MERN application to Azure using GitHub Actions. By the end of this tutorial, you will have:
A live MERN application running on Azure
A fully automated CI/CD pipeline that deploys every new code push
Secure handling of environment variables and database credentials
A clear understanding of how to integrate MongoDB Atlas with Azure services
Whether youβre a beginner exploring cloud deployments or a developer aiming to streamline production workflows, this step-by-step approach will give you the confidence to run and scale MERN applications in the cloud.
Pre-requisites
Before we begin, make sure you have the following set up:
Microsoft Azure Account
Sign up here: Azure Free Account
Students can get $100 free credits with their student email ID β Azure for Students
Access the Azure Portal: portal.azure.com
Git & GitHub Account
Node.js & npm(for local testing)
- Download and install: Node.js
MongoDB Atlas Account (for cloud database hosting)
Sign up here: MongoDB Atlas
Make sure to whitelist your IPs under Network Access Settings.
(For production, restrict access to only trusted services. For this demo, you can allow all IPs
0.0.0.0/0for easier setup.)
Azure CLI (for managing Azure resources from your terminal)
- Install here: Azure CLI Installation Guide
Basic Knowledge of:
React (Frontend)
Express.js & Node.js (Backend)
Git & GitHub Actions (Version Control + CI/CD)
Step-by-Step Deployment
STEP 1) Clone the Repository
First, clone your MERN project (or a demo repo):
git clone <your-repo-url>
cd <your-project-folder>
STEP 2) Understand the Project Structure
A typical MERN project looks like this:
/app
βββ /client (React frontend)
βββ /server (Express backend)
βββ README.md
Frontend: React (served via Azure Static Web Apps)
Backend: Express.js (deployed as Azure Web App)
Database: MongoDB Atlas.

STEP 3) Setup MongoDB Atlas
Create an account at MongoDB Atlas.
Create a new Cluster.
Under Network Access, add
0.0.0.0/0for testing (allows all IPs).
(In production, restrict to trusted IPs only )Get your connection string from Database β Connect β Drivers.
Example:
mongodb+srv://<username>:<password>@cluster0.mongodb.net/myDB


STEP 4) Configure Backend Locally
Inside /server, create a config.env:
ATLAS_URI=<your-mongodb-ATLAS-connection-string>
PORT=5050

Run your backend:
cd server
npm install
npm run dev
Your backend should be live at β http://localhost:5050



STEP 5) Configure Frontend Locally
Inside /client, create a .env file:
VITE_API_URL=http://localhost:5050

Run your frontend:
cd client
npm install
npm run dev
Your frontend should be live at β http://localhost:5173



TESTING THE MERN APP LOCALLY



STEP 6) Deploy Backend to Azure Web App
Go to Azure Portal β Create a Web App
Runtime: Node.js
Region: Choose closest to you





Connect it to GitHub Actions under Deployment Center.
Add environment variables:
ATLAS_URIβ Your MongoDB connection string
Push code β GitHub Actions will build & deploy your backend.

NB; Donβt forget to create federated credentials under app registration for authentication of GitHub actions and azure credentials. (Follow these steps in the Azure portal to create the necessary federated credential.
Find Your App Registration: In the Azure portal, navigate to Azure Active Directory > App registrations. Search for the application registration associated with your service principal, in this case, it appears to be named
mern-deployment-sp.Go to Certificates & secrets: From the left-hand menu of your app registration, select Certificates & secrets.
Add a Federated Credential: Click on the Federated credentials tab, then click Add credential.
Configure the Credential: Fill out the form with the following details from your error log:
Scenario: Choose GitHub Actions deploying to Azure resources.
Organization: Your GitHub organization name (
isaacdivine37).Repository: The name of your repository (
MERN-App-Deployment-Automation-main).Entity type:
Environment.GitHub environment name:
Production(this must match the environment name in your workflow).Name: Give it a descriptive name like
mern-app-production-deployment.)



STEP 7) Deploy Frontend to Azure Static Web Apps
Go to Azure Portal β Create Static Web App.
Connect GitHub repo β choose branch.
Configure build settings:
App Location:
app/clientOutput Location:
dist
Add GitHub Secret:
VITE_API_URLβ Backend Web App URL (e.g.,https://your-api.azurewebsites.net)
Access the Github actions and edit the yaml and include the environment variable for it access the VITE_API_URL

Testing the workflow after including the vite variable

Push code β GitHub Actions will build & deploy your frontend.

STEP 8) Verify Deployment
- Frontend β
https://your-frontend.azurestaticapps.net
They should now communicate via VITE_API_URL.


Backend β https://your-api.azurewebsites.net/record

Verifying our profile at MongoDb Atlas

Why Use Azure Static Web Apps for MERN Frontend?
Azure Static Web Apps is better than regular App Service for React frontends because:
Performance: Static files served via global CDN load faster than server-hosted React
Cost: Free tier covers most apps vs $13+ monthly for App Service
Auto-scaling: Handles traffic spikes automatically without configuration
Independent deployment: Frontend and backend can be updated separately
Built-in CI/CD: Automatic GitHub integration with preview deployments
Use regular App Service only if you need:
Server-side rendering (SSR)
Real-time WebSocket features
Tightly coupled frontend/backend
Resources



