R
Railwayβ€’13mo ago
misimilen

Point to a railway.json in a subfolder

I have a NX monorepo with multiple projects. Should I have one railway.app json in each project? How can I tell the service in railway.app which config file to use?
51 Replies
Percy
Percyβ€’13mo ago
Project ID: N/A
misimilen
misimilenβ€’13mo ago
N/A
Brody
Brodyβ€’13mo ago
what kind of configurations are you needing to do in the railway.json files?
misimilen
misimilenβ€’13mo ago
I think I will need to tell railway to deploy our app using nodejs instead of Docker which its currently configured as because it autodetected a Dockerfile in the repo and I couldnt find anywhere to change that in the UI
Brody
Brodyβ€’13mo ago
if there is a dockerfile, the only way to change that would be to remove or rename it
misimilen
misimilenβ€’13mo ago
Hmm ok so railway.json isnt able to override it?
Brody
Brodyβ€’13mo ago
a Dockerfile will always be used, theres no way to change that besides removing or renaming
misimilen
misimilenβ€’13mo ago
Thanks, now it shows more things like start command and such πŸ™‚ One last thing, is there a similar setting to fly.io's release_command which is suited to run for example database migrations? https://fly.io/docs/reference/configuration/#run-one-off-commands-before-releasing-a-deployment
Brody
Brodyβ€’13mo ago
at what point during the build or deployment would you want to run your desired command?
misimilen
misimilenβ€’13mo ago
Hmm I guess just before the app will be deployed. In our case it's a prisma migrations so we currently run prisma migrate deploy and if it's successful(exit code 0) only then the start cmd is ran (node build/index.js)
Brody
Brodyβ€’13mo ago
would running the migration during build work for you? like as the last step of the build before the image is published and then deployed
misimilen
misimilenβ€’13mo ago
Hmm yea I guess that could work. I will try! 😊
Brody
Brodyβ€’13mo ago
haha did you want me to show you how to set that up?
misimilen
misimilenβ€’13mo ago
well, for that a railway.json is needed right?
Brody
Brodyβ€’13mo ago
yeah it can be done in a railway.json or a nixpacks.toml
misimilen
misimilenβ€’13mo ago
Which is preferred?
Brody
Brodyβ€’13mo ago
well you mentioned having to point to a specific config file in a subfolder, and i dont think you can do that with a railway.json, but you can with a nixpacks.toml can you send me the build table at the top of the build logs and the command you want to run
misimilen
misimilenβ€’13mo ago
╔══════════════════ Nixpacks v1.17.0 ══════════════════╗

β•‘ setup β”‚ nodejs_20, npm-9_x, openssl β•‘

║──────────────────────────────────────────────────────║

β•‘ install β”‚ npm ci β•‘

║──────────────────────────────────────────────────────║

β•‘ build β”‚ npm run build β•‘

║──────────────────────────────────────────────────────║

β•‘ start β”‚ node ./packages/backend/build/index.js β•‘

β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
╔══════════════════ Nixpacks v1.17.0 ══════════════════╗

β•‘ setup β”‚ nodejs_20, npm-9_x, openssl β•‘

║──────────────────────────────────────────────────────║

β•‘ install β”‚ npm ci β•‘

║──────────────────────────────────────────────────────║

β•‘ build β”‚ npm run build β•‘

║──────────────────────────────────────────────────────║

β•‘ start β”‚ node ./packages/backend/build/index.js β•‘

