Skip to content

Commit

Permalink
Updated content
Browse files Browse the repository at this point in the history
Signed-off-by: Vikram Vaswani <vikram@dagger.io>
  • Loading branch information
vikram-dagger committed May 8, 2024
1 parent f926c4e commit 4d9f4f2
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 25 deletions.
33 changes: 28 additions & 5 deletions docs/current_docs/quickstart/build.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ If your application passes all its tests, the typical next step is to build it.
<Tabs groupId="language">
<TabItem value="Go">

Update the generated `dagger/main.go` file with the following code:
Update the `dagger/main.go` file with the following code:

```go file=./snippets/build/go/main.go
```
Expand All @@ -29,10 +29,32 @@ This Dagger Function does many of the same things as the previous one, but with

</TabItem>
<TabItem value="Python">
FIXME

Update the `dagger/src/main/__init__.py` file with the following code:

```python file=./snippets/build/python/__init__.py
```

This Dagger Function does many of the same things as the previous one, but with these modifications:

- It modifies the command passed to the `container().with_exec()` method, changing it to `npm run build`.
- The `npm run build` command builds the application and places the build in a `dist/` directory in the container filesystem.
- It uses the `container().directory()` method to return the directory as Dagger's special `Directory` type. This is called a "just-in-time" directory - a transient artifact produced as the result of a Dagger Function.

</TabItem>
<TabItem value="TypeScript">
FIXME

Update the `dagger/src/index.ts` file with the following code:

```typescript file=./snippets/build/typescript/index.ts
```

This Dagger Function does many of the same things as the previous one, but with these modifications:

- It modifies the command passed to the `container().withExec()` method, changing it to `npm run build`.
- The `npm run build` command builds the application and places the build in a `dist/` directory in the container filesystem.
- It uses the `container().directory()` method to return the directory as Dagger's special `Directory` type. This is called a "just-in-time" directory - a transient artifact produced as the result of a Dagger Function.

</TabItem>
</Tabs>

Expand Down Expand Up @@ -90,14 +112,15 @@ func (m *HelloDagger) BuildWithExport(source *Directory) *Directory {
Directory("./dist").
Export("./build")
}

```
</TabItem>
<TabItem value="Python">

FIXME

</TabItem>
<TabItem value="TypeScript">

FIXME
</TabItem>
<TabItem value="Dagger CLI">
```shell
Expand Down
22 changes: 18 additions & 4 deletions docs/current_docs/quickstart/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ The `Test()` and `Build()` Dagger Functions now use a cache volume for the appli
- They use the `Container().WithMountedCache(`) method to mount this cache volume at the `node_modules/` mount point in the container.
</TabItem>
<TabItem value="Python">
FIXME

```python file=./snippets/cache/python/__init__.py
```

The `test()` and `build()` Dagger Functions now use a cache volume for the application dependencies.

- They use the `dag.cache_volume()` method to initialize a new cache volume.
- They use the `container().with_mounted_cache(`) method to mount this cache volume at the `node_modules/` mount point in the container.

</TabItem>
<TabItem value="TypeScript">
FIXME
```typescript file=./snippets/cache/typescript/index.ts
```

The `test()` and `build()` Dagger Functions now use a cache volume for the application dependencies.

- They use the `dag.cacheVolume()` method to initialize a new cache volume.
- They use the `container().withMountedCache(`) method to mount this cache volume at the `node_modules/` mount point in the container.
</TabItem>
</Tabs>

