a day ago
I have a monorepo with 2 services - a backend which fetches data and provides it as an API and a frontend which uses the API at build-time to do SSG and produce a static website.
If I push a commit that adds new data to the backend and also uses that new data in the frontend, the services deploy concurrently. I cannot be sure that the frontend build will be able to get the new data from the backend, because the backend is also building.
I need to make the frontend build happen after the backend completes. An option to configure them to happen sequentially would achieve this.
Workarounds I thought of include: using a pre-deploy command in the frontend to sleep for a few minutes; or similarly using a GitHub workflow to sleep for a few minutes and making the frontend deploy happen only when that completes. But in both cases I have to guess how long to sleep, plus this wastes compute time.
Or manage it manually by pushing the backend changes first and then waiting then pushing the frontend. But this detracts from the joy of being able to deploy in one go using a monorepo.
Any thoughts / suggestions about how to fix this?
6 Replies
a day ago
Hey there! We've found the following might help you get unblocked faster:
If you find the answer from one of these, please let us know by solving the thread!
a day ago
Thanks for the links, I had a look through (or had already read the docs) and I don't think that answers the question.
a day ago
I would try with an had hoc pipeline and it might work most of the times, using railway up -c
in theory should wait for the deployment to be done, but I've read in a different thread that -c is broken https://station.railway.com/questions/how-to-wait-deployment-with-health-check-c0b176d9
If -c doesn't work you can expose a route in the backend with the backend version (git commit?) and wait for this route to return the right commit in the pipeline, then once the pipeline is happy with the response then you can continue with the frontend
if you need something more reliable you might probably need to use the railway webhook....
a day ago
What about something like this little bash script that you can run? The script first deploys the backend, then polls a version or health endpoint to confirm the backend is live with the latest code, and only then triggers the frontend deployment. This ensures that the frontend build can safely use the new backend data, avoiding race conditions and unnecessary delays from arbitrary sleep timers.
/monorepo
โโ /backend
โโ /frontend
โโ /scripts
โ โโ deploy.sh
โโ package.json
In GitHub Actions, Railway pipeline, or other CI, you can add a step:
- name: Deploy Monorepo
run: ./scripts/deploy.sh
Environment variables:
Make sure
RAILWAY_TOKEN
(or your Railway credentials) is set in your shell or CI environment.Any service-specific variables (like URLs or commit SHA) can be passed via env vars or Railway secrets.
#!/usr/bin/env bash
set -e
BACKEND_SERVICE="backend"
FRONTEND_SERVICE="frontend"
MAX_ATTEMPTS=30
SLEEP_INTERVAL=5
# 1๏ธโฃ Deploy backend
echo "๐ Deploying backend..."
railway up --service $BACKEND_SERVICE
# 2๏ธโฃ Wait for backend to be ready
echo "โณ Waiting for backend to be ready..."
ATTEMPT=0
while true; do
ATTEMPT=$((ATTEMPT+1))
# Replace with your actual endpoint
RESPONSE=$(curl -s http://backend.up.railway.app/version)
COMMIT=$(echo $RESPONSE | jq -r '.commit')
if [[ "$COMMIT" == "$(git rev-parse HEAD)" ]]; then
echo "โ
Backend is live with the correct version!"
break
fi
if [[ $ATTEMPT -ge $MAX_ATTEMPTS ]]; then
echo "โ Backend did not become ready in time."
exit 1
fi
sleep $SLEEP_INTERVAL
done
# 3๏ธโฃ Deploy frontend
echo "๐ Deploying frontend..."
railway up --service $FRONTEND_SERVICE
21 hours ago
Thanks for the useful thoughts, I am going to work with that now and will get back to you.
19 hours ago
railway up -c
is working perfectly for me - ie, it does not return until the deployment is complete. So I can use it to arrange sequential deploys from a github workflow, as you suggested:
name: Sequential Deploy to Railway
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
container: ghcr.io/railwayapp/cli:latest
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy Backend to Railway and wait using -c
run: railway up -c --environment=production --service=backend
- name: Deploy Frontend to Railway
run: railway up --environment=production --service=frontend
The Bash script idea is of course useful if you don't have GitHub actions to hand, but for anyone reading this in future, it looks like something as simple as this would work. I gave it a quick test and it seemed OK to me:
#!/usr/bin/env bash
railway up -c --environment production --service backend
railway up --environment production --service frontend
I will leave a comment on the thread you linked to above saying that -c
seems to do the job.
If, however, anyone in future runs into the issue where -c
does not seem to block for you then @robksawyer's bash script looks very useful.
Status changed to Solved brody โข about 17 hours ago