> ## Documentation Index
> Fetch the complete documentation index at: https://buildcharts.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Exporting build artifacts

> Export build artifacts for local workflows and CI.

Charts can export artifacts by writing files into `/output` inside the Dockerfile. The generated Bake plan then uses a local output to copy that content to `.buildcharts/output/<type>` on the host during `docker buildx bake`.

## Output behavior by stage type

| Stage type     | Output behavior                        |
| -------------- | -------------------------------------- |
| `build`        | `type=cacheonly`                       |
| `docker`       | `type=docker`                          |
| Any other type | Export to `.buildcharts/output/<type>` |

That means `test`, `nuget`, and any custom non-docker type export local files under `.buildcharts/output`.

## How local exports work

BuildCharts implements local outputs using Docker Buildx Bake build artifact exports.

For any non-`build` and non-`docker` stage type, the generated `docker-bake.hcl` includes a local output like:

```hcl theme={"theme":{"light":"plastic","dark":"plastic"}}
output = [
  "type=cacheonly,mode=max",
  "type=local,dest=.buildcharts/output/nuget"
]
```

`type=local` exports the filesystem contents of the target stage to a local directory. That means everything that exists in the final stage is copied to `.buildcharts/output/<type>`.

This is why charts that export artifacts typically use a final `FROM scratch` stage and copy only the artifacts into it, for example:

```dockerfile theme={"theme":{"light":"plastic","dark":"plastic"}}
FROM scratch AS nuget
COPY --from=pre-nuget /output /
```

For the underlying Docker pattern, see [Exporting build artifacts](https://docs.docker.com/guides/bake/#exporting-build-artifacts).

## Work with Docker outputs

`docker` stages use `type=docker`, so the image is loaded into your Docker environment instead of being exported to `.buildcharts/output/docker`.

Use tags in `build.yml` so the result is easy to reference:

```yaml theme={"theme":{"light":"plastic","dark":"plastic"}}
targets:
  src/App/App.csproj:
    - type: docker
      with:
        contexts:
          base: docker-image://mcr.microsoft.com/dotnet/runtime:10.0
        tags: ["docker.io/example/app:${VERSION}-${COMMIT}"]
```

## Dockerfile example

```dockerfile theme={"theme":{"light":"plastic","dark":"plastic"}}
###################################
# Target: pre-sample
###################################
FROM build AS pre-sample

# The target source path, for example `src/MyApp/MyApp.csproj`
ARG BUILDCHARTS_SRC

# The generated target type, for example `build`
ARG BUILDCHARTS_TYPE

RUN echo "BUILDCHARTS_SRC=$BUILDCHARTS_SRC" && \
    echo "BUILDCHARTS_TYPE=$BUILDCHARTS_TYPE" && \
    mkdir -p /output && \
    echo "$BUILDCHARTS_SRC" > /output/source.txt && \
    echo "$BUILDCHARTS_TYPE" > /output/type.txt

###################################
# Target: sample
###################################
FROM scratch AS sample
COPY --from=pre-sample /output/source.txt /output/source.txt
COPY --from=pre-sample /output/type.txt /output/type.txt
```
