`fly launch` doesn't respect environment variables set

When running the fly launch command, env isn’t configured.


$ fly launch --now --generate-name --remote-only -e 'PORT=8080'
# wait for deploy to finish
$ fly config show | jq -c .env
# expected {"PORT":"8080"}

Additionally, env isn’t configured in the fly.toml file (with or without deploy), so needs manually editing (or fly config save after deploy with -e)

As a side, it’d be great if the internal_port is exposed to the container somehow. For now, we’re relying on you using always using ‘8080’ as the default port.
Something like this would help:

fly launch --internal-port 8080 -e 'PORT=8080'

Setting environment variables does not work for fly launch but works for fly deploy. I have tested on Docker image and Elixir Phoenix application using flyctl v0.0.456 linux/amd64 Commit: 43371b58 BuildDate: 2023-02-08T22:32:29Z

If you can split the command I hope it will help in your case:

fly --build-only --generate-name --remote-only
fly deploy -e 'PORT=8080'

You can try APIs to accomplish your goals as well:

Using that as a workaround currently.

I’ve just taken another look.
doing fly deploy -e 'PORT=8080' doesn’t update fly.toml.
This causes subsequent fly deploy calls to lose the environment variable.

The full workaround is now:

fly launch --build-only --remote-only
fly deploy -e 'PORT=8080'
fly config save

# later
fly deploy

Losing the environment variables is really bad. In our case, the environment variable being missing will cause healthchecks to fail. Others may not be so lucky.

$ fly version
flyctl v0.0.464 linux/amd64 Commit: f9840dd8 BuildDate: 2023-02-22T09:52:30Z

I’m trying to understand the use case here… what are you trying to do with fly deploy -e and persisting environment variables?

Today, environment variables that need to persist across deployments either need to be set in the fly.toml application config file or with fly secrets.

The usecase is that we’re trying to provide instructions to our users for quickly launching our product on Fly.
These users aren’t necessarily familiar with Fly (or any kind of hosting provider); so reducing edge-cases, number-of-commands run, and the amount of times they need to edit a config, is a huge benefit.

The main concern is fly launch -e 'PORT=8080'.
It’s a documented feature, but appears to do nothing (for the deploy, or for the config).

I believe, the -e switch merely takes precedence over env vars specified in fly.toml. I don’t think a subsequent call to fly config save (docs) is supposed to pickup variables set with -e from a prior run (afaik, this isn’t mentioned in the docs anywhere), but that’s an interesting and valid expectation. You could consider filing a flyctl feature request on github.