|
| 1 | +--- |
| 2 | +title: Adding a Custom 1-Click Deploy |
| 3 | +sidebar_position: 800 |
| 4 | +description: Add a custom 1-Click Deploy button to deploy your own app. |
| 5 | +--- |
| 6 | + |
| 7 | +# Adding a Custom 1-Click Deploy |
| 8 | + |
| 9 | +This tutorial will show you how to add a custom Defang 1-Click Deploy button to deploy your own app to Defang Playground. |
| 10 | + |
| 11 | +:::info |
| 12 | +This page focuses on adding 1-Click Deploy to any app that is not from [our list of samples](https://defang.io/#samples). We have a separate tutorial on [how to use 1-Click Deploy](/docs/tutorials/using-one-click-deploy) for Defang's pre-made samples, which is meant for new users. |
| 13 | +::: |
| 14 | + |
| 15 | +<!-- |
| 16 | +0. have a project ready, |
| 17 | +1. get the deploy.yaml action (and set configs as needed) |
| 18 | +2. get the compose.yaml for project and put in root folder |
| 19 | +3. click "template repo" in settings github |
| 20 | +4. get the url (encoded version) of this template repo ready for making a new 1-click deployment instance |
| 21 | + https://portal.defang.dev/redirect?url=<encoded url>&name=your-project-name |
| 22 | + this goes thru portal in order for the person deploying to get logged into defang |
| 23 | + https://www.urlencoder.io/ can use this encoder |
| 24 | +5. paste it into the readme/button |
| 25 | +--> |
| 26 | + |
| 27 | + |
| 28 | +## Step 1 - Have an App Ready |
| 29 | + |
| 30 | +The button we will be creating in this tutorial will allow any user to deploy your app to their Defang account. If your app is ready to be deployed with Defang (i.e. has a valid `compose.yaml` file), you can skip this step. |
| 31 | + |
| 32 | +If not, you can either [manually write one](/docs/concepts/compose): |
| 33 | +```yaml |
| 34 | +services: |
| 35 | + service-example: |
| 36 | + image: nginx:latest # replace with the image or build for your app |
| 37 | + ports: |
| 38 | + - mode: ingress # specify ports to expose |
| 39 | + target: 8080 |
| 40 | + published: 8080 |
| 41 | + |
| 42 | +``` |
| 43 | +
|
| 44 | +Or, you can generate a new app using the command: |
| 45 | +
|
| 46 | +``` |
| 47 | +defang generate |
| 48 | +``` |
| 49 | + |
| 50 | +:::info |
| 51 | +Make sure your `compose.yaml` file is in the root folder of your project. |
| 52 | +::: |
| 53 | + |
| 54 | +It may be helpful to have a `README.md` file, or a location to paste your 1-Click Deploy link. |
| 55 | + |
| 56 | +## Step 2 - Copy the GitHub Workflow |
| 57 | + |
| 58 | +In your project, add a new folder named `.github`, then add a folder `workflows` inside it, and copy the entire GitHub Workflow `deploy.yaml` file from [here](https://github.com/DefangLabs/samples/blob/main/starter-sample/.github/workflows/deploy.yaml) into the `workflows` folder. |
| 59 | + |
| 60 | +If your app does not have any [configuration](/docs/concepts/configuration) needed (e.g. API keys, passwords, etc.), you can remove the corresponding lines as indicated in the file. |
| 61 | + |
| 62 | +You should have a `.yaml` file similar to the one below: |
| 63 | +```yaml |
| 64 | +name: Deploy |
| 65 | + |
| 66 | +on: |
| 67 | + push: |
| 68 | + branches: |
| 69 | + - main |
| 70 | + |
| 71 | +jobs: |
| 72 | + deploy: |
| 73 | + environment: playground |
| 74 | + runs-on: ubuntu-latest |
| 75 | + permissions: |
| 76 | + contents: read |
| 77 | + id-token: write |
| 78 | + |
| 79 | + steps: |
| 80 | + - name: Checkout Repo |
| 81 | + uses: actions/checkout@v4 |
| 82 | + |
| 83 | + - name: Deploy |
| 84 | + uses: DefangLabs/[email protected] |
| 85 | +``` |
| 86 | +
|
| 87 | +
|
| 88 | +## Step 3 - Use a Template GitHub Repository |
| 89 | +
|
| 90 | +You have the option of using Defang's template repo or your own template GitHub Repo for this step. There is no difference in end result, but it may be easier to use Defang's template repo so that you do not have to create your own. |
| 91 | +
|
| 92 | +### Option 1 - Use an Existing Template Repo |
| 93 | +
|
| 94 | +1. Click the link to [Defang's 1-Click Template Repo](https://github.com/DefangLabs/1-click-template), or open an existing template repo if you have one. |
| 95 | +2. Click the dropdown menu next to the green "Use this template" button near the top right corner, then click "Create a new repository": |
| 96 | +
|
| 97 | + |
| 98 | +
|
| 99 | +4. You will be taken to a page similar to the one shown below. Then, enter a name for your repository and click the green "Create repository" button at the bottom. |
| 100 | +
|
| 101 | + |
| 102 | +
|
| 103 | +5. Now that you've created a new repository from a template repo, you can move onto the next step, which is getting the encoded URL. |
| 104 | +
|
| 105 | +### Option 2 - Manually Set Up a Template Repo |
| 106 | +
|
| 107 | +1. Create a new repository on GitHub. |
| 108 | +2. Go to the repository's "Settings", seen near the right on the top bar. |
| 109 | +3. Select the "Template Repository" box to turn your repo into a template repo, as seen below: |
| 110 | +
|
| 111 | + |
| 112 | +
|
| 113 | +4. Go back to this repository's home page and follow the instructions in Option 1, skipping the first step. |
| 114 | +
|
| 115 | +## Step 4 - Get the Encoded URL |
| 116 | +
|
| 117 | +Now that you've created a new repository from a template repo, you will need the encoded version of the URL. This is necessary because it is used as a redirect for the 1-Click Deploy link. |
| 118 | +
|
| 119 | +1. Copy the URL of your new repository's homepage (not the template repository's homepage). It should look something like the following: |
| 120 | +``` |
| 121 | +https://github.com/<your-github-username>/<your-project-here> |
| 122 | +``` |
| 123 | +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: |
| 124 | + |
| 125 | +``` |
| 126 | +https%3A%2F%2Fgithub.com%2F<your-github-username>%2F<your-project-here> |
| 127 | +``` |
| 128 | + |
| 129 | +## Step 5 - Create the 1-Click Deploy Link |
| 130 | + |
| 131 | +Finally, you will need to format the 1-Click Deploy Link as `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. |
| 132 | + |
| 133 | + |
| 134 | +:::tip |
| 135 | +Optionally, you can add `&name=<your-project-here>` to the end of the link if you'd like to suggest a name for the user to use for their deployment. |
| 136 | +::: |
| 137 | + |
| 138 | +The finished link should look similar to the one below: |
| 139 | +``` |
| 140 | +https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2F<your-github-username>%2F<your-project-here>&name=<your-project-here> |
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | +Now you have a link for anyone to deploy your app to Defang, with just 1 click! |
| 145 | + |
| 146 | + |
| 147 | +### Examples |
| 148 | +For example, you can add it as a link in a Markdown file: |
| 149 | +``` |
| 150 | +[1-Click Deploy Link](https://portal.defang.dev/redirect?url=<your-encoded-url>&name=<your-project-here>) |
| 151 | +``` |
| 152 | +Or perhaps you can add it to a button with your own styling: |
| 153 | +``` |
| 154 | +[](https://portal.defang.dev/redirect?url=<your-encoded-url>&name=<your-project-here>) |
| 155 | +``` |
0 commit comments