R
Railwayβ€’3mo ago
coltonidle

How to use railway with monorepo?

I created a new project and essentially my server code is not at the top level, and instead is one directory down. How do I configure this with railway?
104 Replies
Percy
Percyβ€’3mo ago
Project ID: N/A
coltonidle
coltonidleβ€’3mo ago
N/A
coltonidle
coltonidleβ€’3mo ago
should have known there was going to be a setting for this. but in my defense... i did try to search for it and i got no hits. but of course i try now and its there in settings. πŸ€¦β€β™‚οΈ thanks Brody! I wonder if i have to move my custom nixpack.toml into the new root folder... hmmmm
Brody
Brodyβ€’3mo ago
you would likely have the nixpacks.toml in the subfolder and then set the location to it with the NIXPACKS_CONFIG_FILE variable
coltonidle
coltonidleβ€’3mo ago
thanks. just moved it to the subfolder, so I will give that a shot. still find it weird that variables cant be in version control (like PORT 8080)
Brody
Brodyβ€’3mo ago
wdym version control? like github?
coltonidle
coltonidleβ€’3mo ago
yeah
Brody
Brodyβ€’3mo ago
its a variable that controls the platform, so it needs to be set on the platform itself but side node, there would be no need for you to set a PORT variable yourself as long as your app listens on the automatically generated PORT variable
coltonidle
coltonidleβ€’3mo ago
oh hmmm. i wonder if i can do that on ktor. i guess the auto generated port variable is set as an env variable?
Brody
Brodyβ€’3mo ago
yep it would be an environment variable, and yes you can do it with ktor, remove the PORT variable you set, now railway will randomly assign one behind the scenes, now all your app has to do is read the PORT environment variable and listen on it
coltonidle
coltonidleβ€’3mo ago
very cool. thanks for teaching. cant seem to get railway to use my /server directory. lmao
coltonidle
coltonidleβ€’3mo ago
No description
Brody
Brodyβ€’3mo ago
what makes you think that?
coltonidle
coltonidleβ€’3mo ago
No description
Brody
Brodyβ€’3mo ago
haha okay well more specifically?
coltonidle
coltonidleβ€’3mo ago
Error: Failed to read Nixpacks config file nixpacks.toml Caused by: 0: Error reading nixpacks.toml 1: No such file or directory (os error 2)
Brody
Brodyβ€’3mo ago
what did you set that NIXPACKS_CONFIG_FILE to?
coltonidle
coltonidleβ€’3mo ago
No description
Brody
Brodyβ€’3mo ago
its blue have you not deployed the changes?
coltonidle
coltonidleβ€’3mo ago
I did, I was just changing things back and forth again Tried /server /server/nixpacks.toml /nixpacks.toml
Brody
Brodyβ€’3mo ago
second option seems like the best but without seeing your project structure i dont know for sure oh maybe you dont need to set that variable if the root directory is set, try omitting it
coltonidle
coltonidleβ€’3mo ago
this is my /server
No description
coltonidle
coltonidleβ€’3mo ago
Nope. same issue even if i remove the env variable. already tried that, and just tried again Error: Failed to read Nixpacks config file nixpacks.toml Caused by: 0: Error reading nixpacks.toml 1: No such file or directory (os error 2)
Brody
Brodyβ€’3mo ago
you completely removed the NIXPACKS_CONFIG_FILE variable from your service? but those logs would indicate that its still set
coltonidle
coltonidleβ€’3mo ago
you know what. i bet you i know the issue i dont think i have a "true" monorepo the root project is still a gradle project. so i probably actually dont want to change root, but I instead might want to move the nixpacks.toml.
Brody
Brodyβ€’3mo ago
what two apps are in this repo?
coltonidle
coltonidleβ€’3mo ago
i used kmp.jetbrains.com to generate the project and so i have two mobile apps (ios and android) and a server directory (ktor)
Brody
Brodyβ€’3mo ago
interesting
coltonidle
coltonidleβ€’3mo ago
but yeah. its not a completely isolated project i.e. isolated monorepo vs shared monorepo
Brody
Brodyβ€’3mo ago
yeah this is more like a shared monorepo, and in that case you liking don't want to set a root directory
coltonidle
coltonidleβ€’3mo ago
doesn't seem like its picking up my nixpacks.toml even though its in the root of the project. do i need to set a variable even if its in the root? yeah. if i just set the build command directly in the railway dashboard then things build properly. not sure why it doesn't like my nixpacks.toml
Brody
Brodyβ€’3mo ago
very odd
coltonidle
coltonidleβ€’3mo ago
ill figure it out. im gettin good at this stuff. πŸ™‚ rubberducking... i think my nixpacks.toml file is wrong
[build]
buildCommand = './gradlew -p server clean build -x check -x test'

[deploy]
startCommand = 'java $JAVA_OPTS -jar server/build/libs/*-all.jar'
[build]
buildCommand = './gradlew -p server clean build -x check -x test'

