Avatar of Faraz PatankarFaraz Patankar

Using Github Actions with Railway

Github Actions come with a pretty neat set of features to automate your workflows. In this post, we talk about using Github Actions to automate your deployments on Railway.


At Railway, we've set up Github triggers so that your app can be automatically deployed when you push to a selected branch within the connected repository. This works for most basic use-cases but after a while, you probably want more control and that's where Github Actions come in.

If you haven't used them before, Github Actions allow you to automate several parts of your development workflow. Think building your app, running some tests, managing pull requests; the possibilities are endless. Recently, within our Discord, we've had a couple of users ask us how they'd go about deploying their app on Railway using Github Actions so we thought it'd be a good idea to publish a short tutorial doing just that.

To interact with your project within Github Actions, you'd have to use the Railway CLI and because you can't really log in to the CLI within a remote server, we allow you to provision project tokens. Project tokens allow the CLI to access all the environment variables associated with a specific project and environment.

You can create a new project token on the Settings page of your project dashboard within the Tokens submenu.

Create project token

Create project token

Once you created your project token, you can add it to your repository secrets on Github by visiting the Secrets submenu under repository settings.

Repository secrets

Repository secrets

name: Deploy to Railway

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    container: ghcr.io/railwayapp/cli:latest
    env:
      SVC_ID: my-service-id
      RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
    steps:
      - uses: actions/checkout@v3
      - run: railway up --service=${{ env.SVC_ID }}

The action shared above is pretty self-explanatory. Whenever some code is pushed to the main branch, the action is triggered. The deploy job pulls the latest version of the CLI docker image and uses it as a base container. The, the only thing left is to checkout your code and upload it to Railway!

Keep in mind that the RAILWAY_TOKEN must be kept secret, while the service Id can be passed freely as a part of deploy command, or as an environment variable, defined inside the job configuration.

While this was a very basic example of deploying your application on Railway using Github Actions, you can do all sorts of things using the project token. We have users using it to manage pull requests, migrate their database before deploying, and, as shown in this blog post, deploying the application itself.

Let us know if you have any thoughts/feedback regarding this post or would like to see more tutorials using Github Actions. The easiest way to reach us is our Discord server which we also use to run all our internal communications which you can read about in this post.