R
Railway

✋|help

Easy (?) shared variables question

Nnball9/11/2023
The environment contains the excellent Payload CMS template, attendant Mongo DB, and a Next.js front end. This works well when the env are set manually for each service, however the shared variables don't. What am I missing?

This works when set on each service, eg. not shared
NEXT_PUBLIC_SERVER_URL=https://cowcatcher.up.railway.app/
PAYLOAD_PUBLIC_SERVER_URL=https://caboose.up.railway.app/

The shared variables do not work
NEXT_PUBLIC_SERVER_URL=https://${{cowcatcher.RAILWAY_PUBLIC_DOMAIN}}
PAYLOAD_PUBLIC_SERVER_URL=https://${{caboose.RAILWAY_PUBLIC_DOMAIN}}

When shared on both front and back end services, the variables look like this
NEXT_PUBLIC_SERVER_URL=${{shared.NEXT_PUBLIC_SERVER_URL}}
PAYLOAD_PUBLIC_SERVER_URL=${{shared.PAYLOAD_PUBLIC_SERVER_URL}}

Project ID 520ae57c-db3e-4e21-ace6-db67bca65e2a
Bbrody1929/11/2023
are those the correct service names? cowcatcher and caboose?
Nnball9/11/2023
Yes 😂
https://cowcatcher.up.railway.app/
Caboose is stuck on loading screen right now. Might be cors.
Bbrody1929/11/2023
can I see a screenshot of the project?
Nnball9/11/2023
Yes, just a moment
To build on this issue, fetching with process.env.PAYLOAD_PUBLIC_SERVER_URL/api/posts is working locally as expected, but in production does not work. The static path https://caboose.up.railway.app/api/posts works in both. This may be an issue with Next.js not reading the service env for some reason? What's odd is that the env prints in TSX…
Screenshot of what precisely? Just the UI?
Bbrody1929/11/2023
the whole project
Nnball9/11/2023
Bbrody1929/11/2023
and both those are built with nixpacks?
Nnball9/11/2023
Yessir
[us-west1]
==============
Using Nixpacks
==============
https://cowcatcher.up.railway.app/blog
Bbrody1929/11/2023
and this might be a silly question but how have you been able to confirm the variables aren't rendering, besides any issues from the next apps
Nnball9/11/2023
        <p>
          process.env.PAYLOAD_PUBLIC_SERVER_URL is working:{' '}
          {process.env.PAYLOAD_PUBLIC_SERVER_URL}
        </p>

They render within the return for export default async function Blog() etc
Bbrody1929/11/2023
what does that show?
Nnball9/11/2023
Nnball9/11/2023
It's not a trailing slash is it… hmmmmmmm
Bbrody1929/11/2023
okay that looks good, now is that when you are using reference variables?
Bbrody1929/11/2023
slow down here, it works
Nnball9/11/2023
k
Bbrody1929/11/2023
are you currently using reference variables?
Nnball9/11/2023
  const res = await fetch(
    `${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/posts?where[_status][equals]=published&depth=1&sort=-displayDate`,
    { next: { revalidate: 3600 } } // revalidate at most every hour
  )