[deploy]
startCommand = 'java $JAVA_OPTS -jar server/build/libs/*-all.jar'
Brody
Brodyβ€’3mo ago
very wrong did gpt write that for you
Brody
Brodyβ€’3mo ago
[phases.build]
cmds = ['./gradlew -p server clean build -x check -x test']

[start]
cmd = ['java $JAVA_OPTS -jar server/build/libs/*-all.jar']
[phases.build]
cmds = ['./gradlew -p server clean build -x check -x test']

[start]
cmd = ['java $JAVA_OPTS -jar server/build/libs/*-all.jar']
thats for a railway.toml
coltonidle
coltonidleβ€’3mo ago
bah. wheres the nixpacks docs. lmaooo
Brody
Brodyβ€’3mo ago
we can pivot to a railway.toml if thats what you'd like to do? https://nixpacks.com/docs/configuration/file
coltonidle
coltonidleβ€’3mo ago
i guess i dont really care. i was just using what i learned in the last thread (from aleks)
Brody
Brodyβ€’3mo ago
maybe a railway.json would even be best? since it has schema validation
{
"$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"
}
}
{
"$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"
}
}
coltonidle
coltonidleβ€’3mo ago
interesting. how does it do schema validation? on the railway side? or can i somehow validate locally? but anyway. yeah. i did what you said and it worked
Brody
Brodyβ€’3mo ago
the $schema value links to the schema file and then vscode tells you if you did something very wrong
coltonidle
coltonidleβ€’3mo ago
maybe railway.json will be a project for tomorrow. dont use vscode (using jetbrains fleet) lol.
Brody
Brodyβ€’3mo ago
i cant see why it wouldnt do json validation as well
coltonidle
coltonidleβ€’3mo ago
gotta shut the laptop for a bit. but. let me know if you have any pref between doing a railway.json/toml vs a nixpacks.toml i am a big fan of config as code. so i want to do that where possible.
Brody
Brodyβ€’3mo ago
i like railway.json by far, since you can embed a nixpacks.toml file into it and have schema validation!
Duchess
Duchessβ€’3mo ago
New reply sent from Help Station thread:
At the same time, though, your nixpacks settings in railway.json will only be respected by Railway's nixpacks, so local nixpacks builds won't respect those settings. If that's not a concern, though, JSON schema validation is always great.
You're seeing this because this thread has been automatically linked to the Help Station thread.
coltonidle
coltonidleβ€’3mo ago
Alright. Going to try to convert to railway.json i guess if railway.json exists, it'll just use it?
Brody
Brodyβ€’3mo ago
correct
coltonidle
coltonidleβ€’3mo ago
because i just converted the file and everything seems to be building fine cool
Brody
Brodyβ€’3mo ago
lets see the file you came up with
coltonidle
coltonidleβ€’3mo ago
should i also just add the rest of this stuff?
No description
coltonidle
coltonidleβ€’3mo ago
I just plugged in the file you passed over πŸ₯°
{
"$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"
}
}
{
"$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"
}
}
Brody
Brodyβ€’3mo ago
its not needed, unless you want to set that stuff from your railway.json file
coltonidle
coltonidleβ€’3mo ago
sent some trains over. πŸ’Έ cheers buddy
Brody
Brodyβ€’3mo ago
ah thanks ❀️
coltonidle
coltonidleβ€’3mo ago
im curious now about this json validation thing. ive never heard of that in my life. lol not sure if jetbrains fleet is doing any sort of validation for me lol
Brody
Brodyβ€’3mo ago
hey, my first introduction to it was when i edited my first railway.json too! i can't imagine fleet not doing the schema validation
coltonidle
coltonidleβ€’3mo ago
i guess thats validation in action?
No description
Brody
Brodyβ€’3mo ago
yeah thats it, its not gonna help you make sure what you do is going to do what you want, but it will let you know if what youre doing is not valid
coltonidle
coltonidleβ€’3mo ago
awesome. gonna try the port number thing and get some sleep. hopefully its easy peasy
coltonidle
coltonidleβ€’3mo ago
https://docs.railway.app/reference/variables#railway-provided-variables Assuming I'm after RAILWAY_TCP_APPLICATION_PORT
Railway Docs
Variables | Railway Docs
Documentation for Railway
coltonidle
coltonidleβ€’3mo ago
even though the docs for that seem wrong? i think its a copy pasta issue
Brody
Brodyβ€’3mo ago
nope, just PORT
coltonidle
coltonidleβ€’3mo ago
hm. howd you find that out? lol. sorry i try to find docs, but did i miss something here?
coltonidle
coltonidleβ€’3mo ago
thanks. for whatever reason... it seems like RAILWAY_TCP_APPLICATION_PORT also worked? unless it actually just defaulted to 8080 and that worked. going to switch to plain ol "PORT" now... 🀞 oooh. very interesting. PORT didn't work...
Brody
Brodyβ€’3mo ago
do you have a tcp proxy on your service? if you do, you shouldn't
coltonidle
coltonidleβ€’3mo ago
i dont believe so actually wait. i think it did work but i had downtime. which caused me to think it didn't work.
coltonidle
coltonidleβ€’3mo ago
ill refer back to the convo we had about health check or w/e
Brody
Brodyβ€’3mo ago
i think this is close enough to what you're running? a health check and RAILWAY_DEPLOYMENT_OVERLAP_SECONDS set to 35 is what the team (and me) are currently recommending until railway rolls out the new and faster http proxy
coltonidle
coltonidleβ€’3mo ago
okay. so RAILWAY_TCP_APPLICATION_PORT doesn't actually work. it was because if i can't find the env variable defined, then it chooses 8080 and somehow using 8080 as a default works now. or actually. nope.
Brody
Brodyβ€’3mo ago
so the goal here would be to not have a PORT variable defined on your service
coltonidle
coltonidleβ€’3mo ago
looks like its just a nice long delay again that was misleading me. I'm just doing this
fun main() {
val serverPort: String =
try {
System.getenv("BLAH")
} catch (e: Exception) {
"8080"
}

embeddedServer(Netty, port = serverPort.toInt(), host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
fun main() {
val serverPort: String =
try {
System.getenv("BLAH")
} catch (e: Exception) {
"8080"
}

embeddedServer(Netty, port = serverPort.toInt(), host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
Brody
Brodyβ€’3mo ago
BLAH?
coltonidle
coltonidleβ€’3mo ago
so basically. before I just had val serverPort: String = "8080" BLAH is just me trying to break stuff and seeing if it works
Brody
Brodyβ€’3mo ago
gotcha
coltonidle
coltonidleβ€’3mo ago
which i thought it did. but it was the previoius instance still working
Brody
Brodyβ€’3mo ago
so just to check, does your railway service have a PORT service variable set?
coltonidle
coltonidleβ€’3mo ago
nope variables are empty at the moment switching back over to val serverPort: String = try { System.getenv("PORT") } catch (e: Exception) { "8080" }
Brody
Brodyβ€’3mo ago
good
coltonidle
coltonidleβ€’3mo ago
and will see if that works cool. yep that works. no PORT variable declaration needed. noice. another thing that can be tucked away as code. thanks!
coltonidle
coltonidleβ€’3mo ago
i do wonder why my ktor server somehow says it starts up at 55:44, but can't actually handle requests until about 30 seconds later. at 56:19
No description
Brody
Brodyβ€’3mo ago
yeah its super handy how railway does that the switch over time
coltonidle
coltonidleβ€’3mo ago
gotcha. so its not my server code initialization actually taking 30 seconds. lol
Brody
Brodyβ€’3mo ago
nope im sure that time will go down when railway rolls out their new http server
coltonidle
coltonidleβ€’3mo ago
cool cool. thats fine. nice to at least know that its out of my control. running a few more sanity checks with healthcheck endpoint and stuff, but i think im all set 🀞
Brody
Brodyβ€’3mo ago
great!
coltonidle
coltonidleβ€’3mo ago
oh yahhh. works great. awesome. so many great little tips learned today. appreciate it. im essentially getting to a point where i can have a small template for all future ktor projects. love to see it.
Brody
Brodyβ€’3mo ago
happy i could help
coltonidle
coltonidleβ€’3mo ago
1/1 replicas never became healthy! Healthcheck failed!
never been so happy to see a failure lol
Brody
Brodyβ€’3mo ago
lol i assume you planned that
coltonidle
coltonidleβ€’3mo ago
yep System.getenv("BLAH") just making sure the healthcheck worked as expected when it defaulted to 8080 and couldn't actually startup thanks again. night πŸ’€
Brody
Brodyβ€’3mo ago
no problem!
coltonidle
coltonidleβ€’3mo ago
heh. i was able to see the railway 404 page or w/e even with the healthcheck and timeout of 500. no storage or db attached to this server.
Attempt #1 failed with service unavailable. Continuing to retry for 4m59s [1/1] Healthcheck succeeded!
so for a split second my server went down. you think im missing anything else here? (i know the last time this happened we narrowed it down to the fact that i had some storage attached). but none of that this time.
Brody
Brodyβ€’3mo ago
RAILWAY_DEPLOYMENT_OVERLAP_SECONDS set to 35 should do the trick
coltonidle
coltonidleβ€’3mo ago
aha. so thats diff from "Healthcheck Timeout" yep. that seemed to do the trick. couldn't repro the railway 404 page. excellent. ill make sure to add that to my default template as well (see, thats something id love to add to railway.json) lol. sat there again and just tried cmd + r to refresh over and over and over and i was able to see the downtime page for a split second.
Brody
Brodyβ€’3mo ago
unfortunately that looks like the best we can do with the current http proxy
coltonidle
coltonidleβ€’3mo ago
bumped to RAILWAY_DEPLOYMENT_OVERLAPSECONDS to 60. and couldn't repro. Β―_(ツ)/Β― might do a few more sanity checks tomorrow. sleep for real this time. lmao
Brody
Brodyβ€’3mo ago
sounds good!