Repository navigation
docs: Add documentation on local cache export #2035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cecac47
docs: Add documentation on local cache export
TomChv efdf641
docs: Add documentation on registry cache export
TomChv f8afa2d
docs: Add documentation on GitHub action cache export
TomChv 0fce839
docs: Fix typo found
TomChv e415536
docs: Add tests file for persistent cache
TomChv 1651d35
docs: Reverse doc orders and improve docs
TomChv ebac133
docs: Fix docker cache-registry container port
gerhard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| --- | ||
| slug: /1230/persistent-cache-with-dagger | ||
| displayed_sidebar: 0.2 | ||
| --- | ||
|
|
||
| # Persistent cache | ||
|
|
||
| CI that takes an eternity is a real pain and can become a bottleneck when your | ||
| infrastructure and process grow. But Dagger, working with a Buildkit daemon, | ||
| has a powerful cache system that triggers actions only when it's necessary. | ||
|
|
||
| However, sometimes you can't keep a persistent Buildkit daemon along your CI/CD. | ||
| For instance, if you use a GitHub Actions runner, your daemon will be created on each | ||
| run and **cache will be lost**. | ||
|
|
||
| In this page, we will see how to use `--cache-from` and `--cache-to` flags to | ||
| keep a permanent cache, from a local environment to GitHub Actions. | ||
|
|
||
| ## Ephemeral cache | ||
|
|
||
| As an example, we will use a Dagger plan to build a Go program. | ||
|
|
||
| You can use any Go project and the following snippet to build it in a | ||
| standard way | ||
|
|
||
| ```cue file=../tests/guides/persistent-cache/build.cue title="build.cue" | ||
| package ci | ||
|
|
||
| import ( | ||
| "dagger.io/dagger" | ||
| "universe.dagger.io/go" | ||
| ) | ||
|
|
||
| dagger.#Plan & { | ||
| // Retrieve source from current directory | ||
| // Input | ||
| client: filesystem: ".": read: { | ||
| contents: dagger.#FS | ||
| // Include only contents we need to build our project | ||
| // Any other files or patterns can be added | ||
| include: ["**/*.go", "go.mod", "go.sum"] | ||
| } | ||
|
|
||
| // Output | ||
| client: filesystem: "./output": write: { | ||
| contents: actions.build.output | ||
| } | ||
|
|
||
| actions: { | ||
| // Alias on source | ||
| _source: client.filesystem.".".read.contents | ||
|
|
||
| // Build go binary | ||
| build: go.#Build & { | ||
| source: _source | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| To build a Go binary, just run `dagger do build`. It should then take some time to | ||
| install dependencies, build the binary and output it. | ||
|
|
||
| Here's an example of a run | ||
|
|
||
| ```shell | ||
| [✔] actions.build.container 338.0s | ||
| [✔] client.filesystem.".".read 0.1s | ||
| [✔] actions.build.container.export 0.1s | ||
| [✔] client.filesystem."./output".write 0.2s | ||
| ``` | ||
|
|
||
| Indeed, Dagger has an ephemeral cache so if you rerun it, that shouldn't take | ||
| that long. | ||
|
|
||
| ```shell | ||
| [✔] actions.build.container 2.9s | ||
| [✔] client.filesystem.".".read 0.0s | ||
| [✔] actions.build.container.export 0.0s | ||
| [✔] client.filesystem."./output".write 0.1s | ||
| ``` | ||
|
|
||
| But if you stop the Buildkit daemon and remove its storage volume, cache will be lost and | ||
| all actions will be executed again. | ||
|
|
||
| :::info | ||
| Now we have seen how ephemeral cache works, let's continue to understand how | ||
| store cache in your local filesystem, so you can clean your Buildkit daemon without | ||
| losing all your CI's cache. | ||
| ::: | ||
|
|
||
| ## Persistent cache in a remote registry | ||
|
|
||
| Buildkit can also import/export cache to a registry. This is a great way to | ||
| share cache between your team and avoid flooding your filesystem. | ||
|
|
||
| :::tip | ||
| Using a registry as cache storage is more efficient than local storage because Buildkit will only re-export | ||
| missing layers on multiple runs. | ||
| ::: | ||
|
|
||
| To store cache in an external registry, you just need to add flags to dagger command : | ||
| `dagger do <action> --cache-to type=registry,mode=max,ref=<registry target>/<image> --cache-from type=registry,ref=<registry target>/<image>` | ||
|
|
||
| :::tip | ||
| Using `mode=max` argument will cache **all** layers from intermediate | ||
|
TomChv marked this conversation as resolved.
Resolved
|
||
| steps, which is really useful in the context of Dagger where you will have | ||
| multiple steps to execute. | ||
| To only store the final layers of the exported result, use `mode=min`. | ||
| ::: | ||
|
|
||
| Let's first deploy a simple registry in your localhost | ||
|
|
||
| ```shell | ||
| docker run -d -p 5000:5000 --restart=always --name cache-registry registry:2 | ||
| ``` | ||
|
|
||
| Then run `dagger do build` with export cache flags. | ||
|
|
||
| ```shell | ||
| dagger do build --cache-to type=registry,mode=max,ref=localhost:5000/cache --cache-from type=registry,ref=localhost:5000/cache | ||
| [✔] actions.build.container 1.3s | ||
| [✔] client.filesystem.".".read 0.0s | ||
| [✔] actions.build.container.export 0.0s | ||
| [✔] client.filesystem."./output".write 0.1s | ||
| ``` | ||
|
|
||
| :::info | ||
| See more options on registry export at [Buildkit cache documentation](https://github.com/moby/buildkit/blob/v0.10.1/README.md#registry-push-image-and-cache-separately) | ||
| ::: | ||
|
|
||
| ## Persistent cache in your local filesystem | ||
|
|
||
| To store cache in your local filesystem, you just need to change flags values to match `type=local`. | ||
|
|
||
| Here's an example that exports cache to a local directory at path `./storage`. | ||
|
|
||
| ```shell | ||
| dagger do build --cache-to type=local,mode=max,dest=storage | ||
| # ... | ||
|
|
||
| tree storage -L 1 | ||
| storage | ||
| ├── blobs | ||
| ├── index.json | ||
| └── ingest | ||
| ``` | ||
|
|
||
| As shown above, new directory has been created that contains cache artifacts from the run | ||
|
|
||
| :::caution | ||
| Local cache exports will overwrite anything already present in the directory, | ||
| including any previous cache exports. Cache for distinct actions can be | ||
| exported to different directories in order to not overwrite each other, | ||
| but this may currently result in duplicated data if the two actions share | ||
| any exported cache data in common. | ||
| ::: | ||
|
|
||
| To import the cache previously stored, you can use `--cache-from type=local,src=<cache folder>`. | ||
|
|
||
| Here's an example, using a new buildkit daemon running on a Docker installation | ||
|
|
||
| ```shell | ||
| # Down buildkit daemon | ||
| # It throws all buildkit data | ||
| docker container stop dagger-buildkitd && docker container rm dagger-buildkitd && docker volume rm dagger-buildkitd | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add a warning that this throws all your data away.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 1651d35 |
||
|
|
||
| # Import cache on rebuild | ||
| dagger do build --cache-to type=local,mode=max,dest=storage --cache-from type=local,src=storage | ||
| [✔] actions.build.container 2.3s | ||
| [✔] client.filesystem.".".read 0.1s | ||
| [✔] actions.build.container.export 0.0s | ||
| [✔] client.filesystem."./output".write 0.4s | ||
| ``` | ||
|
|
||
| :::info | ||
| In this part, we have learned to export and import cache using a local filesystem, if you want | ||
| to see more options on local export, look at [Buildkit cache documentation](https://github.com/moby/buildkit/blob/v0.10.1/README.md#local-directory-1) | ||
| ::: | ||
|
|
||
| ## Persistent cache in GitHub Actions | ||
|
|
||
| Buildkit has a builtin support for storing cache with GitHub Action. | ||
|
|
||
| This cache backend is not that different from `local` or `registry` exports, Let's integrate cache in a | ||
| simple workflow using the one: | ||
|
|
||
| ```yaml title=".github/workflows/build-example.cue" | ||
| name: "Dagger Export" | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| build: | ||
| name: "Build" | ||
| runs-on: ubuntu-latest | ||
| # Set cache export environment | ||
| env: | ||
| DAGGER_CACHE_FROM: type=gha,scope=dagger-cache-example | ||
| DAGGER_CACHE_TO: type=gha,mode=max,scope=dagger-cache-example | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v2 | ||
|
|
||
| - name: "Run Dagger" | ||
| uses: dagger/dagger-for-github@v2 | ||
| with: | ||
| cmds: | | ||
| do build | ||
| ``` | ||
|
|
||
| :::warning | ||
| To avoid invalidating cache between your PR, you can take inspiration from [Dagger ci](https://github.com/dagger/dagger/blob/v0.2.8/.github/workflows/dagger-ci.yml#L61) | ||
| ::: | ||
|
|
||
| :::info | ||
| See more options on GitHub export at [Buildkit cache documentation](https://github.com/moby/buildkit/blob/v0.10.1/README.md#github-actions-cache-experimental) | ||
| ::: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ci | ||
|
|
||
| import ( | ||
| "dagger.io/dagger" | ||
| "universe.dagger.io/go" | ||
| ) | ||
|
|
||
| dagger.#Plan & { | ||
| // Retrieve source from current directory | ||
| // Input | ||
| client: filesystem: ".": read: { | ||
| contents: dagger.#FS | ||
| // Include only contents we need to build our project | ||
| // Any other files or patterns can be added | ||
| include: ["**/*.go", "go.mod", "go.sum"] | ||
| } | ||
|
|
||
| // Output | ||
| client: filesystem: "./output": write: contents: actions.build.output | ||
|
|
||
| actions: { | ||
| // Alias on source | ||
| _source: client.filesystem.".".read.contents | ||
|
|
||
| // Build go binary | ||
| build: go.#Build & { | ||
| source: _source | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module dagger.io/persistentcache | ||
|
|
||
| go 1.17 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| fmt.Println("Hello Dagger!") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.