R
Railwayā€¢8mo 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ā€¢8mo ago
Project ID: N/A
coltonidle
coltonidleā€¢8mo ago
N/A
coltonidle
coltonidleā€¢8mo 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ā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
wdym version control? like github?
coltonidle
coltonidleā€¢8mo ago
yeah
Brody
Brodyā€¢8mo 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ā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
very cool. thanks for teaching. cant seem to get railway to use my /server directory. lmao
coltonidle
coltonidleā€¢8mo ago
No description
Brody
Brodyā€¢8mo ago
what makes you think that?
coltonidle
coltonidleā€¢8mo ago
No description
Brody
Brodyā€¢8mo ago
haha okay well more specifically?
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
what did you set that NIXPACKS_CONFIG_FILE to?
coltonidle
coltonidleā€¢8mo ago
No description
Brody
Brodyā€¢8mo ago
its blue have you not deployed the changes?
coltonidle
coltonidleā€¢8mo ago
I did, I was just changing things back and forth again Tried /server /server/nixpacks.toml /nixpacks.toml
Brody
Brodyā€¢8mo 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ā€¢8mo ago
this is my /server
No description
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
you completely removed the NIXPACKS_CONFIG_FILE variable from your service? but those logs would indicate that its still set
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
what two apps are in this repo?
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
interesting
coltonidle
coltonidleā€¢8mo ago
but yeah. its not a completely isolated project i.e. isolated monorepo vs shared monorepo
Brody
Brodyā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
very odd
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
very wrong did gpt write that for you
Brody
Brodyā€¢8mo 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ā€¢8mo ago
bah. wheres the nixpacks docs. lmaooo
Brody
Brodyā€¢8mo ago
we can pivot to a railway.toml if thats what you'd like to do? https://nixpacks.com/docs/configuration/file
coltonidle
coltonidleā€¢8mo ago
i guess i dont really care. i was just using what i learned in the last thread (from aleks)
Brody
Brodyā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
the $schema value links to the schema file and then vscode tells you if you did something very wrong
coltonidle
coltonidleā€¢8mo ago
maybe railway.json will be a project for tomorrow. dont use vscode (using jetbrains fleet) lol.
Brody
Brodyā€¢8mo ago
i cant see why it wouldnt do json validation as well
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
i like railway.json by far, since you can embed a nixpacks.toml file into it and have schema validation!
Duchess
Duchessā€¢8mo 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ā€¢8mo ago
Alright. Going to try to convert to railway.json i guess if railway.json exists, it'll just use it?
Brody
Brodyā€¢8mo ago
correct
coltonidle
coltonidleā€¢8mo ago
because i just converted the file and everything seems to be building fine cool
Brody
Brodyā€¢8mo ago
lets see the file you came up with
coltonidle
coltonidleā€¢8mo ago
should i also just add the rest of this stuff?
No description
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
its not needed, unless you want to set that stuff from your railway.json file
coltonidle
coltonidleā€¢8mo ago
sent some trains over. šŸ’ø cheers buddy
Brody
Brodyā€¢8mo ago
ah thanks ā¤ļø
coltonidle
coltonidleā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
i guess thats validation in action?
No description
Brody
Brodyā€¢8mo 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ā€¢8mo ago
awesome. gonna try the port number thing and get some sleep. hopefully its easy peasy
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
even though the docs for that seem wrong? i think its a copy pasta issue
Brody
Brodyā€¢8mo ago
nope, just PORT
coltonidle
coltonidleā€¢8mo ago
hm. howd you find that out? lol. sorry i try to find docs, but did i miss something here?
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
do you have a tcp proxy on your service? if you do, you shouldn't
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
ill refer back to the convo we had about health check or w/e
Brody
Brodyā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
so the goal here would be to not have a PORT variable defined on your service
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
BLAH?
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
gotcha
coltonidle
coltonidleā€¢8mo ago
which i thought it did. but it was the previoius instance still working
Brody
Brodyā€¢8mo ago
so just to check, does your railway service have a PORT service variable set?
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
good
coltonidle
coltonidleā€¢8mo 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ā€¢8mo 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ā€¢8mo ago
yeah its super handy how railway does that the switch over time
coltonidle
coltonidleā€¢8mo ago
gotcha. so its not my server code initialization actually taking 30 seconds. lol
Brody
Brodyā€¢8mo ago
nope im sure that time will go down when railway rolls out their new http server
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
great!
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
happy i could help
coltonidle
coltonidleā€¢8mo ago
1/1 replicas never became healthy! Healthcheck failed!
never been so happy to see a failure lol
Brody
Brodyā€¢8mo ago
lol i assume you planned that
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
no problem!
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
RAILWAY_DEPLOYMENT_OVERLAP_SECONDS set to 35 should do the trick
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
unfortunately that looks like the best we can do with the current http proxy
coltonidle
coltonidleā€¢8mo 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ā€¢8mo ago
sounds good!
Want results from more Discord servers?
Add your server