I am using variables set within each service, not shared. Not sure about reference.
Bbrody1929/11/2023
static variables
reference variables
shared variables
Bbrody1929/11/2023
what type are you currently using
Nnball9/11/2023
First ones, static
Should env paths like these include the trailing slash? That may be the culprit.
Bbrody1929/11/2023
you want to use the reference variables, that way if you ever change a service name the variable update with the name change
and yes you typically would not include a trailing slash in these types of variables
Nnball9/11/2023
Ok, to clairfy, ${{cowcatcher.RAILWAY_PUBLIC_DOMAIN}} will somehow automagically update the subdomain if the service name changes?
If so, I love Railway more every day.
Bbrody1929/11/2023
yeah say you change the service name from "cowcatcher" to "horsecatcher"
your ${{cowcatcher.RAILWAY_PUBLIC_DOMAIN}} will automatically update to ${{horsecatcher.RAILWAY_PUBLIC_DOMAIN}}
and then if you then went and changed the public domain of the newly renamed service, all you'd need to do is redeploy the service that uses that variable and it will get rendered with the new domain
Nnball9/11/2023
Killer. I like that. The shared will not render a trailing slash either? Presumably not.
Bbrody1929/11/2023
if you put a trailing slash in the variable, there will be a trailing slash in the rendered variable
Nnball9/11/2023
The shared variable in environment settings looks like this https://${{cowcatcher.RAILWAY_PUBLIC_DOMAIN}}
Once shared to a service, looks like this ${{shared.NEXT_PUBLIC_SERVER_URL}}
Bbrody1929/11/2023
so I don't really see how shared variables come into play here?
just use reference variables
Nnball9/11/2023
Oof, I am getting terms mixed up. Don't use shared in this case, regardless.
The idea is, in the near future, to clone this environment entirely, if possible.
Bbrody1929/11/2023
also if you for example want to set a variable DOMAIN equal to the services own domain, you don't need to specify the service name since it's in the same namespace
Nnball9/11/2023
Ahhhh, smart. Even better.
Required for cors anyway. Thank you!
Bbrody1929/11/2023
so just use reference variables, that would clone across environments nicely
Nnball9/11/2023
Thank you so much. "Caboose" is built on your template, thanks for that too!
Bbrody1929/11/2023
and everything worked fine with that template?
Nnball9/11/2023
Oh yes, even with all the extra stuff I have added. Works like a charm.
Note, process.env.PAYLOAD_PUBLIC_SERVER_URL is indeed printing a trailing slash. This seems wrong somehow.
Nnball9/11/2023
Nnball9/11/2023
Outputs
Nnball9/11/2023
Bbrody1929/11/2023
but does the variable you set have a trailing slash?
Nnball9/11/2023
Yes, it did! Ok, sorry to be such a newb on this.
Bbrody1929/11/2023
yep railway isn't going to touch anything outside of the ${{}}
so if there's a slash, it will be there after rendering
Nnball9/11/2023
At some point, I'd like to learn how to craft whole environments as templates. Creating the linkages between the services and the volumes, for example. I haven't seen much detail in the documentation on that aspect.
Does nixpacks do that as well?
Bbrody1929/11/2023
I'll send you an example project over that uses reference variables heavily across multiple services
Bbrody1929/11/2023
guess railway doesnt want me to show you
Nnball9/11/2023
Lol
Is this documented officially somewhere?
Bbrody1929/11/2023
of course, and I should have linked the docs, but I just wanted to show you a real world example of reference variables, here's the docs though
https://docs.railway.app/develop/variables#reference-variables
i got railway to fix the issue so here is my public example project
https://railway.app/project/22902de5-b872-45ae-ba98-743585f6812a
have a look at all the service variables and how they reference each other!
this is also available as a template if youd like to look at that too
https://railway.app/template/CUZ7Dz
Nnball9/14/2023
Darned if it won't show the variables. I can view the project but not the envs.
Bbrody1929/14/2023
oh yeah, well you can see the variables in the template!
click to expand all variables

Looking for more? Join the community!

Recommended Posts
Plan update issueIm having a issue with plan update. It just shows: Problem processing payment method and I triple cCan't create an account: Device requesting login is not supported. Please try again from a secure coTrying to create a new Railway Account, but getting this error: ```Device requesting login is not saccess to resource inside replicahi, when we setup an instance to have 2 replicas, how do we get the resource related to 1 of the reCommunicate between services with private networkProject ID: 3f51dcb4-6905-4a13-be75-4c6063e4324a I am trying to set up Railway to communicate betweWeb Server Not WorkingI just started a small simple web server. I generated a custom domain for now, and when trying to achttp://0.0.0.0:8000 to urlI am using a Django REST framework. When I query the URL at 0.0.0.0:8000 I can get a response, but aOpenSearch deploymentHi everyone. I require assistance with deploying an instance of OpenSearch since I get an error durUnable to connect with private networkHey guys, I'm a bit unsure on how to use the internal network. I have a .net api that has a privaterclone on railway to transfer data from one cloud to an other.Hello, I'm creating an app the requires to transfer data from one cloud storage (like onedrive to aBun 1.0 on railwayI am spinning up a simple bun app with 1 file in it. But for some reason all of my builds are failinSocket cannot be archivedWhen building the railway project I get a message saying that: /Users/x/Library/Application SupportBUN 1.0 + AstroHas anyone tried to deploy a Bun app yet? I am getting this error. ERROR: failed to solve: processDjango Railway admin consoleHi! I've had a really good time using Railway thus far, sea-change from setting up an EC2 instance Trouble sending emails via SMTPprojectId: `bb66f5c0-53f9-4e9f-b11f-5965f5d6ec74` serviceId: `b3029692-b78a-4487-bcb0-44e001afecb8` Cannot have a TCP proxy and a domainWhen trying to create a service with a proxy, I'm getting this error. Can I not have a TCP proxy serChange region for Postgres DatabaseHey ! Congratulations for the launch of deployment region ! Now, how can I migrate my db to another Connection Pooling or PgBouncer for Postgres Instances?Hello! Anyone know if the Railway Postgres instance has any sort of connection pooling or pgBouncer?webhook to rebuild serviceHi I have one project: Mongo, PayloadCMS + storage, Nextjs I want to trigger nextjs rebuild after aI paid 40 dollars in subscription fees but its still dont pay my debt for credits.deploy prisma/client not foundhey, when I deploy the prisma client in node_modules is not found because its not generating, I have