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

a year 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

a year ago

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


a year 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}" }


a year ago

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


a year ago

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


ectsang
PROOP

a year ago

So in my config.json file can i have

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

And railway will automatically replace it upon deploy?


a year ago

if you tell it to.


a year ago

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


a year ago

You can use any utils your image has


ectsang
PROOP

a year ago

does envsubst exist in ubuntu?


a year ago

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



ectsang
PROOP

a year ago

tyty


ectsang
PROOP

a year ago

in my Dockerfile, is it just

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


ectsang
PROOP

a year 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


a year ago

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


ectsang
PROOP

a year ago

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


ectsang
PROOP

a year ago

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


a year ago

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


ectsang
PROOP

a year ago

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


ectsang
PROOP

a year ago

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


a year ago

sounds good!


ectsang
PROOP

a year ago

@Brody for networking.. i'm having trouble


ectsang
PROOP

a year ago

i exposed the necessary port in my Dockerfile


ectsang
PROOP

a year ago

but /health isn't being reached


a year ago

you need this section -


ectsang
PROOP

a year ago

argh thanks for the assist


ectsang
PROOP

a year ago

that was it


a year ago

no problem, any more questions? 🙂


a year ago

!s


Status changed to Solved brody about 1 year ago


Loading...