Using GitLab CI/CD with Railway

Avatar of wyzllewyzlle
·

GitLab CI/CD is a powerful continuous integration (CI) and continuous deployment (CD) system. In this post, we talk about how you can use GitLab CI/CD to automate your deployments on Railway.


If you haven't used it before, GitLab CI/CD allows you to automate several parts of your development workflow. Think about building your app, catching bugs, and maintaining code standards; 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 GitLab so we thought it'd be a good idea to publish a short tutorial doing just that.

Project tokens

Railway provisions project tokens. They can be used in environments where you cannot login (CI in our case). It let’s CLI access all the variables and logs 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’ve created your project token, you can add it to your repository secrets on GitLab by visiting the Variables submenu under CI/CD settings.

Repository secrets

Repository secrets

Lights, camera, action!

stages:
  - deploy

deploy-railway:
  stage: deploy
  image: ubuntu
  only:
    - pushes
    - main
  script:
    - apt-get update && apt-get install -y curl
    - curl -fsSL https://railway.app/install.sh | sh
    - railway up --service=$RAILWAY_SERVICE_NAME -d

The pipeline configuration shared above is pretty self-explanatory. The deploy job gets triggered on each push to the main branch. Upon triggering, it installs Railway CLI using curl and deploys your application to the specified service on Railway!

Conclusion

While this was a very basic example of deploying your application on Railway using GitLab CI/CD, you can do all sorts of things. We have users who use nixpacks (Railway’s build system) to dry-run their apps before deploying.

Let us know if you would like Railway to add direct GitLab integration by up-voting/commenting on this post or if you have any thoughts/feedback regarding this post. The easiest way to reach us is our Discord server.