Skip to content

Commit 28a5b86

Browse files
Deploy command tut (#2461)
1 parent 66a9860 commit 28a5b86

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed

examples/_data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ export const sidebar = [
395395
href: "/examples/deno_deploy_tutorial/",
396396
type: "tutorial",
397397
},
398+
{
399+
title: "Deploy with the deploy command",
400+
href: "/examples/deploy_command_tutorial/",
401+
type: "tutorial",
402+
},
398403
{
399404
title: "AWS Lambda",
400405
href: "/examples/aws_lambda_tutorial/",

examples/tutorials/deploy_command.md

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
---
2+
title: "Deploy an app with the deno deploy command"
3+
description: "Step-by-step tutorial for using the deno deploy CLI command to create and deploy your first application to Deno Deploy Early Access."
4+
url: /examples/deploy_command_tutorial/
5+
---
6+
7+
The `deno deploy` command provides a powerful CLI for deploying and managing
8+
applications on [Deno Deploy<sup>EA</sup>](https://deno.com/deploy).
9+
10+
If you already have an app to deploy you can skip to
11+
[Deploying your application](#deploying-your-application), or read on to make
12+
and then deploy a simple app.
13+
14+
## Prerequisites
15+
16+
Before using the deploy command, you will need access to Deno
17+
Deploy<sup>EA</sup>, and you will need a Deno Deploy<sup>EA</sup> organization.
18+
19+
1. Visit the
20+
[Deno Deploy account settings](https://dash.deno.com/account#early-access)
21+
2. Turn on the "Enable Early Access" toggle
22+
3. Create a Deno Deploy<sup>EA</sup> organization in the
23+
[Deno Deploy<sup>EA</sup> dashboard](https://app.deno.com/).
24+
25+
## Create a simple web application
26+
27+
First, let's create a basic HTTP server that will serve as our application.
28+
29+
Create a new directory for your project and navigate into it:
30+
31+
```bash
32+
mkdir my-deploy-app
33+
cd my-deploy-app
34+
```
35+
36+
Initialize a new Deno project:
37+
38+
```bash
39+
deno init
40+
```
41+
42+
Replace the contents of `main.ts` with a simple HTTP server:
43+
44+
```ts title="main.ts"
45+
Deno.serve({ port: 8000 }, (req) => {
46+
const url = new URL(req.url);
47+
const userAgent = req.headers.get("user-agent") || "unknown";
48+
const timestamp = new Date().toISOString();
49+
50+
// Log every request
51+
console.log(
52+
`[${timestamp}] ${req.method} ${url.pathname} - User-Agent: ${userAgent}`,
53+
);
54+
55+
// Simple routing
56+
if (url.pathname === "/") {
57+
console.log("Serving home page");
58+
return new Response(
59+
`
60+
<html>
61+
<head><title>My Deploy App</title></head>
62+
<body>
63+
<h1>Welcome to My Deploy App!</h1>
64+
<p>This app was deployed using the deno deploy command.</p>
65+
<nav>
66+
<a href="/about">About</a> |
67+
<a href="/api/status">API Status</a> |
68+
<a href="/api/error">Test Error</a>
69+
</nav>
70+
</body>
71+
</html>
72+
`,
73+
{
74+
headers: { "content-type": "text/html" },
75+
},
76+
);
77+
}
78+
79+
if (url.pathname === "/about") {
80+
console.log("Serving about page");
81+
return new Response(
82+
`
83+
<html>
84+
<head><title>About - My Deploy App</title></head>
85+
<body>
86+
<h1>About This App</h1>
87+
<p>This is a simple demonstration of deploying with the deno deploy CLI.</p>
88+
<p>Check the logs to see request information!</p>
89+
<a href="/">← Back to Home</a>
90+
</body>
91+
</html>
92+
`,
93+
{
94+
headers: { "content-type": "text/html" },
95+
},
96+
);
97+
}
98+
99+
if (url.pathname === "/api/status") {
100+
const responseData = {
101+
status: "ok",
102+
timestamp: new Date().toISOString(),
103+
message: "API is running successfully",
104+
requestCount: Math.floor(Math.random() * 1000) + 1, // Simulate request counter
105+
};
106+
107+
console.log("API status check - all systems operational");
108+
console.log(`Response data:`, responseData);
109+
110+
return Response.json(responseData);
111+
}
112+
113+
if (url.pathname === "/api/error") {
114+
// This endpoint demonstrates error logging
115+
console.error("Error endpoint accessed - demonstrating error logging");
116+
console.warn("This is a warning message that will appear in logs");
117+
118+
return Response.json({
119+
error: "This is a test error for demonstration",
120+
timestamp: new Date().toISOString(),
121+
tip: "Check the logs with: deno deploy logs",
122+
}, { status: 500 });
123+
}
124+
125+
// 404 for all other routes
126+
console.warn(`404 - Route not found: ${url.pathname}`);
127+
return new Response("Not Found", { status: 404 });
128+
});
129+
```
130+
131+
### Test your application locally
132+
133+
Update the `dev` task in the `deno.json` file in the root, to allow network
134+
access:
135+
136+
```json
137+
"dev": "deno run -N --watch main.ts"
138+
```
139+
140+
Then run the dev command:
141+
142+
```sh
143+
deno run dev
144+
```
145+
146+
Visit `http://localhost:8000` to see your application running. Try navigating to
147+
the different routes (`/about`, `/api/status`, and `/api/error`) to verify
148+
everything works. You'll notice that each request is logged to the console -
149+
these are the same logs you'll be able to see when the app is deployed!
150+
151+
## Authentication
152+
153+
The `deno deploy` command handles authentication automatically. When you first
154+
run a deploy command, it will prompt you to authenticate. Run the deploy command
155+
with the `--help` flag to see all available options:
156+
157+
```bash
158+
deno deploy --help
159+
```
160+
161+
:::note Deno Deploy<sup>EA</sup> organization requirement
162+
163+
If you don't already have a Deno Deploy<sup>EA</sup> organization set up, you
164+
can create one through the
165+
[Deno Deploy<sup>EA</sup> web app](https://app.deno.com).
166+
167+
:::
168+
169+
## Deploy your application
170+
171+
Now let's use the `deno deploy` command to deploy your application! Ensure that
172+
you are in the root directory of your project and run:
173+
174+
```bash
175+
deno deploy
176+
```
177+
178+
Select the appropriate options in the terminal when prompted.
179+
180+
The deployment process will:
181+
182+
1. Make a tarball of your application code
183+
2. Upload the tarball to Deno Deploy
184+
3. Unpack the tarball
185+
4. Build and deploy to the edge network
186+
5. Provide you with a live URL
187+
188+
You have now successfully deployed your application! You can visit the returned
189+
URL to see your app in action.
190+
191+
If you need to make changes to your application, simply update your code and run
192+
the `deno deploy` command again.
193+
194+
Our demo application had some logging built in, we can use the built in logging
195+
features of Deno Deploy to monitor the application.
196+
197+
## Monitoring your application
198+
199+
### View application logs
200+
201+
After deploying your application, you can stream live logs to see exactly what's
202+
happening on the app:
203+
204+
```bash
205+
deno deploy logs
206+
```
207+
208+
Visit your application URL and navigate to different pages. You'll see logs
209+
like:
210+
211+
- Request logs showing HTTP method, path, and user agent
212+
- Info logs from `console.log()` calls
213+
- Warning logs from `console.warn()` calls
214+
- Error logs from `console.error()` calls
215+
216+
Open your app url in the browser and try visiting the `/api/error` endpoint to
217+
see the error logs in action.
218+
219+
### View logs for a specific time range
220+
221+
To view logs for a specific time range, you can use the `--start` and `--end`
222+
flags:
223+
224+
```bash
225+
deno deploy logs \
226+
--start "2024-01-01T00:00:00Z" \
227+
--end "2024-01-01T23:59:59Z"
228+
```
229+
230+
## Managing environment variables
231+
232+
Your application might need environment variables for configuration. The
233+
`deno deploy` command provides comprehensive environment variable management.
234+
235+
### List environment variables
236+
237+
You can view all environment variables for your application:
238+
239+
```bash
240+
deno deploy env list
241+
```
242+
243+
### Add and update environment variables
244+
245+
To add individual environment variables, use the `deno deploy env add` command,
246+
for example:
247+
248+
```bash
249+
deno deploy env add API_KEY "your-secret-key"
250+
deno deploy env add DATABASE_URL "postgresql://..."
251+
```
252+
253+
Then to update them, use the `deno deploy env update-value` command, for
254+
example:
255+
256+
```bash
257+
deno deploy env update-value API_KEY "new-secret-key"
258+
deno deploy env update-value DATABASE_URL "postgresql://new-user:new-pass@localhost/new-db"
259+
```
260+
261+
### Delete environment variables
262+
263+
To delete an environment variable, use the `deno deploy env delete` command, for
264+
example:
265+
266+
```bash
267+
deno deploy env delete API_KEY
268+
deno deploy env delete DATABASE_URL
269+
```
270+
271+
### Load environment variables from a .env file
272+
273+
You can also use an `.env` file to load your environment variables to your
274+
deployed app:
275+
276+
```bash
277+
deno deploy env load .env
278+
```
279+
280+
🦕 You've successfully deployed your first application with the `deno deploy`
281+
command! Check out the [`deno deploy` docs](/runtime/reference/cli/deploy/) for
282+
more commands and options.
283+
284+
For more information on Deno Deploy<sup>EA</sup>, check the
285+
[Deno Deploy EA documentation](/deploy/early-access/).

0 commit comments

Comments
 (0)