Guide on creating shareable ephemeral environments for your MedusaJS application
In the world of enterprise eCommerce dominated by Shopify and other similar website building platforms, the developer experience for creating applications is localized to either a few enterprise ready solutions or building your own solution from scratch. MedusaJS was created to empower developers to take eCommerce application development in their own hands instead of being dependent on the dominant eCommerce application platforms in the market. It has bridged this gap by providing extensible eCommerce logic for creating and managing abstractions such as carts, products, order management, the ability to orchestrate these abstractions etc. and allowing developers to manage their own platform at no cost. Teams can start creating their own eCommerce application using these building blocks or even integrate them into their existing applications as they see fit.
When developing large scale eCommerce applications, teams building multiple services for it can have issues with testing, previewing, showcasing etc. The following are some hurdles which are faced during development, for example:
In all these cases, the developer can benefit from isolated environments, ideally triggered from the changes created to the eCommerce application. These changes would be reflected in the pull requests created by them. We can use Uffizzi to create these ephemeral environments on every pull request.
Ephemeral environments are isolated, temporary, and easily configurable. They can be shared among developers, QA teams, and stakeholders via a predictable url, which is publicly accessible. They help debug and test faster on every iteration, ensuring a smoother development process and enhanced collaboration across teams.
We are going to create a Github Actions workflow which will be triggered on every pull request made to your Medusajs application repo. Here we are using https://github.com/UffizziCloud/medusajs-previews as an example to demonstrate what that looks like. Upon successful execution the workflow will have created an ephemeral environment which can be accessed from the pull request itself. Let’s take a look at the application. It’s service architecture, and how Uffizzi is configured to create ephemeral environments for the same.
The MedusaJS is only our backend which will expose an API to the frontend from where the store can be accessed. The backend is also connected to a database and a cache which will run as services of their own. The following is what the service architecture for the entire ecommerce application will look like :
The user will only have access to a single service from the ephemeral environment URL. Here we will be configuring it to expose only the frontend service.
The following is the directory structure of this sample application with only the relevant files and directories for this guide:
Project directory structure
The frontend and the medusa backend services will be built into container images which are then used to create containers. The builds for both these images need to be mentioned in the uffizzi-build.yml configuration file which can be seen in the build-medusa and build-storefront jobs. The image tags built and pushed from here are then referenced in the Uffizzi Compose using the ${MEDUSA_IMAGE} and the ${STOREFRONT_IMAGE} placeholders. To understand more on how this can be done, check out this blog which will guide you through extending Github Actions with Uffizzi. We will not be going through how to setup the uffizzi-build.yml and uffizzi-preview.yml in this blogpost.
The medusa.config.js projectConfig object below gives a better idea of how the medusa backend connects to other services :
MedusaJS configuration
In the above configuration the values for REDIS_URL and DATABASE_URL are pointing to the sockets exposed by the redis and postgres services. Check out https://docs.medusajs.com/ for any other configuration you would like to add.
version: "3"
x-uffizzi:
ingress:
service: frontend
port: 8000
services:
backend:
image: ${MEDUSA_IMAGE}
environment:
DATABASE_URL: postgres://postgres:postgres@0.0.0.0:5432/medusa-docker
REDIS_URL: redis://0.0.0.0
NODE_ENV: development
JWT_SECRET: something
COOKIE_SECRET: something
PORT: 9000
ports:
- "9000:9000"
deploy:
resources:
limits:
memory: 2000M
frontend:
image: ${STOREFRONT_IMAGE}
container_name: frontend
ports:
- "8000:8000"
deploy:
resources:
limits:
memory: 2000M
postgres:
image: postgres:10.4
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: medusa-docker
redis:
image: redis
expose:
- "6379:6379"
Uffizzi Configuration for MedusaJS
The Uffizzi configuration is derived from the docker-compose.yml. This allows anyone to get started with Uffizzi given that they know how to configure docker-compose files. Now let’s look at some of the changes we have made on top of the docker-compose.yml so that it can be used with Ufffizzi
Once the configurations are finalised, the ephemeral environments can be tested by creating a pull request against the repo.
After the above changes have been committed to the github repo which houses the medusajs application, and GitHub workflows, the repo is now ready to be tested.
Opening a pull request to your repo containing the Medusa backend and frontend, the two stage workflow would kick off the ephemeral environment creation process after which the environment link would be advertised in a comment posted on the PR like the one below.
The x-uffizzi section defines configuration specific to Uffizzi. The ingress has to be defined here which dictates which service will all the requests be routed to. In this specific configuration, all requests will be routed to the frontend service. As the frontend service is connected to
Following the link will take you to your Medusajs ecommerce store built from the changes in the pull request. You can explore the store and see how the ephemeral environment works.
Once you know how to set up the github actions workflow to trigger ephemeral environments for your Medusajs application, you can now start evolving them as per your needs. The following are a few ways you can start getting creative with your Ephemeral Environments.
If you have a project for which you would like to build ephemeral environments for and discuss the different ways you can use them to better fit your needs, we look forward to speaking with you. You can get in touch with us by going to https://www.uffizzi.com/contact and we will reach out to you !
Uffizzi has been used in multiple open source projects across github with Github Actions. These projects are all listed here.