Using GitLab CI/CD with Railway
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.
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
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
workflow:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
railway-deploy:
stage: deploy
image: ghcr.io/railwayapp/cli:latest
variables:
SVC_ID: my-service-id
script:
- railway up --service=$SVC_ID
In this minimal example we use the CLI docker image as a base image for a job that will deploy to your service defined by the service Id. The workflow only triggers on pushes to projects default branch (usually main
). Refer to the GitLab CI documentation to learn more about workflow configuration options.
Here, GitLab CI only provides a way to connect your code to Railway, the entire building part is handled by Railway build infrastructure. For some projects, it may be beneficial to handle the build stage within GitLab CI. In this case, you can still use the Railway CLI to deploy build artifacts (such as Golang or Rust binaries) to Railway.
Keep in mind that the RAILWAY_TOKEN
must be kept secret, while 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 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.