#Env variables from .env

1 messages · Page 1 of 1 (latest)

dense hamlet
#

I suggest you parse that file and add the corresponding .withEnvVariable() steps. There may be a module in the daggerverse doing this already.

#

Similarly to a Dockerfile, doing it from a withExec (equivalent to RUN), won't persistent them in the container.

molten portal
dense hamlet
#

Btw, that error is saying that you should do this instead:

- .withExec(['sh', '-c', '.', './.env.ci']);
+ .withExec(['sh', '-c', '. ./.env.ci']);

Basically the whole script is a single argument to -c.

dense hamlet
molten portal
#

This is working for me already in the buildEnv stage .withExec(['sh', '-c', '.', '.env.ci'])

molten portal
dense hamlet
molten portal
dense hamlet
#
dagger /usr/src $ sh -c "." "/.env"
/.env: line 1: .: filename argument required
.: usage: . filename [arguments]
dagger /usr/src $ sh -c ". /.env"
dagger /usr/src $
#

I don't know what's in your buildEnv, can you share?

molten portal
#
  async buildEnv(source: Directory, envFile?: File): Promise<Container> {
    const nodeCache = dag.cacheVolume('node:20');
    const nxCache = dag.cacheVolume('nx');

    let container = dag
      .container()
      .from('node:20.17.0')
      .withDirectory('/src', source, {
        exclude: [
          '.dagger',
          '.nx',
          '**/node_modules',
          '**/dist',
          '**/coverage',
          '**/.reports',
        ],
      })
      .withWorkdir('/src')
      .withMountedCache('/root/.npm', nodeCache)
      .withMountedCache('/src/.nx', nxCache)
      .withEnvVariable('NPM_CONFIG_REGISTRY', '$CI_NPM_CONFIG_REGISTRY', {
        expand: true,
      });

    if (envFile) {
      container = container
        .withFile('.env.ci', envFile, { permissions: 0o744 })
        .withExec(['sh', '-c', '.', '.env.ci']);
    }

    return container.withExec(['npm', 'install']);
  }
dense hamlet
#

Can you show the trace where that sh step runs successfully?

molten portal
#
34  :   Container.from DONE [1.5s]
36  :   Container.withFile(
36  :       path: ".env.ci"
36  :       permissions: 484
36  :       source: Host.file(path: "/builds/...../.env.ci"): File!
36  :     ): Container!
37  :   Container.withExec(args: ["sh", "-c", ". .env.ci"]): Container!
37  :   Container.withExec DONE [0.0s]
dense hamlet
#

But this is using ". .env.ci" and not ".", ".env.ci".

molten portal
#

copied a different part

Monorepo.mergePipeline(
12  :     envFile: Host.file(path: "/builds/..../.env.ci"): File!
12  :     .......
13  :   Container.from DONE [0.8s]
15  :   Container.withFile(
15  :       path: ".env.ci"
15  :       permissions: 484
15  :       source: Host.file(path: "/builds/..../.env.ci"): File!
15  :     ): Container!
16  :     copy /.env.ci /src/.env.ci
17  :   Container.withExec(args: ["sh", "-c", ".", ".env.ci"]): Container!
17  :   Container.withExec DONE [0.0s]
18  :   Container.withExec(args: ["npm", "install"]): Container!
18  :   Container.withExec DONE [0.0s]
19  :   Container.sync: ContainerID!
20  :   Container.withDirectory(
20  :       directory: ModuleSource.resolveDirectoryFromCaller(path: "."): Directory!
20  :       exclude: [".dagger", ".nx", "**/node_modules", "**/dist", "**/coverage", "**/.reports"]
20  :       path: "/src"
20  :     ): Container!
21  :     copy / /src
22  :   Container.withWorkdir(path: "/src"): Container!
22  :   Container.withWorkdir DONE [0.0s]
23  :   Container.withMountedCache(
23  :       cache: cacheVolume(key: "node:20"): CacheVolume!
23  :       path: "/root/.npm"
23  :     ): Container!
23  :   Container.withMountedCache DONE [0.0s]
24  :   Container.withMountedCache(
24  :       cache: cacheVolume(key: "nx"): CacheVolume!
24  :       path: "/src/.nx"
24  :     ): Container!
24  :   Container.withMountedCache DONE [0.0s]
25  :   Container.withEnvVariable(expand: true, name: "NPM_CONFIG_REGISTRY", value: "$CI_NPM_CONFIG_REGISTRY"): Container!
25  :   Container.withEnvVariable DONE [0.0s]
15  :   Container.withFile DONE [0.0s]
20  :   Container.withDirectory DONE [0.0s]
18  :   Container.withExec DONE [2m17.7s]
#

I dont think the command structure is not the problem because i have tried different combinations already

dense hamlet
#

The command still seems wrong to me. It could be a diff in the different base images. You're passing a script to sh -c with contents . (thus the error), then executing .env.ci as a script (not sourced, as in . == source). Maybe you intend to execute .env.ci as a script instead of sourcing it? Would you please do as I suggest and get past that error? Then we can focus on the next issue.

#

Are you using a Dagger module by the way? Doesn't seem like it to me, but just want to be sure.

molten portal
#

I'm not using any modules, in fact I'm very new to this and don't know how to use it.

#

Sure, I will make those changes and update you here. Thank you so much for your response. Leaving the desk for the day

molten portal
#

Issue was because the container exec user didnt have enough permission;
workaround:

        .withFile('/tmp/.env.ci', config.envFile, { permissions: 0o744 })
        .withExec(['sh', '-c', 'cd /tmp'])
        .withExec([
          'sh',
          '-c',
          'while IFS= read -r line; do export "$line"; done < .env.ci',
        ]);
vestal plaza
molten portal
#

True. Is there any otherway to pass down the env file. The issue is withEnvVariable lets us pass only one variable at a time

molten portal