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

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Use Node 12
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install packages
        run: yarn install --frozen-lockfile

      - name: Run tests
        run: yarn test

      - name: Install Railway
        run: npm i -g @railway/cli

      - name: Deploy
        run: railway up
        env:
          RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}

The action shared above is pretty self-explanatory. Whenever some code is pushed to the main branch, the action is triggered. Upon triggering, it installs all your packages using yarn, runs your tests, and deploys your application to Railway.

The critical parts here are the installation of the Railway CLI and the use of the RAILWAY_TOKEN which we created earlier on in the blog post.

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.