Shared Variables is a new feature that makes it easier to ship microservice and monorepo projects by defining secrets in a centralized place and consuming them across multiple services.
Let’s take a look at how Shared Variables works.
Configuring shared variables
Shared Variables may be accessed from Project Settings or from the Variables tab within a service’s settings.
The easiest way to access shared variables is from the new Shared Variables section within Project Settings. From here, you can create shared variables for each environment and select the services with which to share these variables.
Once a shared variable is added to a service, it acts like any other service variable, meaning it will be exposed automatically to your deployments.
Define shared variables and distribute to services from the Project Settings page
Referencing shared variables from a service
Shared variables can also be added from the Variables tab within a service. You’ll now see a new button labeled
Insert Shared Variable which provides a list of available shared variables to add to the service.
Insert shared variables via the Variables tab in the service settings
Existing variables within a service can now be promoted to shared variables using the
Promote action from this same Variables view.
Promote a service variable to a shared variable using the actions dropdown
When you promote a service-level variable, the variable becomes a shared variable at the project level. To see this in action, try promoting a variable within a service and then visiting Shared Variables within Project Settings.
What “sharing” actually means
Variables can be added to a service from Project Settings or from a service’s Variables tab. But what is actually happening when you do this?
All shared variables are exposed to Railway’s template engine under the
shared.* namespace. When you add a shared variable with a service, like we showed above, Railway creates a new service-level variable that references the shared variable in its value.
You may observe this by revealing the value of any shared variable within a service and noting the templating syntax.
Reveal the value of a shared variable reference by clicking into the variable
This also means you may create these references manually, which opens the door to composing shared variables in a more complex manner.
As a result, variables that are shared may be referenced in different ways by various services within a project.
Shared Variables is available to all Railway users starting today!
Head over to your project settings or service settings to get started or check out the Shared Variables docs to learn more.
If you have any questions or feedback on this feature, please be sure to join us on Discord and let us know. 👋