How to install only what's necessary when auto-deploying?
This isn't an issue with Railway. This is a question on how to optimize the install/build/deploy process for the applications I host on Railway.
I have a "shared" monorepo in which I have a
frontend
and backend
application (as well as others, but my issue can be conveyed with just these two apps). The monorepo uses pnpm
as a package manager and turbo
(https://turbo.build/repo) for building/caching.
My Github repository is connected to my Railway project, so I get those sweet auto-deploys when pushing to the main
branch.
When I push to the main
branch, the Railway service connected to the frontend
application runs pnpm install
, which installs all the dependencies in the repository -- all of those of frontend
, backend
, and all internal packages. The same happens with the service connected to the backend
application.
It's inefficient because the frontend
service installs backend
-only dependencies (prisma
, @nestjs
, etc.) and the backend
service installs frontend
-only dependencies (react
, @remix-run
, etc.).
The question I have: is there a way to prevent the service from installing dependencies that aren't required for the given application?
I'm investigating turbo prune
(https://turbo.build/repo/docs/reference/command-line-reference/prune).
When I run turbo prune frontend
locally, it isolates the frontend
application and all its dependencies into an out
directory. I can then cd out
and run pnpm install
to install only the dependencies for the frontend
app (e.g. react
and @remix-run
but not prisma
and @nestjs
). And finally I can run the build
and start
scripts to get the frontend
running without a problem.
My issue comes when trying to use turbo prune
when deploying via Railway. When I push to main
, Railway immediately runs pnpm install
and so I can't do turbo prune frontend && cd out
before that pnpm install
happens.
(Reaching character limit, see more in thread)7 Replies
Project ID:
67e6b82d-d699-41f6-a415-466ef9889928
Project ID:
67e6b82d-d699-41f6-a415-466ef9889928
So then I believe my options are:
Option 1: Nope can't do anything about it
Option 2:
Commit the out
directory from turbo run frontend
to the Github repo, then configure the Root Directory (https://docs.railway.app/guides/builds#build-command) of the frontend
Railway service to use the out
directory. This isn't ideal because (1) I'd have to remember to generate the out
directory before I push to main
, and (2) I commit duplicative deployment-specific code to my Github repo.
Option 3:
I don't know if this is feasible, but I run turbo prune frontend
in a Github Action to generate the out
directory, and then (somehow, IDK if using the Railway CLI in a Github Action is possible/recommended) have the frontend
Railway service use that out
directory.
Option 4: You help me lol.
Thanks in advance for any thoughts/help in figuring this out.
Also there's Option 5: Use a custom Dockerfile (https://docs.railway.app/guides/dockerfiles) but that's outside my expertise so that'll be fun to figure out.first off, this is a well put together question, you've articulated all your questions perfectly!
second, option 5 would be your absolute best bet and would allow you to very easily copy only the stuff you need into the image, it also allows you to run any commands you want during build, like the prune commands you mentioned. watch a youtube video or two and you will be an expert in no time!
Haha thank you thank you, I definitely spent like an hour putting this together to make things as easy for all of us, especially you Brody since I figured it'd be you to help me out given your history as this server's MVP π
I eventually figured the custom Dockerfile would be the best option, just needed to confirm haha. Thanks for the advice and all that you do.
i know how to write dockerfiles, but i dont know turbo so i wouldnt be too much help writing you one for this usecase, but id be happy to sanity check anything you come up with!
Oooh thanks again, will lyk if I run into trouble with it π
sounds good!