β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
The migrate command I wish to run is npx prisma migrate deploy with CWD in ./packages/backend. I would also like to run this with node 18.
Brody
Brodyβ€’13mo ago
The migrate command I wish to run is npx prisma migrate deploy with CWD in ./packages/backend.
okay so what command will execute npx prisma migrate deploy in ./packages/backend without using cd
I would also like to run this with node 18.
set engines.node to 18 in your package.json
misimilen
misimilenβ€’13mo ago
Not sure I understand, but I guess we would have to put cd ./packages/backend && npx prisma migrate deploy somewhere, maybe in the railway.json file?
Brody
Brodyβ€’13mo ago
i think i said without using cd do you have a migrate script in your package.json
misimilen
misimilenβ€’13mo ago
no, but i could just add it like so "migrate-backend1": "cd ./packages/backend && npx prisma migrate deploy" right.
Brody
Brodyβ€’13mo ago
without using cd
misimilen
misimilenβ€’13mo ago
because we have a nx monorepo it's contained within that package
Brody
Brodyβ€’13mo ago
i know
misimilen
misimilenβ€’13mo ago
so it needs to be like executed from that context
Brody
Brodyβ€’13mo ago
i know how many apps do you have in the monorepo
misimilen
misimilenβ€’13mo ago
right now only a backend and frontend be we do got other services in other repos
Brody
Brodyβ€’13mo ago
how many railway services do you have
misimilen
misimilenβ€’13mo ago
just a postgres and a compute service im trying out railway so I set it up today
Brody
Brodyβ€’13mo ago
ignoring databases, if this repo contains two apps, you need two railway services, one service for the frontend, and one service for the backend
misimilen
misimilenβ€’13mo ago
the frontend is running at Vercel so currently it's only the one backend app I want to launch on railway
Brody
Brodyβ€’13mo ago
okay thanks for the clarification add a migrate:backend script to your package.json prisma migrate deploy then add this nixpacks.toml file to your project
[phases.migrate]
dependsOn = ['build']
cmds = ['npm run migrate:backend --prefix packages/backend']
[phases.migrate]
dependsOn = ['build']
cmds = ['npm run migrate:backend --prefix packages/backend']
misimilen
misimilenβ€’13mo ago
That's the ./package.json and not the ./packages/backend/package.json?
Brody
Brodyβ€’13mo ago
why not just tell railway to start from within /packages/backend??
misimilen
misimilenβ€’13mo ago
because we also have some common packages that's shared between frontend and backend so we need to build the backend from the root. it's how nx wants us to do it πŸ™‚
Brody
Brodyβ€’13mo ago
pain then yes, package.json at the root
misimilen
misimilenβ€’13mo ago
wait a minute.. now I remember. The npm i has to be performed from the root because nx hoists shared packages from all packages in the monorepo but the build can be started from packages/backend without any problem even though it references packages outside.
Brody
Brodyβ€’13mo ago
well that doesnt really help, you cant tell railway what directory to use for each step, its a one time setting can you send your package.json the one from the root
misimilen
misimilenβ€’13mo ago
{
"name": "monorepo",
"private": true,
"scripts": {
"lint": "eslint .",
"prettier": "prettier . --check",
"prettier:fix": "prettier . --write",
"check": "npm run lint && npm run prettier && npm run build",
"dev": "concurrently --kill-others \"npm:dev:frontend\" \"npm:dev:backend\" \"npm:ngrok\"",
"dev:frontend": "cd packages/frontend && npm run dev",
"dev:backend": "cd packages/backend && npm run dev",
"start": "npx nx run-many -t start",
"build": "npx nx run-many -t build",
"prisma:push": "cd packages/backend && npx prisma db push",
"prisma:generate": "cd packages/backend && npx prisma generate",
"prisma:migrate": "cd packages/backend && npx prisma migrate dev",
"prisma:seed": "cd packages/backend && npx prisma db seed",
"ngrok": "ngrok http --domain=XXXXXXX 3000"
},
"engines": {
"node": ">=18"
},
"version": "0.0.0",
"devDependencies": {
"@types/node": "^20.5.1",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"concurrently": "^8.2.0",
"eslint": "^8.47.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"eslint-plugin-unicorn": "^48.0.1",
"prettier": "3.0.2"
},
"workspaces": [
"packages/*"
],
"dependencies": {
"nx": "^16.7.2",
"type-fest": "^4.2.0",
"typescript": "^5.1.6"
}
}
{
"name": "monorepo",
"private": true,
"scripts": {
"lint": "eslint .",
"prettier": "prettier . --check",
"prettier:fix": "prettier . --write",
"check": "npm run lint && npm run prettier && npm run build",
"dev": "concurrently --kill-others \"npm:dev:frontend\" \"npm:dev:backend\" \"npm:ngrok\"",
"dev:frontend": "cd packages/frontend && npm run dev",
"dev:backend": "cd packages/backend && npm run dev",
"start": "npx nx run-many -t start",
"build": "npx nx run-many -t build",
"prisma:push": "cd packages/backend && npx prisma db push",
"prisma:generate": "cd packages/backend && npx prisma generate",
"prisma:migrate": "cd packages/backend && npx prisma migrate dev",
"prisma:seed": "cd packages/backend && npx prisma db seed",
"ngrok": "ngrok http --domain=XXXXXXX 3000"
},
"engines": {
"node": ">=18"
},
"version": "0.0.0",
"devDependencies": {
"@types/node": "^20.5.1",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"concurrently": "^8.2.0",
"eslint": "^8.47.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"eslint-plugin-unicorn": "^48.0.1",
"prettier": "3.0.2"
},
"workspaces": [
"packages/*"
],
"dependencies": {
"nx": "^16.7.2",
"type-fest": "^4.2.0",
"typescript": "^5.1.6"
}
}
Brody
Brodyβ€’13mo ago
i see a lot of cd 😬
misimilen
misimilenβ€’13mo ago
all of the scripts in there are basically only used during dev that's problematic?
Brody
Brodyβ€’13mo ago
its not ideal / a proper solution are you aware of the prefix flag for npm?
misimilen
misimilenβ€’13mo ago
Brody
Brodyβ€’13mo ago
yes remove all the cd stuff and move over to using --prefix let me know when thats done!
misimilen
misimilenβ€’13mo ago
hmm, it doesn't look like it's the one I linked to. --prefix seems to be a hidden and obscure flag according to this https://github.com/npm/cli/issues/1368 and should be avoided, some people say in the comments.
Brody
Brodyβ€’13mo ago
looks like everyone in that thread is just using it for the wrong reasons, but okay, does nx have any way to run commands in subdirectorys turbo does, surely nx must
misimilen
misimilenβ€’13mo ago
not sure tbh, im not fond of nx 😦
Brody
Brodyβ€’13mo ago
move to turbo!
misimilen
misimilenβ€’13mo ago
in the long run ya maybe πŸ˜„ for now im using nx, but I "gave up" and just started my app like this now and it almost seems to work: npm run prisma:deploy && node ./packages/backend/build/index.js
Brody
Brodyβ€’13mo ago
that works too
Want results from more Discord servers?
Add your server