1

At the moment I am using flutter_dotenv to load some environment variables from a file. It worked okay until I tried to create a GitHub Action that runs flutter test.

The file with the environment variables has to be added as an asset in pubspec.yaml acording to the documentation for the package so when I run the action I get the following error:

No file or variants found for asset: dotenv.

Where dotenv is the name of the file with environment variables. The reason I get the error is because dotenv is in .gitignore (obviously), so that file can't be found.

Is there a way to use environment variables in Flutter without having to add the file to pubspec.yaml? Or is it possible to add the file "locally" (meaning adding it to GitHub without having to track it with git) for the action in some way?

Hannes Hultergård
  • 1,157
  • 1
  • 11
  • 33
  • If I understand you right you would need add this file manually after a clean github fetch – DEFL Dec 17 '21 at 18:31
  • 1
    Yes, that's what I do in my pipelines. I generally base64 encode the contents of the file in a secret, then in my action script, decode secret and write the content to a file. Here is a link to SO answer doing the same thing: https://stackoverflow.com/a/66603047/12132021 – Jared Anderton Dec 17 '21 at 18:53
  • Why is `dotenv` "obviously" in `.gitignore`? Do the environment variables contain secrets? If so, then yes, referencing them in `pubspec.yaml` probably is inappropriate. I don't have direct experience with GitHub Actions, but [you can set custom environment variables in the workflow configuration](https://docs.github.com/en/actions/learn-github-actions/environment-variables). You alternatively might want to look into using [`direnv`](https://direnv.net/), which allows an `.envrc` file to conditionally load a private environment file only if it exists. – jamesdlin Dec 17 '21 at 18:56
  • jamesdlin, I just mean that it wouldn't have to be environment variables if it wasn't different for each environment. That's why the file is in `.gitignore`. – Hannes Hultergård Dec 17 '21 at 22:11
  • Thank you Jared, will look at that solution – Hannes Hultergård Dec 17 '21 at 22:15

0 Answers0