|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +title: 💎 Clever Cloud |
| 4 | +--- |
| 5 | + |
| 6 | +# Clever Cloud 💎 |
| 7 | + |
| 8 | +[Clever Cloud](https://www.clever-cloud.com/) is a PaaS provider that enables you to start apps quickly using `git` while they manage the infrastructure, scalability, and run lifetime. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Before you get started, if you don't already have these, you'll need to create: |
| 13 | + |
| 14 | +- A [Clever Cloud Account](https://api.clever-cloud.com/v2/sessions/signup) |
| 15 | +- Add billing information |
| 16 | + |
| 17 | +:::caution |
| 18 | +As with any PaaS service you should check the pricing before continuing. Clever Cloud has a transparent pricing policy depending on the runtime type and size, see the [Clever Cloud Pricing Page](https://www.clever-cloud.com/pricing/). |
| 19 | +::: |
| 20 | + |
| 21 | +You might be able to do everything in the console, but for this tutorial, we will use the [Clever Cloud CLI](https://www.clever-cloud.com/doc/reference/clever-tools/getting_started/), so you'll have to install it on your computer. |
| 22 | + |
| 23 | +Then log in running: |
| 24 | + |
| 25 | +```bash |
| 26 | +clever login |
| 27 | +``` |
| 28 | + |
| 29 | +## Deploying |
| 30 | + |
| 31 | +### 1. Create the Clever Cloud application |
| 32 | + |
| 33 | +_Clever Cloud_ has [many available runtime](https://www.clever-cloud.com/product/) that are optimized for the type of application you want to deploy. Unfortunately, **dart** is **not** _yet_ one of them. |
| 34 | + |
| 35 | +In order to deploy those type of applications, there is a [_Docker_ runtime](https://www.clever-cloud.com/doc/deploy/application/docker/docker/) available, and we will this one. |
| 36 | + |
| 37 | +So, first things first, lets create a _Docker_ runtime: |
| 38 | + |
| 39 | +:::tip |
| 40 | +Make sure you are at the root of your project and not in `build`, as this command will produce a `.clever.json` file in the current folder. |
| 41 | +::: |
| 42 | + |
| 43 | +```bash |
| 44 | +clever create --type docker <app-name> --region <zone> --org <org> |
| 45 | +``` |
| 46 | + |
| 47 | +For example, if the project's name is _api_ and you want it to be deployed in _Paris_ (par), you can run: |
| 48 | + |
| 49 | +```bash |
| 50 | +clever create --type docker api --region par |
| 51 | +``` |
| 52 | + |
| 53 | +The `<org>` argument is optional if you only have one Clever Cloud organization. |
| 54 | +:::note |
| 55 | +For a full list of all the available zones refer to [Clever Cloud's zones list](https://www.clever-cloud.com/blog/features/2020/11/05/ovh-clever-cloud-zones/). |
| 56 | + |
| 57 | +::: |
| 58 | + |
| 59 | +As a result, you should have `Your application has been successfully created!` |
| 60 | + |
| 61 | +If you check your project's file, you'll find a `.clever.json` file containing something like this: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "apps": [ |
| 66 | + { |
| 67 | + "app_id": "app_<some_id>", |
| 68 | + "org_id": "<org>", |
| 69 | + "deploy_url": "https://<some_src>.services.clever-cloud.com/app_<some_id>.git", |
| 70 | + "name": "<app-name>", |
| 71 | + "alias": "<app-name>" |
| 72 | + } |
| 73 | + ] |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +:::note |
| 78 | +You **can** add this file to your git repository. |
| 79 | +::: |
| 80 | + |
| 81 | +:::tip |
| 82 | +By default, the runtime is scaled to **XS**, which is 1 CPU / 1Gio RAM, if you want to scale it down for testing purposes you can use the following: |
| 83 | + |
| 84 | +```bash |
| 85 | +clever scale --flavor nano |
| 86 | +``` |
| 87 | + |
| 88 | +Giving the output `App rescaled successfully` |
| 89 | +::: |
| 90 | + |
| 91 | +### 2. Deploy your API to Clever Cloud |
| 92 | + |
| 93 | +As Clever Cloud uses `git` we don't have to compile binaries and share them, Clever Cloud handles that for us, making the deployment steps as followed: |
| 94 | + |
| 95 | + (0. send the sources) |
| 96 | + 1. build the app |
| 97 | + 2. run the app |
| 98 | + |
| 99 | +:::note |
| 100 | +_Clever Cloud_ listens on port `8080` by default which is also Dart Frog's default port, it works out of the box! |
| 101 | +::: |
| 102 | + |
| 103 | +In order to tell _Clever Cloud_ how to build our project, we will need to create a `Dockerfile`, because the one created by `dart_frog build` is a built item and not sent to _Clever Cloud_ with _git_. |
| 104 | + |
| 105 | +Here is the `Dockerfile` you can add at the root of your project. |
| 106 | + |
| 107 | +```docker |
| 108 | +# This stage will compile sources to get the build folder |
| 109 | +FROM dart:stable AS build |
| 110 | +
|
| 111 | +# Install the dart_frog cli from pub.dev |
| 112 | +RUN dart pub global activate dart_frog_cli |
| 113 | +
|
| 114 | +# Set the working directory |
| 115 | +WORKDIR /app |
| 116 | +
|
| 117 | +# Copy Dependencies in our working directory |
| 118 | +COPY pubspec.* /app/ |
| 119 | +COPY routes /app/routes |
| 120 | +# Uncomment the following line if you are serving static files. |
| 121 | +# COPY --from=build public /app/public |
| 122 | +
|
| 123 | +# Add all of your custom directories here, for example if you have a "models" directory: |
| 124 | +# COPY models /app/models |
| 125 | +
|
| 126 | +# Get dependencies |
| 127 | +RUN dart pub get |
| 128 | +
|
| 129 | +# 📦 Create a production build |
| 130 | +RUN dart_frog build |
| 131 | +
|
| 132 | +# Compile the server to get the executable |
| 133 | +RUN dart compile exe /app/build/bin/server.dart -o /app/build/bin/server |
| 134 | +
|
| 135 | +# Build minimal serving image from AOT-compiled `/server` and required system |
| 136 | +# libraries and configuration files stored in `/runtime/` from the build stage. |
| 137 | +FROM scratch |
| 138 | +
|
| 139 | +COPY --from=build /runtime/ / |
| 140 | +COPY --from=build /app/build/bin/server /app/bin/server |
| 141 | +# Uncomment the following line if you are serving static files. |
| 142 | +# COPY --from=build /app/build/public /public/ |
| 143 | +
|
| 144 | +# Expose the server port (useful for binding) |
| 145 | +EXPOSE 8080 |
| 146 | +
|
| 147 | +# Run the server |
| 148 | +CMD ["/app/bin/server"] |
| 149 | +``` |
| 150 | + |
| 151 | +:::tip |
| 152 | +If you already have a `Dockerfile` you can rename it and specify to _Clever Cloud_ that the docker file to run is not `Dockerfile`: |
| 153 | + |
| 154 | +```bash |
| 155 | +clever env set CC_DOCKERFILE <the name of your file> |
| 156 | +``` |
| 157 | + |
| 158 | +::: |
| 159 | + |
| 160 | +Add the newly created `Dockerfile` to `git` via `git add Dockerfile`, commit and then run: |
| 161 | + |
| 162 | +```bash |
| 163 | +clever deploy |
| 164 | +``` |
| 165 | + |
| 166 | +Your **source code** will be sent to Clever Cloud. |
| 167 | + |
| 168 | +:::warning |
| 169 | +Consider Clever Cloud like a new [remote (from a git point of view)](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes) and clever deploy a git push. |
| 170 | +::: |
| 171 | + |
| 172 | +And watch the magic happening |
| 173 | + |
| 174 | +``` |
| 175 | +App is brand new, no commits on remote yet |
| 176 | +New local commit to push is XXX (from refs/heads/main) |
| 177 | +Pushing source code to Clever Cloud… |
| 178 | +Your source code has been pushed to Clever Cloud. |
| 179 | +Waiting for deployment to start… |
| 180 | +Deployment started (deployment_XXX) |
| 181 | +Waiting for application logs… |
| 182 | +... |
| 183 | +... |
| 184 | +Deployment successful |
| 185 | +``` |
| 186 | + |
| 187 | +### 3. Enjoy your API on Clever Cloud! 🎉 |
| 188 | + |
| 189 | +You have successfully built and deployed your API to _Clever Cloud_ |
| 190 | + |
| 191 | +To access your app you can go to |
| 192 | + |
| 193 | +`https://app-\<your app id\>.cleverapps.io/` |
| 194 | + |
| 195 | +:::tip |
| 196 | +Your app's id is in the `.clever.json` file. |
| 197 | + |
| 198 | +Please note that the app id is `app_XX` and the url is `app-XX` (underscore in file, hyphen in url). |
| 199 | +::: |
| 200 | + |
| 201 | +## Additional Resources |
| 202 | + |
| 203 | +- [What is Clever Cloud](https://www.clever-cloud.com/presentation/) |
| 204 | +- [Clever Cloud Pricing](https://www.clever-cloud.com/pricing/) |
| 205 | +- [Clever Cloud Doc](https://www.clever-cloud.com/doc/) |
| 206 | +- [Clever Cloud CLI](https://www.clever-cloud.com/doc/getting-started/cli/) |
0 commit comments