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.
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.
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, thenode:16-slim
image. This base image is the Node.js version to use for testing. TheFrom()
method returns a newContainer
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 theContainer.WithWorkdir()
method to set the working directory to that mount point.- Notice that the
Container.WithDirectory()
accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes thenode_modules
(locally-installed dependencies) andci
(pipeline code) directories.
- Notice that the
- It uses the
Container.WithExec()
method to define the commands to install dependencies and run tests in the container - in this case, the commandsnpm install
andnpm 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
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.
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, thenode:16-slim
image. This base image is the Node.js version to use for testing. Thefrom()
method returns a newContainer
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 theContainer.withWorkdir()
method to set the working directory to that mount point.- Notice that the
Container.withDirectory()
accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes thenode_modules
(locally-installed dependencies) andci
(pipeline code) directories.
- Notice that the
- It uses the
Container.withExec()
method to define the commands to install dependencies and run tests in the container - in this case, the commandsnpm install
andnpm 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 node ci/index.mjs
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.
This code listing does the following:
- It creates a Dagger client with
with dagger.Connection()
as before. - It uses the client's
container().from_()
method to initialize a new container from a base image - again, thenode:16-slim
image. This base image is the Node.js version to use for testing. Thefrom_()
method returns a newContainer
object with the result. - It uses the
Container.with_directory()
method to mount the source code directory on the host at the/src
mount point in the container, and theContainer.with_workdir()
method to set the working directory to that mount point.- Notice that the
Container.with_directory()
accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes thenode_modules
(locally-installed dependencies) andci
(pipeline code) directories.
- Notice that the
- It uses the
Container.with_exec()
method to define the commands to install dependencies and run tests in the container - in this case, the commandsnpm install
andnpm 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 python ci/main.py
The from_()
, with_directory()
, with_workdir()
and with_exec()
methods all return a Container
, making it easy to chain method calls together and create a pipeline that is intuitive to understand.
- Go
- Node.js
- Python