Expand All @@ -49,8 +63,8 @@ The revised pipeline will produce the same result as before. On the first `dagge
Call the Dagger Function a few times and time each run to confirm that subsequent executions are faster:

```shell
dagger call ci \
--source=.
dagger call \
ci --source=.
```

:::tip CACHING
Expand Down
31 changes: 27 additions & 4 deletions docs/current_docs/quickstart/ci.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A better approach is to create a "master" Dagger Function, which can then orches
<Tabs groupId="language">
<TabItem value="Go">

Update the generated `dagger/main.go` file and create a new Dagger Function which internally uses the three Dagger Functions created in previous sections:
Update the `dagger/main.go` file and create a new Dagger Function which internally uses the three Dagger Functions created in previous sections:

```go file=./snippets/ci/go/main.go
```
Expand All @@ -37,12 +37,35 @@ This Dagger Function expects a `Directory` as argument. It performs the followin
- It calls the `Package()` Dagger Function to build and return a container image of the application.
- It calls the `Container().Publish()` method to publish the container image to the registry and return the image identifier.
</TabItem>

<TabItem value="Python">
FIXME

Update the `dagger/src/main/__init__.py` file and create a new Dagger Function which internally uses the three Dagger Functions created in previous sections:

```python file=./snippets/ci/python/__init__.py
```

This Dagger Function expects a `Directory` as argument. It performs the following operations:

- It calls the `test()` Dagger Function to run the application's tests and return the results.
- It calls the `build()` Dagger Function to build the application and return the build output directory.
- It calls the `package()` Dagger Function to build and return a container image of the application.
- It calls the `container().publish()` method to publish the container image to the registry and return the image identifier.

</TabItem>
<TabItem value="TypeScript">
FIXME

Update the `dagger/src/index.ts` file and create a new Dagger Function which internally uses the three Dagger Functions created in previous sections:

```typescript file=./snippets/ci/typescript/index.ts
```

This Dagger Function expects a `Directory` as argument. It performs the following operations:

- It calls the `test()` Dagger Function to run the application's tests and return the results.
- It calls the `build()` Dagger Function to build the application and return the build output directory.
- It calls the `package()` Dagger Function to build and return a container image of the application.
- It calls the `container().publish()` method to publish the container image to the registry and return the image identifier.

</TabItem>
</Tabs>

Expand Down
30 changes: 26 additions & 4 deletions docs/current_docs/quickstart/package.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ At this point, you've written Dagger Functions to test, build and deliver the ap
<Tabs groupId="language">
<TabItem value="Go">

Update the generated `dagger/main.go` file and create a new Dagger Function to copy the build output directory into an NGINX web server container image.
Update the `dagger/main.go` file and create a new Dagger Function to copy the build output directory into an NGINX web server container image.

```go file=./snippets/package/go/main.go
```
Expand All @@ -29,10 +29,32 @@ This Dagger Function performs the following operations:

</TabItem>
<TabItem value="Python">
FIXME

Update the `dagger/src/main/__init__.py` file and create a new Dagger Function to copy the build output directory into an NGINX web server container image.

```python file=./snippets/package/python/__init__.py
```

This Dagger Function performs the following operations:

- It uses the `dag` client's `container().from_()` method to initialize a new container from a base image - in this case, the `nginx:1.25-alpine` image. The `from_()` method returns a new `Container` object representing the container image.
- It uses the `container().with_directory()` method to write the build output directory to the `/usr/share/nginx/html` path in the container and return a revised `Container`. The location of the build output directory is obtained from the `build` argument passed to the Dagger Function.
- It uses the `container().with_exposed_port()` function to expose port 80 (the default NGINX port in the `nginx:1.25-alpine` image) and return a revised `Container`.

</TabItem>
<TabItem value="TypeScript">
FIXME

Update the `dagger/src/index.ts` file and create a new Dagger Function to copy the build output directory into an NGINX web server container image.

```typescript file=./snippets/package/typescript/index.ts
```

This Dagger Function performs the following operations:

- It uses the `dag` client's `container().from()` method to initialize a new container from a base image - in this case, the `nginx:1.25-alpine` image. The `from()` method returns a new `Container` object representing the container image.
- It uses the `container().withDirectory()` method to write the build output directory to the `/usr/share/nginx/html` path in the container and return a revised `Container`. The location of the build output directory is obtained from the `build` argument passed to the Dagger Function.
- It uses the `container().withExposedPort()` function to expose port 80 (the default NGINX port in the `nginx:1.25-alpine` image) and return a revised `Container`.

</TabItem>
</Tabs>

Expand Down Expand Up @@ -142,7 +164,7 @@ Test the published container image using the command below (update the image nam
docker run -d --rm --publish 8080:80 ttl.sh/hello-dagger-47384
```

Browse to port 8080 of the Dagger host and confirm that you see the application index page, served by NGINX:
Browse to port 8080 of the Dagger host and confirm that you see the application index page, served by NGINX.

FIXME: add image

Expand Down
42 changes: 34 additions & 8 deletions docs/current_docs/quickstart/refactor.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,52 @@ With your knowledge of the Dagger Functions available in the Node module, let's
<Tabs groupId="language">
<TabItem value="Go">

Update the generated `dagger/main.go` file with the following code:
Update the `dagger/main.go` file with the following code:

```go file=./snippets/refactor/go/main.go
```

This code listing revises the `Test()` and `Build()` Dagger Functions from earlier, replacing calls to the core Dagger API with calls to Dagger Functions from the Node module.

- It calls the Node module's constructor function via the `dag` client. This function returns a `node` container image with the given Node.js version. This container image is represented as a `Node` object.
- It calls the `Node.WithNpm()` function, which returns a revised container after adding the `npm` package manager and a cache volume for `npm`.
- It calls the `Node.WithSource()` function, which returns a revised container with the directory passed to the Dagger Function via the `source` argument mounted in the container filesystem. The `Node.WithSource()` function also creates a cache volume for application dependencies.
- It calls the `Node.Install()` function, which runs `npm install` in the container and returns a revised container with the application's dependencies installed.
- It calls the `Node.Commands().Run()` function with an array of arguments, which returns a revised container configured to execute the `npm run test:unit run` or the `npm run build` command in the container image.
- It calls the `Node().WithNpm()` function, which returns a revised container after adding the `npm` package manager and a cache volume for `npm`.
- It calls the `Node().WithSource()` function, which returns a revised container with the directory passed to the Dagger Function via the `source` argument mounted in the container filesystem. The `Node().WithSource()` function also creates a cache volume for application dependencies.
- It calls the `Node().Install()` function, which runs `npm install` in the container and returns a revised container with the application's dependencies installed.
- It calls the `Node().Commands().Run()` function with an array of arguments, which returns a revised container configured to execute the `npm run test:unit run` or the `npm run build` command in the container image.
- It uses the `Container().Stdout()` method to return the test results or the `Container().Directory()` method to return the build output directory.
</TabItem>

