Skip to main content

Quickstart

Build the application

If your application passes all its tests, the typical next step is to build it.

So, let's update the previous pipeline and add a build step to it, by running npm run build.

tip

The npm run build command is appropriate for a React application, but other applications are likely to use different commands. Modify your Dagger pipeline accordingly.

package main

import (
"context"
"fmt"
"os"

"dagger.io/dagger"
)

func main() {
ctx := context.Background()

// initialize Dagger client
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))

if err != nil {
panic(err)
}
defer client.Close()

// use a node:16-slim container
// mount the source code directory on the host
// at /src in the container
source := client.Container().
From("node:16-slim").
WithDirectory("/src", client.Host().Directory("."), dagger.ContainerWithDirectoryOpts{
Exclude: []string{"node_modules/", "ci/"},
})

// set the working directory in the container
// install application dependencies
runner := source.WithWorkdir("/src").
WithExec([]string{"npm", "install"})

// run application tests
test := runner.WithExec([]string{"npm", "test", "--", "--watchAll=false"})

// build application
// write the build output to the host
buildDir := test.WithExec([]string{"npm", "run", "build"}).
Directory("./build")

_, err = buildDir.Export(ctx, "./build")
if err != nil {
panic(err)
}

e, err := buildDir.Entries(ctx)
if err != nil {
panic(err)
}

fmt.Printf("build dir contents:\n %s\n", e)
}
  • It invokes the Container.WithExec() method again, this time to define the command npm run build in the container.
  • It obtains a reference to the build/ directory in the container with the Container.Directory() method. This method returns a Directory object.
  • It writes the build/ directory from the container to the host using the Directory.Export() method.

Run the pipeline by executing the command below from the application directory:

dagger run go run ci/main.go

After Dagger resolves the pipeline, the built application is available in a new build/ sub-directory of the application directory. Confirm this by executing the tree command in the application directory, as shown below:

tree build
build/
├── asset-manifest.json
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
├── robots.txt
└── static
├── css
│ ├── main.073c9b0a.css
│ └── main.073c9b0a.css.map
└── js
├── 787.305db8b6.chunk.js
├── 787.305db8b6.chunk.js.map
├── main.5e49019b.js
├── main.5e49019b.js.LICENSE.txt
└── main.5e49019b.js.map

3 directories, 14 files