#Make .env file variables available during build
1 messages · Page 1 of 1 (latest)
Container.with_exec and then sourcing the .env file seems like the best bet.
But yeah, a convenience method like Container.with_env_file that would populate the env variable with the contents of that env file would be nice.
Thank you, that seems indeed as the best bet.
That convenience method would be great indeed.
runlocal(src: Directory, user: Secret, password: Secret, dbPort: number): Service { const envFile = '.env'; // Path to your environment file return ( dag .container() .from('debian:buster-slim') .withServiceBinding('db', dag.postgres(user, password, { dbPort }).database().asService()) .withDirectory('/src', src) .withWorkdir('/src') .withExec([ 'bash', '-c',
set -o allexport;
source ${envFile};
set +o allexport;
, ]) .directory('/src') .dockerBuild({ dockerfile: 'Dockerfile-local', buildArgs: [], }) .withExposedPort(3000) .asService() );
It looks like my env vars sourced this way are not available during the dockerbuild step.
Is there another, better way to provide them?
env vars with the dockerBuild method need to be passed via the buildArgs attribute. That's how you generally pass env vars to a docker build command
in that context, creating a helper that parses your .env file and generates a BuildArg type should be trivial 🙏
not a typescript dev myself but this seems quite straightforward:
Is this chat still available?
yes it is
Not showing for me
Could you please add env loading code snippet here
Thanks in advance
how about this one? https://chatgpt.com/share/67167d95-ed14-800e-8d53-0fbac6d6b043
Can view this, thank you so much
But it doesn''t show how appended this on the dagger container
@lyric thicket that snippet is intended to be used with the DockerBuild function. I don't think you're currently using that, don't you?
No, I'm not using docker build. I am looking for a way to inject env varibles as a file at one go.
return dag.container()
.from('node:20.17.0')
.withDirectory('/src', source)
.withWorkdir('/src')
.withFile('.env.ci', envFile)
.withExec(['.', '.env.ci']);
unfortunately its not working
Here is what i did
// loading file with permissions .withFile('.env.ci', envFile, { permissions: 744, }) // adding env variables to bashrc with double quotes .withExec([ 'bash', '-c', ` sed -E 's/^([^=]+)=(.*)$/export \\1="\\2"/' .env.ci >> ~/.bashrc `, ]);
The catch is, these would be shell variable and not environment variables; we can use the shell variables in the existing processes and its child processes.
@lyric thicket This is how I did in my code in python.
I simply transform the .env file to a dictionary using the dotenv python lib, and then I iterate on each key and add them in the container using with_env_variable method.
The get_source function return the source code where my test runs (This is a Directoy object)
# Get the raw content of the .env file
env_file_content = await self.get_source().file("<env_file_path>").contents()
# Transform the raw content to a dictionnary
env_variables = dotenv_values(stream=StringIO(env_file_content))
# Set the env variables (test_container is simply a Container object)
for k, v in env_variables.items():
test_container = test_container.with_env_variable(k, v)
Hope it can help!
@ocean radish hi, thanks for sharing your config.
I was using the same method you mentioned; the issue comes when i use this with our self hosted gitlab. Here is the log; we want to view clean logs actually;
..................many similar lines..............
183 : Container.withEnvVariable(name: "CI_PROJECT_ID", value: "342342345"): Container!
183 : Container.withEnvVariable DONE [0.0s]
184 : Container.withEnvVariable(name: "CI_MERGE_REQUEST_SOURCE_BRANCH_PROTECTED", value: "false"): Container!
184 : Container.withEnvVariable DONE [0.0s]
185 : Container.withEnvVariable(name: "CI_COMMIT_SHA", value: "sfukjvbskdjflvn"): Container!
185 : Container.withEnvVariable DONE [0.0s]
186 : Container.withEnvVariable(name: "CI_CONCURRENT_ID", value: "0"): Container!
186 : Container.withEnvVariable DONE [0.0s]
187 : Container.withEnvVariable(name: "CI_MERGE_REQUEST_SQUASH_ON_MERGE", value: "true"): Container!
187 : Container.withEnvVariable DONE [0.0s]
188 : Container.withEnvVariable(name: "CI_REGISTRY_USER", value: "jdbhknakvnaewlkd"): Container!
188 : Container.withEnvVariable DONE [0.0s]
189 : Container.withEnvVariable(name: "CI_SERVER_PORT", value: "443"): Container!
..................many similar lines..............
Ah I see, I can't help for that, it will effectively create 1 log per env variables injected in the container