Skip to content

Commit e5c419c

Browse files
authored
Merge pull request #170 from DefangLabs/one-click-mods
Small modifications to one-click
2 parents c1d44ab + 6b570d5 commit e5c419c

File tree

6 files changed

+132
-9
lines changed

6 files changed

+132
-9
lines changed

docs/tutorials/adding-custom-one-click-deploy.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ sidebar_position: 800
44
description: Add a custom 1-Click Deploy link to deploy your own app.
55
---
66

7+
import { URLProvider, URLEncode } from "../../src/components/OneClick";
8+
9+
<URLProvider>
10+
711
# Adding Custom 1-Click Deploy to Your App
812

9-
This tutorial will show you how to add a 1-Click Deploy link to deploy your app to the Defang Playground.
13+
This tutorial will show you how to add a 1-Click Deploy link so other people can easily deploy your app to the Defang Playground and eventually to their own cloud accounts.
1014

1115
The link is often placed as a button in the `README.md` file of your project repository, and is the easiest way to allow anyone to deploy your app.
1216

@@ -83,12 +87,16 @@ You will need the encoded version of the URL of the page from the previous step.
8387
```
8488
https://github.com/new?template_name=<your-repo-name>&template_owner=<your-github-username>
8589
```
86-
2. You can go online and paste the URL from the step above into a URL encoder tool of your choice. You should end up with an encoded URL, similar to the one shown below:
90+
2. You need to URL encode your url for the next step. For example, the url above would be encoded as:
8791

8892
```
8993
https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3D<your-repo-name>%26template_owner%3D<your-github-username>
9094
```
9195

96+
You can just paste your url in here to get the encoded version:
97+
98+
<URLEncode />
99+
92100
## Step 5 - Create the 1-Click Deploy Link
93101

94102
You will need to create a 1-Click Deploy link with the following format: `https://portal.defang.dev/redirect?url=` + your encoded URL. This ensures that the user can get [logged in](/docs/concepts/authentication/) to Defang before they get redirected to clone your app for deployment.
@@ -113,3 +121,5 @@ Or perhaps you can add it to a button with your own styling:
113121
```
114122
[![1-click-deploy-button](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=<your-encoded-url>&name=<your-project-here>)
115123
```
124+
125+
</URLProvider>

