How to run pre and post-deployment scripts?

iam
PRO

8 months ago

I want to run DB migration script as a pre-deployment script, but not sure how properly specify it. Deployment start command overrides the default one, which I don't want to do, because I don't know the exact command that Railway uses to run Docker-based deployment. And I'm not sure what will be available for this command - node.js env, my project files, etc.

Awaiting User Response

7 Replies

unicodeveloper
PRO

8 months ago

Hi, you can add the commands to run your migration script in a deploy script and add it to to the Custom Build Command in the Build section.

E.g sh ./migrate.sh

or you can add it to the Custom Start Command in the Deploy section. e,g npm run migrate && npm start. This way Railway runs the migration script on startup and and runs your app server.

These are guides that show how migrations are run on deploy/start up on Railway:

https://docs.railway.app/guides/laravel#set-up-database-migrations-crons-and-workers

https://docs.railway.app/guides/rails


Status changed to Awaiting User Response railway[bot] 9 months ago


iam
PRO

8 months ago

I'm sorry, but it doesn't help. Did you read my post? I saw both pages in the docs, but as I mentioned in my post there are two issues:

  1. I'm using service deployment from Dockerfile. Custom Build/Start commands override default ones. This implies I have to restore the original command that starts my container, but I don't know which command Railway uses to start container.

  1. I don't know which tools are available for my custom build/start commands. Node.js? Which version? Which env vars available for this commands?


Status changed to Awaiting Railway Response railway[bot] 9 months ago


unicodeveloper
PRO

8 months ago

You can call the deployment command in the CMD section of the Dockerfile, e.g

CMD ["node ace migration:run --force && node ./bin/server.js"]

or you can use the RUN - https://docs.docker.com/reference/dockerfile/#run instruction to call your migration script command in the Dockerfile. So Railway will run that command as part of the build stage.


Status changed to Awaiting User Response railway[bot] 9 months ago


iam
PRO

8 months ago

This is something I want to avoid:

  1. I don't want to run migration every time container is started, it may happen when I scale the number of instances or just restart the service. I want to run it only when I actually deploy new changes.

  2. RUN command creates a caching layer which I don't want to do for the migration step, it's unrelated to the image itself.

Is it possible to run a pre or post deployment commands without overriding the default ones? Or can you just let me know the default command that Railway uses to start Docker-based deployment? And what tools will be available for this command?


Status changed to Awaiting Railway Response railway[bot] 8 months ago


unicodeveloper
PRO

8 months ago

Hi, there's no way to hook into Railway. We don't provide that.

We provide the options to:

1. Use the build and start commands from the UI like i mentioned already

  1. You can run anything within your Dockerfile to run your migrations and any command you wish to. This very much works for your use case.

Btw, there's also no downside in running migrations every time the container starts. Your migration script in your app will only run if there are changes to the schema.


Status changed to Awaiting User Response railway[bot] 8 months ago


iam
PRO

8 months ago

Doesn't look like DB is accessible for the app service during the build:


Status changed to Awaiting Railway Response railway[bot] 8 months ago


8 months ago

The private network is not available during build, if you need to access services privately please do that during runtime before starting your app.

As long as you use a readiness type health check there would be no issues with service disruptions between deployments.


Status changed to Awaiting User Response railway[bot] 8 months ago


How to run pre and post-deployment scripts? - Railway Help Station