how to replace {{namespace.var}} on new deploy according to a json file?
ectsang
PROOP

2 years ago

let's say i have a npm script start that takes in a config.json file, in which i have 3 secret variables.

I know how to set variables as "sealed" in the dashboard, but how do I ensure the placeholders in my .json file get replaced upon each new deploy?

Thanks!

Solved

29 Replies

ectsang
PROOP

2 years ago

0288702c-a08b-4f8c-b274-652a6ff81652


2 years ago

Use env substitute command

Create a template file, run this command in your dockerfile (using ARG VARIABLE_NAME to ingest the variables):

envsubst < config.template.json > config.json

Example template file:

  "apiKey": "${API_KEY}",
  "dbPassword": "${DB_PASSWORD}",
  "authToken": "${AUTH_TOKEN}"
}```


2 years ago

or use a pre-deploy command, but I assume you need this done in build step.


2 years ago

Alternatively, change your code to use env vars directly, this would be the most sensible & straight forward approach.


ectsang
PROOP

2 years ago

So in my config.json file can i have

{
  "somevar": "${{SOME_VAR}}"
}

And railway will automatically replace it upon deploy?


2 years ago

if you tell it to.


2 years ago

You'll need to setup your dockerfile/build/pre-deploy command to do that.


2 years ago

You can use any utils your image has


ectsang
PROOP

2 years ago

does envsubst exist in ubuntu?


2 years ago

Should exist in all unix based envs (assuming it hasn't been stripped on image creation), yes



ectsang
PROOP

2 years ago

tyty


ectsang
PROOP

2 years ago

in my Dockerfile, is it just

RUN envsubst < config.json.template > config.json


ectsang
PROOP

2 years ago

i ask because when i build it locally it doesn't run (simply replaces with ""). But in the shell i can see the env var set , and upon manually running envsubst it did replace


2 years ago

please see our docs on using variables at build time in a Dockerfile -


ectsang
PROOP

2 years ago

I've set both ARG RAILWAY_ENVIRONMENT and ARG MY_VAR immediately after the FROM node line.. it still fails


ectsang
PROOP

2 years ago

fails as in .. it never replaced the env var in my .json config file


2 years ago

are you using the correct syntax in your config.json.template file?


ectsang
PROOP

2 years ago

ok solved it.. i'm not sure why the SEALED variable requires me to do it twice


ectsang
PROOP

2 years ago

i manually updated via the dashboard after i created it. And it worked


2 years ago

sounds good!


ectsang
PROOP

2 years ago

@Brody for networking.. i'm having trouble


ectsang
PROOP

2 years ago

i exposed the necessary port in my Dockerfile


ectsang
PROOP

2 years ago

but /health isn't being reached


2 years ago

you need this section -


ectsang
PROOP

2 years ago

argh thanks for the assist


ectsang
PROOP

2 years ago

that was it


2 years ago

no problem, any more questions? 🙂


2 years ago

!s


Status changed to Solved brody • over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...