Back to Dagger

Module Dependencies

docs/versioned_docs/version-0.17.2/api/module-dependencies.mdx

0.21.55.0 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Module Dependencies

Installation

You can call Dagger Functions from any other Dagger module in your own Dagger module simply by adding it as a module dependency with dagger install, as in the following example:

shell
dagger install github.com/shykes/daggerverse/[email protected]

This module will be added to your dagger.json:

json
...
"dependencies": [
  {
    "name": "hello",
    "source": "github.com/shykes/daggerverse/hello@54d86c6002d954167796e41886a47c47d95a626d"
  }
]

When you add a dependency to your module with dagger install, the dependent module will be added to the code-generation routines and can be accessed from your own module's code.

The entrypoint to accessing dependent modules from your own module's code is dag, the Dagger client, which is pre-initialized. It contains all the core types (like Container, Directory, etc.), as well as bindings to any dependencies your module has declared.

Here is an example of accessing the installed hello module from your own module's code:

<Tabs groupId="language"> <TabItem value="Go">
go
func (m *MyModule) Greeting(ctx context.Context) (string, error) {
  return dag.Hello().Hello(ctx)
}
</TabItem> <TabItem value="Python">
python
@function
async def greeting(self) -> str:
  return await dag.hello().hello()
</TabItem> <TabItem value="TypeScript">
typescript
@func()
async greeting(): Promise<string> {
  return await dag.hello().hello()
}
</TabItem> <TabItem value="PHP">
php
#[DaggerFunction]
public function greeting(): string
{
    return dag()->hello()->hello();
}
</TabItem> <TabItem value="Java">
java
@Function
public String greeting() throws ExecutionException, DaggerQueryException, InterruptedException {
    return dag().hello().hello();
}
</TabItem> </Tabs>

Here is a more complex example. It is a Dagger Function that utilizes a module from the Daggerverse to build a Go project, then chains a Dagger API method to open an interactive terminal session in the build directory.

First, install the module:

shell
dagger install github.com/kpenfound/dagger-modules/[email protected]

Next, create a new Dagger Function:

<Tabs groupId="language"> <TabItem value="Go"> ```go file=./snippets/dependencies/chaining/go/main.go ``` </TabItem> <TabItem value="Python"> ```python file=./snippets/dependencies/chaining/python/main.py ``` </TabItem> <TabItem value="TypeScript"> ```typescript file=./snippets/dependencies/chaining/typescript/index.ts ``` </TabItem> <TabItem value="PHP"> ```php file=./snippets/dependencies/chaining/php/src/MyModule.php ``` </TabItem> <TabItem value="Java"> ```java file=./snippets/dependencies/chaining/java/MyModule.java ``` </TabItem> </Tabs>

This Dagger Function accepts two arguments - the source directory and a list of build arguments - and does the following:

  • It invokes the Golang module via the dag Dagger client.
  • It calls a Dagger Function from the module to build the source code and return a just-in-time directory with the compiled binary.
  • It chains a core Dagger API method to open an interactive terminal session in the returned directory.

Here is an example call for this Dagger Function:

<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'example https://github.com/golang/example#master:/hello .' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." example https://github.com/golang/example#master:/hello . ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call example --build-src=https://github.com/golang/example#master:/hello --build-args=. ``` </TabItem> </Tabs>

You can also use local modules as dependencies. However, they must be stored in a sub-directory of your module. For example:

shell
dagger install ./path/to/module

:::note Installing a module using a local path (relative or absolute) is only possible if your module is within the repository root (for Git repositories) or the directory containing the dagger.json file (for all other cases). :::

Uninstallation

To remove a dependency from your Dagger module, use the dagger uninstall command. The dagger uninstall command can be passed either a remote repository reference or a local module name.

The commands below are equivalent:

shell
dagger uninstall hello
dagger uninstall github.com/shykes/daggerverse/hello

Update

To update a dependency in your Dagger module to the latest version (or the version specified), use the dagger update command. The target module must be local.

The commands below are equivalent:

shell
dagger update hello
dagger update github.com/shykes/daggerverse/hello

:::note Given a dependency like github.com/path/name@branch/tag:

  • dagger update github.com/path/name updates the dependency to the latest commit of the branch/tag.
  • dagger update github.com/path/name@version updates the dependency to the latest commit for the version branch/tag. :::