a month ago
When deploying a service using Railway IaC (railway/iac) I discovered that any services that contain generated secrets get redeployed even if there were no changes to the service. It seems like the Railway planner thinks the secret has changed. Example of how I generate secrets:
MONGO_INITDB_ROOT_PASSWORD: {
generator: '${{secret(64, "abcdef0123456789")}}',
preserveExisting: true,
},Is this an expected behavior? I'd expect the service to not get a new deployment if nothing has changed :shrug:
7 Replies
a month ago
That's not what you want, but it's how the planner behaves today. A generated secret on a service that already exists can't be compared to the stored value, so the plan treats it as a change and the service gets redeployed. Also, preserveExisting isn't an option the IaC config recognizes, so it doesn't stop this.
To avoid the redeploy, use the generator only when you first create the variable. Once the secret exists, author it as preserve(), which keeps whatever value we already have and never plans a change. railway config pull writes existing secrets out as preserve() for you.
Status changed to Awaiting User Response Railway • 29 days ago
Railway
That's not what you want, but it's how the planner behaves today. A generated secret on a service that already exists can't be compared to the stored value, so the plan treats it as a change and the service gets redeployed. Also, `preserveExisting` isn't an option the IaC config recognizes, so it doesn't stop this. To avoid the redeploy, use the generator only when you first create the variable. Once the secret exists, author it as `preserve()`, which keeps whatever value we already have and never plans a change. `railway config pull` writes existing secrets out as `preserve()` for you.
a month ago
Is there a plan to fix it? I can make sure that once secret is created I just use preserve() in its place, but that's something I'd call a hack rather than a proper solution... especially if there is no function to check if a secret exists or not and I have to use CLI for that (railway config pull)
Status changed to Awaiting Railway Response Railway • 29 days ago
a month ago
There's no committed change to how the planner treats generated secrets on existing services that we can point you to, so preserve() is the supported way to keep a stored secret from planning a change today.
You don't have to swap it in by hand or run railway config pull each time, though. The config file is real TypeScript and defineRailway accepts an async function, so you can fetch the service's existing variable names at plan time and build env from them, emitting preserve() for keys that already exist and the secret() generator only for keys that don't. Values stay out of source, and a key that genuinely disappears still shows up in the plan.
Status changed to Awaiting User Response Railway • 29 days ago
a month ago
The SDK does not provide any functionality to fetch existing variable names for a service, hence from my TS code I'd have to call into the CLI to run railway config pull. This doesn't feel right.
Status changed to Awaiting Railway Response Railway • 29 days ago
a month ago
You don't need the CLI for this. The public GraphQL API's variables query returns a service's variables for a given project and environment, so the async defineRailway function can call it with a fetch and use Object.keys() on the result to choose between preserve() and the secret() generator. That query also returns values, so read only the key names and leave the values out of your config. See Manage Variables with the Public API.
The IaC SDK doesn't have a built-in helper to check whether a variable already exists, and we have no committed change to how generated secrets are planned on existing services that we can point you to.
Status changed to Awaiting User Response Railway • 29 days ago
a month ago
I will implement a workaround, but this feels like something that should be prioritized in railway/iac...
Status changed to Awaiting Railway Response Railway • 29 days ago
a month ago
That's fair feedback, and thanks for laying out the use case so clearly. We don't have committed work on how generated secrets are planned for existing services, or on an SDK helper to check whether a variable already exists, that we can point you to. For now, the API-driven approach is how to keep those services from redeploying when nothing has changed.
Status changed to Awaiting User Response Railway • 29 days ago
21 days ago
This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!
Status changed to Solved Railway • 21 days ago