Raw
1 import { Note, Table, InlineIcon, getOsShortcut, Kbd } from "docs-ui"
2 import { XMarkMini } from "@medusajs/icons"
3
4 export const metadata = {
5 title: `Deployments`,
6 }
7
8 # {metadata.title}
9
10 In this guide, you'll learn about deployments in Cloud, how they're created, and how you can troubleshoot them.
11
12 ## Deployments Overview
13
14 Each [environment](../environments/page.mdx) has at least one deployment, which is publicly accessible at the [environment's URL](./access/page.mdx#find-backend-and-admins-url).
15
16 A deployment is created from the latest source code of an [environment](../environments/page.mdx)'s branch. An environment can have only one live deployment at a time.
17
18 The latest deployment of an environment is the live version of that environment, unless you [redeploy a previous deployment](#redeploy-a-deployment).
19
20 If you deployed both the Medusa backend and storefront in an environment, each deployment refers to the backend and storefront. The deployment will also show the status of both the backend and storefront.
21
22 ---
23
24 ## How are Deployments Created?
25
26 For long-lived environments, Medusa creates a new deployment every time you push a new commit to the environment's branch. For example, if your Production environment is connected to the `main` branch, Medusa will create a new Production deployment every time you push a new commit to the `main` branch.
27
28 For short-lived preview environments, Medusa creates a new environment and deployment when you open a pull request. Medusa will redeploy the preview environment every time you push a new commit to the pull request branch.
29
30 ---
31
32 ## Build Process for Deployments
33
34 ### Medusa Application Only
35
36 Before deploying your application on Cloud, Medusa runs the `build` script defined in your project's `package.json` file, which must run the `medusa build` command, among other build steps you may have.
37
38 Cloud also supports optional lifecycle scripts that you can define in your backend and storefront `package.json` files:
39
40 - `cloud:preinstall`: Executed before Cloud installs dependencies
41 - `cloud:prebuild`: Executed after dependencies are installed but before the build process starts
42
43 For example, your `package.json` scripts may look like this:
44
45 ```json title="package.json"
46 {
47 "scripts": {
48 "cloud:preinstall": "echo 'Pre-install setup'",
49 "cloud:prebuild": "echo 'Pre-build setup'",
50 "build": "medusa build && npm run other-build-steps"
51 }
52 }
53 ```
54
55 <Note>
56
57 You can replace `npm run other-build-steps` with the appropriate command for your package manager, such as `yarn other-build-steps`.
58
59 </Note>
60
61 ### Medusa Application with Storefront
62
63 If you're deploying both a Medusa application and a storefront on Cloud, Medusa will run the:
64
65 1. The `build` command defined in the backend's `package.json` file, which must run the `medusa build` command.
66 2. The build command relevant to the storefront, depending on the frontend framework you're using. For example, if you're using Next.js for your storefront, Medusa will run the `next build` command in the storefront's directory.
67 - Medusa currently doesn't support custom build scripts for storefronts.
68
69 In a monorepo, Medusa doesn't run the `build` script of your root `package.json` file. It builds each of the two packages separately, targeting only that package and the workspace packages it depends on. So, the other packages of your monorepo don't enter the build process.
70
71 <Note>
72
73 If you're using `turbo`, Medusa prunes your monorepo down to the package it's building. This means most files at the root of your monorepo aren't available during the build. Learn more in the [Prerequisites](../projects/prerequisites/page.mdx#root-files-available-during-the-build-turborepo) guide.
74
75 </Note>
76
77 Storefront builds and deployments operate independently of the backend status. This means storefront deployments can proceed successfully even if the backend build or deployment fails.
78
79 ### What Gets Deployed in the Medusa Application?
80
81 Medusa deploys the contents of the `.medusa/server` directory of the Medusa application. This directory is created by the [build process](!docs!/learn/build). It includes the compiled JavaScript files in your project, the production build of the admin dashboard, and other necessary files to run your Medusa application in production.
82
83 <Note>
84
85 Learn more about the `.medusa/server` directory in the [Build guide](!docs!/learn/build#output-directory-structure).
86
87 </Note>
88
89 So, if you have custom assets like JSON files that your application needs at runtime, make sure to copy them to the `.medusa/server` directory after the `medusa build` command in your `build` script.
90
91 For example, if your application needs a `src/data/custom.json` file at runtime, you can add a script that copies it to `.medusa/server/src/data/custom.json` after the build process is complete.
92
93 You can also customize the Medusa Admin build configurations using the [admin.vite](!docs!/learn/configurations/medusa-config#adminvite) option in your `medusa-config.ts` file.
94
95 <Note type="warning">
96
97 Do not expose sensitive files in your deployments, especially in the `.medusa/server/public` directory, as they will be publicly accessible. Sensitive files include those containing secret API keys, database credentials, or any other confidential information.
98
99 </Note>
100
101 ---
102
103 ## Find Project Deployments
104
105 You can find the deployments for a project in its dashboard.
106
107 To find the deployments for a project:
108
109 <Note title="Tip">
110
111 You can also navigate to a project's deployments from the [command palette](../command-palette/page.mdx). Press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, select **Deployments**, then choose a project.
112
113 </Note>
114
115 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
116 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
117 3. Click **Deployments** in the sidebar under the project.
118
119 You'll find all deployments for the project's environments on the Deployments page, sorted in descending order. For each deployment, you can see its branch, environment, status, and more.
120
121 ---
122
123 ## Find Environment Deployments
124
125 You can find the deployments for an environment in the project and environment dashboards.
126
127 For example, to find the deployments for the Production environment:
128
129 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
130 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
131 3. In the project's dashboard, click on the name of the environment. For example, "Production".
132
133 You'll find a table of all deployments for the environment, sorted in descending order. For each deployment, you can see its commit, status, and more.
134
135 ---
136
137 ## Find Deployment Details
138
139 You'll often need to check a deployment's details, such as its status, commit information, and logs.
140
141 To view a deployment's details:
142
143
144 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
145 2. Click **Projects** in the sidebar and select the project that contains the environment you want to view.
146 3. Click **Deployments** in the sidebar under the project.
147 4. Click on the deployment you want to view. You can also search deployments by their commit hash.
148
149 This will open the deployment's details page, where you can also see the deployment's commit at the top of the page.
150
151 On the deployment details page, you'll find:
152
153 1. **Backend**: The status of the backend deployment.
154 2. **Storefront**: The status of the storefront deployment, if applicable. If the storefront is not deployed, this section will show "Not deployed".
155 3. **Duration**: The time it took to build and deploy the deployment.
156 4. **Commit**: The commit hash that the deployment was created from, with the name of the branch, the commit message, the author, and when the commit was pushed.
157
158 ---
159
160 ## Deployment Statuses and Lifecycle
161
162 A deployment can have one of the following statuses:
163
164 <Table>
165 <Table.Header>
166 <Table.Row>
167 <Table.HeaderCell>
168 Status
169 </Table.HeaderCell>
170 <Table.HeaderCell>
171 Description
172 </Table.HeaderCell>
173 </Table.Row>
174 </Table.Header>
175 <Table.Body>
176 <Table.Row>
177 <Table.Cell>
178 Building
179 </Table.Cell>
180 <Table.Cell>
181 The deployment is currently being built. This is the initial status of a deployment.
182 </Table.Cell>
183 </Table.Row>
184 <Table.Row>
185 <Table.Cell>
186 Build canceled
187 </Table.Cell>
188 <Table.Cell>
189 The build process was canceled due to a newer commit pushed to the branch, which resulted in a new deployment.
190 </Table.Cell>
191 </Table.Row>
192 <Table.Row>
193 <Table.Cell>
194 Build failed
195 </Table.Cell>
196 <Table.Cell>
197 The build process failed due to a build error in either the backend or storefront (if applicable). You can check the [build logs](../logs/page.mdx) to troubleshoot the issue.
198 </Table.Cell>
199 </Table.Row>
200 <Table.Row>
201 <Table.Cell>
202 Awaiting deployment
203 </Table.Cell>
204 <Table.Cell>
205 The deployment has finished building and is waiting to be deployed.
206 </Table.Cell>
207 </Table.Row>
208 <Table.Row>
209 <Table.Cell>
210 Deploying
211 </Table.Cell>
212 <Table.Cell>
213 The deployment is currently being deployed.
214 </Table.Cell>
215 </Table.Row>
216 <Table.Row>
217 <Table.Cell>
218 Deploy failed
219 </Table.Cell>
220 <Table.Cell>
221 The deployment failed during the deployment process. You can check the [Runtime Logs](../logs/page.mdx) to troubleshoot the issue.
222 </Table.Cell>
223 </Table.Row>
224 <Table.Row>
225 <Table.Cell>
226 Live
227 </Table.Cell>
228 <Table.Cell>
229 The deployment finished successfully and is now the live version of the environment.
230 </Table.Cell>
231 </Table.Row>
232 <Table.Row>
233 <Table.Cell>
234 Ready
235 </Table.Cell>
236 <Table.Cell>
237 The deployment was previously deployed, but it's not currently the live version of the environment. This happens when a new deployment becomes the live version.
238 </Table.Cell>
239 </Table.Row>
240 </Table.Body>
241 </Table>
242
243 So, the lifecycle of a successful deployment is Building → Awaiting deployment → Deploying → Live. Then, once a new deployment is created, the previous deployment's status changes to Ready.
244
245 <Note>
246
247 If a deployment is stuck at a status like "Building" or "Deploying" for a long time, contact support for assistance.
248
249 </Note>
250
251 ![Diagram showcasing the lifecycle of a deployment](https://res.cloudinary.com/dza7lstvk/image/upload/v1750150491/Cloud/deployment-lifecycle_y85gdl.jpg)
252
253 ---
254
255 ## Access Live Deployment
256
257 Once a deployment's status is "Live", you can access its Medusa Admin, send requests to its API routes, and access its storefront (if applicable).
258
259 Learn more in the [Access Live Deployment](./access/page.mdx) guide.
260
261 ---
262
263 ## Troubleshooting Failed Deployments
264
265 ### AI Troubleshooting
266
267 When a deployment or its build fails, you can use the "Fix with AI" feature to troubleshoot the issue with the help of the [Cloud Assistant](../assistant/page.mdx). The assistant is always available for failed deployments, allowing you to get assistance whenever there are issues in your deployments. The AI agent will analyze the deployment and build logs, identify the root cause of the issue, and provide you with actionable steps to resolve it.
268
269 Learn more in the [Fix Deployment and Build Issues with AI](./fix-with-ai/page.mdx) guide.
270
271 ### Manually Troubleshooting Build Failures
272
273 If a deployment's status is "Build failed", you can:
274
275 - Check the [build logs](../logs/page.mdx) to understand why it failed. The build logs will show you the errors that occurred during the build process for either the backend or storefront (if applicable), which can help you fix the issue in your code.
276 - Use the [Cloud CLI](../cli/commands/logs/page.mdx) to stream the build logs and identify the issue. This is useful when debugging the issue with your AI agent.
277 - Contact support for help with the issue, if necessary.
278
279 ### Manually Troubleshooting Deployment Failures
280
281 If a deployment's status is "Deploy failed", you can:
282
283 - Check the [runtime logs](../logs/page.mdx) to understand why it failed. The runtime logs will show you the errors that occurred during the deployment process, which can help you fix the issue in your code.
284 - Use the [Cloud CLI](../cli/commands/logs/page.mdx) to stream the deployment logs and identify the issue. This is useful when debugging the issue with your AI agent.
285 - Contact support for help with the issue, if necessary.
286
287 <Note>
288
289 If the logs don't show a cause related to your application, [check the Cloud status page](../status/page.mdx) for ongoing Cloud incidents. If the **Build & Deploy** service isn't operational, wait for Cloud to resolve the incident, then [redeploy the deployment](#redeploy-a-deployment).
290
291 </Note>
292
293 ---
294
295 ## Redeploy a Deployment
296
297 You might need to redeploy an old deployment if there are unexpected issues with the current live deployment.
298
299 By redeploying a previous deployment, you revert or rollback the live version of its environment to the code in that deployment's commit.
300
301 <Note title="Important">
302
303 Redeploying a deployment will not revert database changes made in the latest deployment. If you need to revert database changes, contact support for assistance based on your use case.
304
305 </Note>
306
307 To redeploy a deployment:
308
309 1. [Go to the deployment's details page](#find-deployment-details).
310 2. Click the "Redeploy" button at the top right of the page.
311
312 <Note>
313
314 The "Redeploy" button is disabled if the build's container image has been deleted. In that case, you must create a new deployment by pushing a new commit to the environment's branch.
315
316 </Note>
317
318 This will trigger the redeployment process for the selected deployment. The deployment will go through [the same lifecycle](#deployment-statuses-and-lifecycle) as a new deployment.
319
320 Once the redeployment is complete, the deployment's status will change to "Live" and it will become the new live version of the environment.
321
322 ---
323
324 ## Change Deployment Rules
325
326 For each environment, you can change the rules that trigger a new deployment. For example, you can change the branch that the environment is connected to, which changes when a new deployment is created.
327
328 To change the branch that triggers a deployment for an environment, refer to the [Change Environment's Branch](../environments/long-lived/page.mdx#change-environments-branch) guide.
329
330 ### Change Environment Deployment Rules
331
332 To change the deployment rules for an environment:
333
334 1. If you're in a different organization, [switch to the organization](../organizations/page.mdx#switch-organization).
335 2. Click **Projects** in the sidebar and select the project that contains the environment.
336 3. In the project's dashboard, click on the name of the environment. For example, "Production".
337 4. Click **Deployment Rules** in the sidebar under the environment's section.
338 5. In the "Deploy rules" section, you can click **Add condition** to add a new deployment rule. You can then select:
339 - **Condition Type**: Either **Author** or **Commit Message**. This determines what the condition will check for when a new commit is pushed to the environment's branch.
340 - **Operator**: Either **Is**, **Is not**, or **Contains**. This determines how the condition will check the commit's author or message.
341 - **Value**: The value to compare against. For example, if the condition type is **Author** and the operator is **Is**, you would enter the author's name here.
342 - To remove a condition, click the <InlineIcon Icon={XMarkMini} alt="x" /> icon next to the condition.
343 6. Click "Save" to apply the changes.
344
345 The deployment rules will take effect for next deployments. For example, if you change the author that triggers a deployment, the next deployment will only be created if a commit is pushed to the environment's branch by that author.
346