Environment variables are not being read

Hi there, the environment variable I set on Fly.io is not being read by my app during deployment.

  • The start script is in a Dockerfile
  • The hardcoded variables work fine
  • When I use fly ssh console and echo ${DYNAMIC_KEY} it returns the correct variable

So I suspect that maybe this is being set after the Docker script completes? Any idea how I can get this to work?

For extra context, I am using the machines api to create and set the environment variable (which works perfectly fine).

config: {
        image: currentImage,
        size: "shared-cpu-1x",
        env: {
          DYNAMIC_KEY: generateKey(),
        },
        ......

I need this to be dynamically set since the repo is public and I am spinning up these machines on demand.

1 Like

I am not sure I follow the architecture you’re using. It sounds like you have an always-on machine that you can shell into, and in that machine, DYNAMIC_KEY is set as an env var correctly. In that machine you want to spawn a new machine using the API, but you want to pass DYNAMIC_KEY into the new machine. Does that sound right?

If that is correct, what sort of script are you using in the first machine? If this is Bash or some similar shell, then do your echo ${DYNAMIC_KEY} in the script to ensure it has inherited the key-value pair from the shell environment.

1 Like

Hello @purplegrape! Thanks for your question, and thanks a lot @halfer for sharing your thoughts on this!

Are you perhaps looking for build secrets? These secrets would be available during build time of an app–right around the time you do a fly deploy. It’s possible that you’ve set your DYNAMIC_KEY as a Fly Secret–though these are available during runtime, they’re not up for grabs during build time.