Skip to main content

Quickstart

Test the application

The pipeline in the previous example was illustrative - you wouldn't ever use it in the real world! So let's write a Dagger pipeline to do something more useful, like running an application's tests.

The code listing below demonstrates a Dagger pipeline that runs tests for the example application, by executing the npm run test command.

tip

The npm run test 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.HostDirectoryOpts{
Exclude: []string{"node_modules/", "ci/", "build/"},
}))

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

// run application tests
out, err := runner.WithExec([]string{"npm", "test", "--", "--watchAll=false"}).
Stderr(ctx)
if err != nil {
panic(err)
}
fmt.Println(out)
}

This code listing does the following:

  • It creates a Dagger client with Connect() as before.
  • It uses the client's Container().From() method to initialize a new container from a base image - again, the node:16-slim image. This base image is the Node.js version to use for testing. The From() method returns a new Container object with the result.
  • It uses the Container.WithDirectory() method to mount the source code directory on the host at the /src mount point in the container, and the Container.WithWorkdir() method to set the working directory to that mount point.
    • Notice that the Container.WithDirectory() method accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes the node_modules (locally-installed dependencies), ci (pipeline code) and build (build artifacts) directories.
  • It uses the Container.WithExec() method to define the commands to install dependencies and run tests in the container - in this case, the commands npm install and npm test -- --watchAll=false.
  • It uses the Container.Stderr() method to return the error stream of the last executed command. No error output implies successful execution (all tests pass).
    • Failure, indicated by error output, will cause the pipeline to terminate.

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

dagger run go run ci/main.go
tip

The From(), WithDirectory(), WithWorkdir() and WithExec() methods all return a Container, making it easy to chain method calls together and create a pipeline that is intuitive to understand.