docs/tutorials/using-one-click-deploy.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,41 @@ To access the full range of features provided by Defang, we recommend using the
1717
## Step 1 - Choose a Sample
1818
Head to our [list of samples](https://defang.io/#samples) and click a sample you want to deploy. Then, click on the button that says "1-Click Deploy".
1919

20-
![one-click-deploy-button](/img/use-one-click-tutorial/one-click-deploy-button.png)
20+
<img src="/img/use-one-click-tutorial/one-click-deploy-button.png" alt="one-click-deploy-button" width="500"/>
21+
<br/>
2122

2223
:::info
2324
Alternatively, you can find the "1-Click Deploy" button located in the `README.md` file of each sample's GitHub repository.
2425

25-
![deploy-with-defang-button](/img/use-one-click-tutorial/deploy-with-defang-button.png)
26+
<img src="/img/use-one-click-tutorial/deploy-with-defang-button.png" alt="deploy-with-defang-button" width="400"/>
27+
<br/>
2628
:::
2729

28-
## Step 2 - Allow Permissions
30+
## Step 2 - Login
2931

30-
After you've clicked, you will be prompted to use GitHub to log in. Once you see a "Create a new repository" page appear, allow the sample repository to get cloned into your GitHub account. You can do this by clicking the green "Create repository" button at the bottom.
32+
For 1-click deployments to work, Defang has to have your permission, which you can grant by logging in. If you are already logged in, you will be automatically taken to the next step.
3133

32-
![create-repository](/img/use-one-click-tutorial/create-repository.png)
34+
![login-screen](/img/use-one-click-tutorial/login-screen.png)
3335

34-
## Step 3 - Wait for Deployment to Complete
36+
37+
## Step 3 - Create Your Repo
38+
39+
Onced logged in, you'll be redirected to GitHub. Click the "Create repository button" to create a new repository with the sample project.
40+
41+
<img src="/img/use-one-click-tutorial/create-repository.png" alt="create-repository" width="600"/>
42+
<br/>
43+
44+
45+
## Step 4 - Wait for Deployment to Complete
3546

3647
A Github Action workflow will automatically start running to install Defang and deploy the sample to the Defang Playground. You can see this by going into the "Actions" tab in your GitHub repository.
3748

38-
You can view the status of your deployment in the [Defang Portal](https://portal.defang.dev/), or by downloading the [Defang CLI](/docs/getting-started).
49+
You can view the status of your deployment in the [Defang Portal](https://portal.defang.dev/), or by downloading the [Defang CLI](/docs/getting-started). You can also see deployment progress in the "Actions" tab of your GitHub repository:
50+
51+
<img src="/img/use-one-click-tutorial/actions.png" alt="github-actions-tab" width="400"/>
52+
53+
<br/>
54+
<br/>
3955

4056
:::tip
4157
If you decide to make a commit later to a repository created from 1-Click Deploy, then the project will automatically get deployed again to Defang Playground.

src/components/OneClick/index.tsx

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { Stack, TextField } from '@mui/material';
2+
import { createContext, useContext, useState } from 'react';
3+
import CodeBlock from '@theme/CodeBlock';
4+
5+
6+
7+
const URLContext = createContext({
8+
url: "",
9+
setUrl: (url: string) => { }
10+
});
11+
12+
export function URLProvider({ children }: { children: React.ReactNode }) {
13+
const [url, setUrl] = useState("");
14+
return (
15+
<URLContext.Provider value={{ url, setUrl }}>
16+
{children}
17+
</URLContext.Provider>
18+
);
19+
}
20+
21+
22+
23+
export function OriginalRepoUrl() {
24+
const { url, setUrl } = useContext(URLContext);
25+
return (
26+
<Stack spacing={2}>
27+
<TextField value={url} onChange={e => setUrl(e.target.value)} label="Your Repo URL" helperText="Like https://github.com/defanglabs/defang" />
28+
</Stack>
29+
);
30+
}
31+
32+
function getTemplateUrl(url: string) {
33+
// extract github user and repo from url
34+
const regex = /https:\/\/github.com\/([^\/]+)\/([^\/]+)/;
35+
const match = url.match(regex);
36+
const user = match ? match[1] : "";
37+
const repo = match ? match[2] : "";
38+
39+
// https://github.com/new?template_name=sample-django-template&template_owner=DefangSamples
40+
return `https://github.com/new?template_name=${repo}&template_owner=${user}&name=${repo}`;
41+
}
42+
43+
export function TemplateUrl() {
44+
const { url } = useContext(URLContext);
45+
46+
const templateUrl = getTemplateUrl(url);
47+
48+
return (
49+
<Stack spacing={2}>
50+
<TextField value={templateUrl} label="Your Template URL" />
51+
</Stack>
52+
);
53+
}
54+
55+
function getEncodedTemplateUrl(url: string) {
56+
const templateUrl = getTemplateUrl(url);
57+
return encodeURIComponent(templateUrl);
58+
}
59+
60+
61+
export function EncodedTemplateUrl() {
62+
const { url } = useContext(URLContext);
63+
const encodedTemplateUrl = getEncodedTemplateUrl(url);
64+
return (
65+
<Stack spacing={2}>
66+
<TextField value={encodedTemplateUrl} label="The Encoded Template URL" />
67+
</Stack>
68+
);
69+
}
70+
71+
function getOneClickUrl(url: string) {
72+
const encodedTemplateUrl = getEncodedTemplateUrl(url);
73+
return `https://portal.defang.dev/redirect?url=${encodedTemplateUrl}`;
74+
}
75+
76+
77+
export function OneClickUrl() {
78+
const { url } = useContext(URLContext);
79+
const oneClickUrl = getOneClickUrl(url);
80+
return (
81+
<Stack spacing={2}>
82+
<TextField value={oneClickUrl} label="Your 1-Click URL" />
83+
</Stack>
84+
);
85+
}
86+
87+
export function URLEncode() {
88+
const [val, setVal] = useState("");
89+
return (
90+
<Stack spacing={2}>
91+
<TextField value={val} placeholder='Paste a url here' onChange={e => setVal(e.target.value)} />
92+
<CodeBlock className="language-bash">
93+
{encodeURIComponent(val)}
94+
</CodeBlock>
95+
</Stack>
96+
)
97+
}
39.2 KB
Loading
Loading
Loading

0 commit comments

Comments
 (0)