R
Railway•15mo ago
nball

Easy (?) shared variables question

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/
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}}
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}}
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
50 Replies
Percy
Percy•15mo ago
Project ID: 520ae57c-db3e-4e21-ace6-db67bca65e2a
Brody
Brody•15mo ago
are those the correct service names? cowcatcher and caboose?
nball
nballOP•15mo ago
Yes 😂 https://cowcatcher.up.railway.app/ Caboose is stuck on loading screen right now. Might be cors.
Brody
Brody•15mo ago
can I see a screenshot of the project?
nball
nballOP•15mo ago
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?
Brody
Brody•15mo ago
the whole project
nball
nballOP•15mo ago
No description
Brody
Brody•15mo ago
and both those are built with nixpacks?
nball
nballOP•15mo ago
Yessir [us-west1] ============== Using Nixpacks ============== https://cowcatcher.up.railway.app/blog
Brody
Brody•15mo ago
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
nball
nballOP•15mo ago
<p>
process.env.PAYLOAD_PUBLIC_SERVER_URL is working:{' '}
{process.env.PAYLOAD_PUBLIC_SERVER_URL}
</p>
<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
Brody
Brody•15mo ago
what does that show?
nball
nballOP•15mo ago
No description
nball
nballOP•15mo ago
It's not a trailing slash is it… hmmmmmmm
Brody
Brody•15mo ago
okay that looks good, now is that when you are using reference variables?
nball
nballOP•15mo ago
Brody
Brody•15mo ago
slow down here, it works
nball
nballOP•15mo ago
k
Brody
Brody•15mo ago
are you currently using reference variables?
nball
nballOP•15mo ago
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
)
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.
Brody
Brody•15mo ago
static variables reference variables shared variables
No description
Brody
Brody•15mo ago
what type are you currently using
nball
nballOP•15mo ago
First ones, static Should env paths like these include the trailing slash? That may be the culprit.
Brody
Brody•15mo ago
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
nball
nballOP•15mo ago
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.
Brody
Brody•15mo ago
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
nball
nballOP•15mo ago
Killer. I like that. The shared will not render a trailing slash either? Presumably not.
Brody
Brody•15mo ago
if you put a trailing slash in the variable, there will be a trailing slash in the rendered variable
nball
nballOP•15mo ago
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}}
Brody
Brody•15mo ago
so I don't really see how shared variables come into play here? just use reference variables
nball
nballOP•15mo ago
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.
Brody
Brody•15mo ago
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
nball
nballOP•15mo ago
Ahhhh, smart. Even better. Required for cors anyway. Thank you!
Brody
Brody•15mo ago
so just use reference variables, that would clone across environments nicely
nball
nballOP•15mo ago
Thank you so much. "Caboose" is built on your template, thanks for that too!
Brody
Brody•15mo ago
and everything worked fine with that template?
nball
nballOP•15mo ago
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.
nball
nballOP•15mo ago
No description
nball
nballOP•15mo ago
Outputs
nball
nballOP•15mo ago
No description
Brody
Brody•15mo ago
but does the variable you set have a trailing slash?
nball
nballOP•15mo ago
Yes, it did! Ok, sorry to be such a newb on this.
Brody
Brody•15mo ago
yep railway isn't going to touch anything outside of the ${{}} so if there's a slash, it will be there after rendering
nball
nballOP•15mo ago
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?
Brody
Brody•15mo ago
I'll send you an example project over that uses reference variables heavily across multiple services
Brody
Brody•15mo ago
guess railway doesnt want me to show you
No description
nball
nballOP•15mo ago
Lol Is this documented officially somewhere?
Brody
Brody•15mo ago
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
nball
nballOP•15mo ago
Darned if it won't show the variables. I can view the project but not the envs.
Brody
Brody•15mo ago
oh yeah, well you can see the variables in the template! click to expand all variables
Want results from more Discord servers?
Add your server