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