Deploying Ktor onto Railway isn’t too popular, but I have found two different ways to do it.
Option 1
use https://start.ktor.io/ and just download the baseline project there
On railway set build step to ./gradlew buildFatJar
On railway set deploy step to ./gradlew runFatJar
On railway Set a variable of PORT to 8080
Note: In railway free tier, ./gradlew runFatJar seems to sometimes time-out so you can instead use java -jar thejarname-all.jar seems to work fine without causing issues
Option 2
use https://start.ktor.io/ and just download the baseline project there
On railway set PORT variable to 8080
Add a nixpacks.toml to the root of your project containing…
cmd = ‘java $JAVA_OPTS -jar build/libs/*-all.jar’
(Basically the same as setting the start command in option 1)
Option 3
Bonus tips:
Downtime/503’s should be non-existent if you use railway’s health-check feature + set the overlap time. This will make sure that your old service is still running until the new service is actually up and running.
Set a healthcheck path at railway > [Your Project] > Settings > Healthcheck https://docs.railway.app/reference/healthchecks
Set the RAILWAY_DEPLOYMENT_OVERLAP_SECONDS more info
You can skip setting a PORT variable in Option 1, 2 and railway will randomly assign one. Then in your ktor code you can use System.getenv(“PORT”) to get the port from railway.
Instead of using nixpacks.toml you can use railway.json since it will do schema validation for you.
“$schema”: “https://schema.up.railway.app/railway.schema.json”,
“build”: {
“builder”: “NIXPACKS”,
“buildCommand”: “./gradlew -p server clean build -x check -x test”
},
“deploy”: {
“startCommand”: “java $JAVA_OPTS -jar server/build/libs/*-all.jar”
}
}