#Using docker as an intermediate build step

1 messages · Page 1 of 1 (latest)

buoyant perch
#

Hey everybody; I'm having a hard time groking how directories work here; I have the following pipeline, and can't figure out how to get the built assets back out of the container (they should end up in /src/build in the container)

import { connect, Client } from "@dagger.io/dagger"
import { isMethodSignature } from "typescript"

// initialize Dagger client
connect(
  async (client: Client) => {
    
    let repo = client.git("https://github.com/evidence-dev/template", {keepGitDir: false,}).branch("next").tree()

    const buildSh = `

cd /src
npm i
npm run build:sources
npm run build

    `


    const contents = await client
        .container()
        .from("node:20")
        .withMountedDirectory("/src", repo)
        .withWorkdir("/src")
        .withNewFile("/src/build.sh", {contents: buildSh})
        .withExec(["bash", "/src/build.sh"])
        
    console.log(await contents.stdout())
  },
  { LogOutput: process.stderr }
)
#

Also; is waiting for stdout required to actaully execute the container?

hasty socket
#

you can get directory of your assets and export it instead of getting stdout

buoyant perch
#

Of course, as I ask this; I make progress, this seems to be behaving the way I wanted:

import fs from "fs/promises"
import { connect, Client, DaggerSDKError } from "@dagger.io/dagger"

// initialize Dagger client
connect(
  async (client: Client) => {
    
    let repo = client.git("https://github.com/evidence-dev/template", {keepGitDir: false,}).branch("next").tree()

    const buildSh = `
cd /src
npm i
npm run build:sources
npm run build
cp /src/.evidence/template/build /build -r
`


    const contents = await client
        .container()
        .from("node:20")
        .withMountedDirectory("/src", repo)
        .withWorkdir("/src")
        .withNewFile("/src/build.sh", {contents: buildSh})
        .withExec(["bash", "/src/build.sh"])
        
    const output = client.generatedCode(contents.directory("/"))
    console.log(await output.code().directory("/build").entries({
        path: "data"
    }))
  },
  { LogOutput: process.stderr }
)
#

stdout was just there for debugging;

I was getting really odd results where the paths that were appearing in the errors were not matching what I was inputting:

ClientError: resolve : lstat /tmp/buildkit-mount2956006930/build/build: no such file or directory:

I was just looking for /build, not /build/build

hasty socket
#

no need generated code here. just call directory after .withExec and than export

buoyant perch
#

Ah brilliant

hasty socket
#

not familiar with js api but it should be similar to this snippet

   ...
        .container()
        .from("node:20")
        .withMountedDirectory("/src", repo)
        .withWorkdir("/src")
        .withNewFile("/src/build.sh", {contents: buildSh})
        .withExec(["bash", "/src/build.sh"])
        .directory("/src/build")
        .export(ctx, "/export/path")
buoyant perch
#
await client
        .container()
        .from("node:20")
        .withMountedDirectory("/src", repo)
        .withWorkdir("/src")
        .withNewFile("/src/build.sh", {contents: buildSh})
        .withExec(["bash", "/src/build.sh"])
        .directory("/build")
        .export("artifacts")

Works as expected

#

What is ctx in your example?

hasty socket
#

just a habit from golang. it shouldn't exist in javascript. it's just a context object passed as parameter

buoyant perch
#

Ah gotcha

light lintel
#

🚀 thx aweris. @buoyant perch shall we resolve this?