<TabItem value="Python">
FIXME

Update the `dagger/src/main/__init__.py` file with the following code:

```python file=./snippets/refactor/python/__init__.py
```

This code listing revises the `test()` and `build()` Dagger Functions from earlier, replacing calls to the core Dagger API with calls to Dagger Functions from the Node module.

- It calls the Node module's constructor function via the `dag` client. This function returns a `node` container image with the given Node.js version. This container image is represented as a `node` object.
- It calls the `node().with_npm()` function, which returns a revised container after adding the `npm` package manager and a cache volume for `npm`.
- It calls the `node().with_source()` function, which returns a revised container with the directory passed to the Dagger Function via the `source` argument mounted in the container filesystem. The `node().with_source()` function also creates a cache volume for application dependencies.
- It calls the `node().install()` function, which runs `npm install` in the container and returns a revised container with the application's dependencies installed.
- It calls the `node().commands().run()` function with an array of arguments, which returns a revised container configured to execute the `npm run test:unit run` or the `npm run build` command in the container image.
- It uses the `container().stdout()` method to return the test results or the `container().directory()` method to return the build output directory.

</TabItem>
<TabItem value="TypeScript">
FIXME
Update the `dagger/src/index.ts` file with the following code:

```typescript file=./snippets/refactor/typescript/index.ts
```

This code listing revises the `test()` and `build()` Dagger Functions from earlier, replacing calls to the core Dagger API with calls to Dagger Functions from the Node module.

- It calls the Node module's constructor function via the `dag` client. This function returns a `node` container image with the given Node.js version. This container image is represented as a `node` object.
- It calls the `node().withNpm()` function, which returns a revised container after adding the `npm` package manager and a cache volume for `npm`.
- It calls the `node().withSource()` function, which returns a revised container with the directory passed to the Dagger Function via the `source` argument mounted in the container filesystem. The `node().withSource()` function also creates a cache volume for application dependencies.
- It calls the `node().install()` function, which runs `npm install` in the container and returns a revised container with the application's dependencies installed.
- It calls the `node().commands().run()` function with an array of arguments, which returns a revised container configured to execute the `npm run test:unit run` or the `npm run build` command in the container image.
- It uses the `container().stdout()` method to return the test results or the `container().directory()` method to return the build output directory.

</TabItem>
</Tabs>

Expand Down
34 changes: 34 additions & 0 deletions docs/current_docs/quickstart/snippets/build/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { dag, Container, Directory, object, func } from '@dagger.io/dagger'

@object()
class HelloDagger {
/**
* Returns a directory with the production build
*/
@func()
build(source: Directory): Directory {
return dag
.container()
.from('node:21-slim')
.withDirectory('/src', source.withoutDirectory('dagger'))
.withWorkdir('/src')
.withExec(['npm', 'install'])
.withExec(['npm', 'run', 'build'])
.directory('./dist')
}

/**
* Returns the result of running unit tests
*/
@func()
async test(source: Directory): Promise<string> {
return dag
.container()
.from('node:21-slim')
.withDirectory('/src', source.withoutDirectory('dagger'))
.withWorkdir('/src')
.withExec(['npm', 'install'])
.withExec(['npm', 'run', 'test:unit', 'run'])
.stdout()
}
}
59 changes: 59 additions & 0 deletions docs/current_docs/quickstart/snippets/ci/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { dag, Container, Directory, object, func } from '@dagger.io/dagger'

@object()
class HelloDagger {
/**
* Tests, builds, packages and publishes the application
*/
@func()
async ci(source: Directory): Promise<string> {
// run tests
this.test(source)
// obtain the build output directory
let build = this.build(source)
// create and publish a container with the build output
return await this.package(build).publish('ttl.sh/myapp-' + Math.floor(Math.random() * 10000000))
}

/**
* Returns a container with the production build
*/
@func()
package(build: Directory): Container {
return dag
.container()
.from('nginx:1.25-alpine')
.withDirectory('/usr/share/nginx/html', build)
.withExposedPort(80)
}

/**
* Returns a directory with the production build
*/
@func()
build(source: Directory): Directory {
return dag
.container()
.from('node:21-slim')
.withDirectory('/src', source.withoutDirectory('dagger'))
.withWorkdir('/src')
.withExec(['npm', 'install'])
.withExec(['npm', 'run', 'build'])
.directory('./dist')
}

/**
* Returns the result of running unit tests
*/
@func()
async test(source: Directory): Promise<string> {
return dag
.container()
.from('node:21-slim')
.withDirectory('/src', source.withoutDirectory('dagger'))
.withWorkdir('/src')
.withExec(['npm', 'install'])
.withExec(['npm', 'run', 'test:unit', 'run'])
.stdout()
}
}
Loading

0 comments on commit 4d9f4f2

Please sign in to comment.