|
| 1 | +[//]: # (title: Sevalla) |
| 2 | + |
| 3 | +<show-structure for="chapter" depth="2"/> |
| 4 | + |
| 5 | +<link-summary>Learn how to prepare and deploy a Ktor application to Sevalla.</link-summary> |
| 6 | + |
| 7 | +In this tutorial, you will learn how to prepare and deploy a Ktor application to [Sevalla](https://sevalla.com/). You can use one of the following initial projects, depending on the way used to [create a Ktor server](server-create-and-configure.topic): |
| 8 | + |
| 9 | +* [embedded-server](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/embedded-server) |
| 10 | + |
| 11 | +* [Engine-main](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/engine-main) |
| 12 | + |
| 13 | +## Prerequisites {id="prerequisites"} |
| 14 | + |
| 15 | +Before starting this tutorial, you need to [create a Sevalla account](https://sevalla.com) (comes with $50 in free credits). |
| 16 | + |
| 17 | +## Clone a sample application {id="clone-sample-app"} |
| 18 | + |
| 19 | +To open a sample application, follow the steps below: |
| 20 | + |
| 21 | +1. Clone the [Ktor documentation repository](https://github.com/ktorio/ktor-documentation). |
| 22 | +2. Open the [codeSnippets](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets) project. |
| 23 | +3. Open the [embedded-server](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/embedded-server) or [engine-main](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/engine-main) sample, which demonstrates two different approaches to setting up a Ktor server — either by configuring it directly in code or through an external configuration file. The only difference in deploying these projects is how to specify a port used to listen for incoming requests. |
| 24 | + |
| 25 | +## Prepare the application {id="prepare-app"} |
| 26 | + |
| 27 | +### Step 1: Configure the port {id="port"} |
| 28 | + |
| 29 | +Sevalla injects a random port using the `PORT` environment variable. Your application must be configured to listen on that port. |
| 30 | + |
| 31 | +If you've chosen the [embedded-server](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/embedded-server) sample with server configuration specified in code, you can obtain the environment variable value using `System.getenv()`. Open the <path>Application.kt</path> file placed in the <path>src/main/kotlin/com/example</path> folder and change the port parameter value of the `embeddedServer()` function as shown below: |
| 32 | + |
| 33 | +```kotlin |
| 34 | +fun main() { |
| 35 | + val port = System.getenv("PORT")?.toIntOrNull() ?: 8080 |
| 36 | + embeddedServer(Netty, port = port, host = "0.0.0.0") { |
| 37 | + // ... |
| 38 | + }.start(wait = true) |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +If you've chosen the [engine-main](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/engine-main) sample with server configuration specified in the <path>application.conf</path> file, you can assign the environment variable to the port parameter by using the `${ENV}` syntax. Open the <path>application.conf</path> file placed in <path>src/main/resources</path> and update it as shown below: |
| 43 | + |
| 44 | +```hocon |
| 45 | +ktor { |
| 46 | + deployment { |
| 47 | + port = 5000 |
| 48 | + port = ${?PORT} |
| 49 | + } |
| 50 | + application { |
| 51 | + modules = [ com.example.ApplicationKt.module ] |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Step 2: Add a Dockerfile {id="dockerfile"} |
| 57 | + |
| 58 | +To build and run your Ktor project on Sevalla, you need a Dockerfile. Here’s a sample Dockerfile using a multi-stage build: |
| 59 | + |
| 60 | +```docker |
| 61 | +# Stage 1: Build the app |
| 62 | +FROM gradle:8.5-jdk17-alpine AS builder |
| 63 | +WORKDIR /app |
| 64 | +COPY . . |
| 65 | +RUN gradle installDist |
| 66 | +
|
| 67 | +# Stage 2: Run the app |
| 68 | +FROM eclipse-temurin:17-jre-alpine |
| 69 | +WORKDIR /app |
| 70 | +COPY --from=builder /app/build/install/<project-name>/ ./ |
| 71 | +ENV PORT=8080 |
| 72 | +CMD ["./bin/<project-name>"] |
| 73 | +``` |
| 74 | + |
| 75 | +Make sure to replace `<project-name>` with the project name defined in your <path>settings.gradle.kts</path> file: |
| 76 | + |
| 77 | +```kotlin |
| 78 | +rootProject.name = "ktor-app" |
| 79 | +``` |
| 80 | + |
| 81 | +## Deploy the application {id="deploy-app"} |
| 82 | + |
| 83 | +Sevalla builds and deploys your application directly from a connected Git repository. This can be hosted on platforms like GitHub, GitLab, Bitbucket, or any supported Git provider. To deploy successfully, ensure your project is committed and pushed, and includes all necessary files (such as your Dockerfile, <path>build.gradle.kts</path>, and source code). |
| 84 | + |
| 85 | +To deploy the application, sign in to [Sevalla](https://sevalla.com/) and follow the steps below: |
| 86 | + |
| 87 | +1. Click **Applications -> Create an app** |
| 88 | +  |
| 89 | +2. Choose your Git repository and select the appropriate branch (usually `main` or `master`). |
| 90 | +3. Set the **application name**, select a **region**, and choose your **pod size** (you can start with 0.5 CPU / 1GB RAM). |
| 91 | +4. Click **Create**, but skip the deploy step for now |
| 92 | +  |
| 93 | +5. Go to **Settings -> Build** and click **Update Settings** under the **Build environment** card. |
| 94 | +  |
| 95 | +6. Set the build method to **Dockerfile**. |
| 96 | +  |
| 97 | +7. Confirm the **Dockerfile path** is `Dockerfile` and the **Context** is `.`. |
| 98 | +8. Return to your application's **Deployment** tab and click **Deploy**. |
| 99 | + |
| 100 | +Sevalla will clone your Git repository, build the Docker image using your Dockerfile, inject the `PORT` environment variable, and run your application. If everything is configured correctly, your Ktor app will be live at `https://<your-app>.sevalla.app`. |
0 commit comments