A library to use .env
files in your Shuttle projects.
Shuttle Secrets is ok, but sometimes you need to use environment variables so they can be used in other crates.
You could use Shuttle Secrets and then iterate over the secrets and set them as environment variables, or you can use this crate.
This crate leverages Shuttle Static Folder to read the .env
files and set the environment variables.
Add the following to your Cargo.toml
:
toml
[dependencies]
shuttle-env-vars = "0.21.0"
Then create a .env
file in the folder you want to use. Note that the name of the file can be anything.:
Once you have created you .env
file you have to add the following to the main
function:
```rust
async fn main( #[shuttleenvvars::EnvVars(folder = "nameofyourfolder", envprod = "nameofyourenvfile")] envfolder¡: PathBuf, ) -> _ { ... } ```
If you want to use a particular .env
file in local mode, you can use the env_local
parameter:
```rust
async fn main( #[shuttleenvvars::EnvVars(folder = "nameofyourfolder", envprod = "nameofyourenvfile", envlocal = "yourpathtoyourlocalenvfile")] _envfolder¡: PathBuf, ) -> _ { ... } ```
When executing locally, both folder
and env_prod
will be ignored and only env_local
will be used. It's important to note that env_local
is a path to a file, not a file name.
Typically, the .env
files are not committed to your repository and are ignored.
That's a good practice but will won't work for Shuttle it won't upload the files that are present in the .gitignore
file.
To overcome this, you can take a look at the Caveats
section in Shuttle Static Folder because that's precisely what we need to do.
We will need to create a file called .ignore
and explictly include our .env
files.
For example, if you have a .gitignore
file like this:
sh
dist/
static/
target/
.env*
Then, in order to include the .env
folder and files in the final archive, you’ll have to create a .ignore
file like this:
sh
!.env*
That will do the trick!
If you are using Shuttle in your CI/CD pipeline, you will need to add the .env
files to the final archive.
One solution to avoid having to commit the .env
file, is to store the .env
file content as a secret in your CI/CD provider and then create the .env
file in the CI/CD pipeline.
For example, if you are using GitHub Actions, you can do something like this:
yaml
- name: Set ENV vars file
shell: bash
run: |
mkdir .env
echo "${{ secrets.PROD_ENV_VARS }}" > .env/.env-prod