1 import { InlineIcon, getOsShortcut, Kbd } from "docs-ui"
2 import { EllipsisHorizontal, PencilSquare } from "@medusajs/icons"
3
4 export const metadata = {
5 title: `Long-Lived Environments`,
6 }
7
8 # {metadata.title}
9
10 In this guide, you'll learn about long-lived environments in Cloud and how to manage them.
11
12 Long-lived environments are stable environments that you create for specific branches in your GitHub repository, such as Production or Staging. They allow you to test changes in a production-like environment before deploying them to the live application.
13
14 <Note>
15
16 Learn more about what environments are and their types in the [Environments](../page.mdx) guide.
17
18 </Note>
19
20 ## Long-Lived Environments Infrastructure
21
22 Production and long-lived environments are stable and scalable, making them ideal for hosting your Medusa applications. Medusa ensures that they're:
23
24 - **Always available with zero-downtime deployments**: Your application will always be available for users, even during deployments. Medusa automatically handles rolling updates without interrupting your users.
25 - **Automatically scaled to meet peak traffic**: Medusa automatically scales your servers to handle traffic spikes, such as during flash sales or marketing campaigns. Your application can handle sudden increases in traffic without manual intervention.
26 - **Equipped with server and worker instances**: Your application runs on multiple server instances that handle incoming HTTP requests, while worker instances handle background tasks like scheduled jobs and subscribers. This separation allows your application to handle both web traffic and background processing efficiently.
27
28 ---
29
30 ## Create a Long-Lived Environment on Cloud
31
32 It's recommended to create long-lived environments for different stages of your project's development.
33
34 For example, you can create a Staging environment to test new features and updates before deploying them to Production. That way, you can test changes in a production-like environment without affecting the live application.
35
36 This section explains how to create a long-lived environment in Cloud.
37
38 ### Prerequisites: Create Environment Branch
39
40 A long-lived environment is associated with a specific branch in your GitHub repository. So, before creating an environment, make sure you have the necessary branch for it in your repository.
41
42 For example, if you're creating a Staging environment, create and push a `staging` branch in your repository:
43
44 ```bash
45 git checkout -b staging # create branch
46 git push origin staging # push branch to remote
47 ```
48
49 ### Steps to Create a Long-Lived Environment
50
51 To create a long-lived environment:
52
53 <Note title="Tip">
54
55 You can also navigate to the environment creation form from the [command palette](../../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, select **Create Environment**, then choose a project.
56
57 </Note>
58
59 1. In the [project's dashboard](../../projects/page.mdx#open-project-dashboard), click on the "Add environment" button at the top right.
60 2. In the side window that opens:
61 - **Environment name**: Enter the name of the environment. For example, `Staging`.
62 - **Branch**: Click the branch field to open the list of your repository's branches, then select the branch you want to associate with this environment. For example, `staging`. You can use the search field at the top of the list to find a branch by its name.
63 - If a branch is already linked to another environment, the list shows a badge with that environment's name next to the branch, and you can't select it.
64 - If you don't see the branch you want to use, refresh the page to load the latest branches from your repository.
65 - **Custom subdomain**: Set the custom subdomain for the Medusa application (and, subsequently, storefront). Medusa applications are a subdomain of `medusajs.app`, and their storefront is a subdomain of `medusajs.site`.
66 - Refer to the [Subdomain Restrictions](../../projects/page.mdx#subdomain-restrictions-for-projects) guide for reserved subdomains and restrictions.
67 - **Infrastructure Specs** (optional): By default, your long-lived environment will use the infrastructure specs of your organization's plan. You can also choose to upgrade or downgrade these specs for a specific environment. For example, you can downgrade your long-lived environment to **Develop** if you want to reduce costs. When you enable the Infrastructure Specs toggle, you can choose a different spec. Refer to the [Plans and Pricing](../../pricing/page.mdx) to learn more about the specs of each plan.
68 3. Once you're done, click the "Create" button.
69
70 The created environment will appear in the [project's dashboard](../../projects/page.mdx) as a card.
71
72 ### Trigger Environment Deployment
73
74 After creating the environment, Medusa will automatically trigger a deployment for the environment using the associated branch (for example, `staging`). This includes:
75
76 - Deploying the Medusa application and storefront (if applicable) from the associated branch.
77 - Setting up the database and resources for the environment. These are different from the resources of other environments.
78
79 After that, Medusa will automatically deploy the environment whenever you push a new commit to the associated branch.
80
81 Then, wait for the deployment to finish. You can check its status in the "Deployments" section. Once it's deployed, you can [access the environment](#access-deployed-environment) using its URL.
82
83 ### Log in to the Medusa Admin
84
85 Once the environment is deployed, you can log in to the Medusa Admin with your Cloud account by clicking the "Log in with Medusa Cloud" button. Medusa creates a user in the Medusa application with the same email as your Cloud account.
86
87 <Note title="Can't find the Log in With Medusa Cloud Button?">
88
89 Refer to the [Access Deployment](../../deployments/access/page.mdx#cant-find-the-log-in-with-medusa-cloud-button) guide for troubleshooting steps.
90
91 </Note>
92
93
94 ---
95
96 ## Access Deployed Environment
97
98 Once an environment is deployed, you can access it through its URL.
99
100 Learn more about how to access an environment's deployment in the [Access Deployment](../../deployments/access/page.mdx) guide.
101
102 ---
103
104 ## Open Environment Dashboard
105
106 On the environment's dashboard, you can view its details, logs, and settings. You can also manage the environment's variables, deployment rules, and database dump.
107
108 To open an environment's dashboard:
109
110 1. If you're in a different organization, [switch to the organization](../../organizations/page.mdx#switch-organization).
111 2. Click **Projects** in the sidebar and select the project that contains the environment.
112 3. In the project's dashboard, click on the environment's card.
113
114 This will open the environment's dashboard.
115
116 ### Find Environment Details
117
118 On the environment's dashboard, you can find the following details:
119
120 1. **Environment Medusa Backend URL**: The URL to access the environment. You can find it below the environment's name.
121 2. **Environment Storefront URL**: The URL to access the environment's storefront. You can find it below the "Storefront" title in the environment's card.
122 3. **Medusa Application's Deployment Status**: The current status of the latest Medusa application deployment, such as "Live" or "Deploying". You can find it next to the "Backend" title in the environment's card.
123 4. **Storefront's Deployment Status**: The current status of the latest storefront deployment, such as "Live" or "Deploying". You can find it next to the "Storefront" title in the environment's card.
124 5. **Environment Deployments**: The list of deployments for the environment, including the latest deployment. You can find it in the "Deployments" section of the environment's dashboard.
125
126 <Note title="Why are there two Medusa Backend URLs?">
127
128 Aside from the subdomain you set when [creating the environment](../../environments/long-lived/page.mdx#create-a-long-lived-environment-on-cloud), Medusa also provides a unique, randomly generated URL for each environment. Both of these URLs point to the same deployment.
129
130 </Note>
131
132 ---
133
134 ## Promote Environment to Production
135
136 You can promote a long-lived environment to Production in Cloud. This is useful when you want to make the environment's deployment the live application for your users, or when you want to switch the Production environment to a different branch.
137
138 <Note title="Caution" type="warning">
139
140 Before promoting an environment to Production, consider the following:
141
142 - By promoting a long-lived environment to Production, the Production environment will become a long-lived environment, and the promoted environment will become the new Production environment. The previous Production environment will no longer be the live application for your users, and its name will be changed to the name of the promoted environment.
143 - Promoting the environment doesn't retain the Production environment's settings or custom domain. If you want to keep certain settings the same, such as the same custom domain, make sure to set them up again after promotion.
144
145 </Note>
146
147 For example, if you have a Staging environment and you want to promote it to Production, the Staging environment will become the new Production environment, and the previous Production environment will be renamed to "Staging".
148
149 To promote a long-lived environment to Production:
150
151 1. In the [project's dashboard](../../projects/page.mdx#open-project-dashboard), find the long-lived environment you want to promote to Production.
152 2. Click on the <InlineIcon Icon={EllipsisHorizontal} alt="three-dots" /> icon at the bottom right of the environment's card.
153 3. Choose "Promote to Production" from the dropdown menu.
154
155 This will promote the environment's deployment to Production, making it the live application for your users.
156
157 ---
158
159 ## View Environment Logs
160
161 You can view build and runtime logs for an environment in the "Logs" section of the environment's dashboard. These logs help you debug issues in your Medusa application and storefront.
162
163 Learn more in the [Logs guide](../../logs/page.mdx).
164
165 ---
166
167 ## Monitor Environment Performance
168
169 You can monitor the performance of your environment in the "Monitoring" section of the environment's dashboard. This includes monitoring HTTP requests, background jobs, and more.
170
171 Learn more in the [Monitoring guide](../../monitoring/page.mdx).
172
173 ---
174
175 ## Manage Environment Variables
176
177 In the environment's **Environment variables** settings, you can manage the environment variables for the environment's Medusa application and storefront. Environment variables are key-value pairs that store sensitive information, such as API keys, or configurations that your application needs to run.
178
179 Learn how to manage environment variables in the [Environment Variables](../environment-variables/page.mdx) guide.
180
181 ### Redeploy Environment
182
183 After you add or change the environment's variables, you must redeploy the environment for the changes to take effect:
184
185 1. In the [project's dashboard](../../projects/page.mdx#open-project-dashboard), click on the environment you want to redeploy.
186 2. In the environment's dashboard, click on the "Redeploy" button to redeploy the environment with the new environment variables.
187
188 This redeploys the environment's live deployment with the updated environment variables.
189
190 ---
191
192 ## Manage Domains
193
194 Your long-lived environment has two types of domains:
195
196 - **Medusa-provided subdomains**: The default `medusajs.app` (backend) and `medusajs.site` (storefront) subdomains assigned to the environment. For example, `acme.medusajs.app` and `acme.medusajs.site`.
197 - **Custom domains**: Your own domain names to serve the backend and storefront instead of the Medusa-provided subdomains. For example, `acme.com`.
198
199 To update these domains, see the [Subdomains](../subdomains/page.mdx) and [Custom Domains](../custom-domains/page.mdx) guides.
200
201 ---
202
203 ## Change Environment's Branch
204
205 You can't change the branch of preview environments. You can only change it for long-lived environments, such as Production or Staging.
206
207 To change the branch associated with a long-lived environment:
208
209 1. If you're in a different organization, [switch to the organization](../../organizations/page.mdx#switch-organization).
210 2. Click **Projects** in the sidebar and select the project that contains the environment.
211 3. In the project's dashboard, click on the name of the environment. For example, "Production".
212 4. Click **Deployment Rules** in the sidebar under the environment's section.
213 5. In the "Linked branch" section, click the <InlineIcon Icon={PencilSquare} alt="pencil" /> icon to change the branch.
214 6. In the side window that opens, click the branch field and select the branch you want to link to the environment. You can use the search field at the top of the list to find a branch by its name. Branches already linked to another environment show a badge with that environment's name, and you can't select them.
215 7. Click **Save** to apply the changes.
216
217 The next time you push a commit to the new branch, Medusa will create a new deployment for the environment. Pushes to the previous branch will no longer trigger new deployments for the environment.
218
219 ---
220
221 ## Import and Export Environment's Database Dump
222
223 In the environment's **Database** settings, you can export a database dump of the environment. This is useful for backing up the environment's data, debugging issues, or migrating the data to another environment.
224
225 You can also import a database dump to use existing data that you have, or to restore the environment to a previous state.
226
227 Learn more in the [Database](../../database/page.mdx) guide.
228
229 ---
230
231 ## Change Infrastructure Specifications for Long-Lived Environment
232
233 <Note>
234
235 To change the infrastructure specs of the Production environment, see the [Projects](../../projects/page.mdx#change-project-infrastructure-specifications) guide.
236
237 </Note>
238
239 By default, long-lived environments that aren't the Production environment use the specs of the **Develop** plan. This is useful to reduce costs on those environments, but that means it may not be fitting for high-traffic or complex operations.
240
241 To change the infrastructure specs for a long-lived environment:
242
243 1. In the [environment's dashboard](#open-environment-dashboard), click **Deployment Rules** in the sidebar.
244 2. In the **Infrastructure Specs** section, select the plan you'd like to use its specs for the long-lived environment.
245 3. Click **Save Changes**.
246
247 The new specs for the environment will be applied afterward.
248
249 ---
250
251 ## Delete an Environment
252
253 <Note title="Danger" type="warning">
254
255 Deleting an environment will delete all its resources and settings, including its database and environment variables. This action is irreversible. You won't be able to recover any data after deletion.
256
257 </Note>
258
259 You can delete any environment in your project, except for the Production environment.
260
261 To delete an environment:
262
263 1. In the environment's dashboard, click **Deployment Rules** in the sidebar.
264 2. Click the **Delete** button in the "Delete Environment" section.