Skip to main content

anodizer_core/config/
docker.rs

1use std::collections::HashMap;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use super::build::BuildHooksConfig;
7use super::release::{SkipPushConfig, skip_push_schema};
8use super::{StringOrBool, deserialize_string_or_bool_opt};
9
10// Use `DockerV2Config` (canonical) for docker image builds.
11
12/// Per-pipe retry configuration for `docker.retry` / `docker_manifest.retry`.
13///
14/// **Deprecated**: prefer the top-level `retry:` block ([`super::RetryConfig`])
15/// which applies to docker pipes (and every other network-bound stage) via
16/// `Project.Retry`. When a per-pipe block is present alongside the top-level
17/// block, the per-pipe values win for back-compat, but
18/// `stage-docker::resolve_retry_params` emits a one-shot deprecation warning.
19/// New configs should leave this field unset.
20//
21// Note: `#[deprecated]` on the type cascades through derive-generated impls
22// (Default, Serialize, JsonSchema, ...) and is hard to silence cleanly, so the
23// deprecation lives in (a) this rustdoc prose, (b) the runtime `tracing::warn!`
24// fired once per process by `stage-docker::resolve_retry_params`, and (c) the
25// schemars-generated JSON-schema description carries the same prose for
26// editor / IDE consumers.
27#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
28#[serde(default, deny_unknown_fields)]
29pub struct DockerRetryConfig {
30    /// Number of retry attempts for failed docker push operations
31    /// (default: 10, set in `crates/stage-docker/src/lib.rs::resolve_retry_settings`).
32    pub attempts: Option<u32>,
33    /// Duration string for the initial retry delay (default: `"10s"`).
34    /// Examples: `"1s"`, `"500ms"`.
35    pub delay: Option<String>,
36    /// Maximum delay between retries (default: `"5m"`). Caps the exponential
37    /// backoff so attempt-9 with a 10s base does not stretch to ~42 min.
38    /// Example: `"30s"`.
39    pub max_delay: Option<String>,
40}
41
42// ---------------------------------------------------------------------------
43// DockerV2Config
44// ---------------------------------------------------------------------------
45
46/// Docker V2 configuration — the canonical Docker build API.
47///
48/// Notable surface:
49/// - `images` + `tags` (cleaner separation than a single `image_templates` list)
50/// - `annotations` map for OCI annotations (`--annotation`)
51/// - `build_args` map for build-time variables
52/// - `skip` as a [`StringOrBool`] template for conditional opt-out
53/// - `sbom` as a [`StringOrBool`] — when truthy, adds `--sbom=true` to buildx
54/// - `flags` for arbitrary extra `docker build` flags
55/// - `platforms` is the only target selector — no per-arch field overrides
56#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
57#[serde(default, deny_unknown_fields)]
58pub struct DockerV2Config {
59    /// Unique identifier for this Docker V2 config.
60    pub id: Option<String>,
61    /// Build IDs filter: only include binary artifacts whose metadata `id` is in this list.
62    pub ids: Option<Vec<String>>,
63    /// Path to the Dockerfile relative to the project root.
64    pub dockerfile: String,
65    /// Base image names (e.g., ["ghcr.io/owner/app"]). Combined with `tags` to form full references.
66    pub images: Vec<String>,
67    /// Tag suffixes (e.g., ["latest", "{{ Version }}"]). Each image is tagged with each tag.
68    pub tags: Vec<String>,
69    /// OCI labels to apply to the image via `--label key=value` flags.
70    pub labels: Option<HashMap<String, String>>,
71    /// Auto-inject the standard predefined `org.opencontainers.image.*` labels
72    /// (created, source, revision, version, title, description, licenses, url,
73    /// documentation, vendor), derived from project/git/Cargo context. Default
74    /// ON; set `oci_labels: false` to opt out. Each auto-label is emitted only
75    /// when its value is derivable, and any key the user also supplies in
76    /// `labels:` wins (the auto value never clobbers an explicit user label).
77    /// `created` is derived from `SOURCE_DATE_EPOCH` for byte-reproducibility
78    /// (never wall-clock) and is omitted when no reproducible source date is
79    /// resolvable.
80    #[serde(deserialize_with = "deserialize_string_or_bool_opt", default)]
81    pub oci_labels: Option<StringOrBool>,
82    /// OCI annotations to apply via `--annotation key=value` flags.
83    pub annotations: Option<HashMap<String, String>>,
84    /// Extra files to copy into the Docker build context.
85    pub extra_files: Option<Vec<String>>,
86    /// Target platforms for multi-arch builds (e.g., ["linux/amd64", "linux/arm64"]).
87    pub platforms: Option<Vec<String>>,
88    /// Build arguments passed as `--build-arg KEY=VALUE`.
89    ///
90    /// Each value is template-expanded and forwarded verbatim to buildx
91    /// (one argv token per pair, no shell tokenization). Prefer
92    /// `{{ Env.VAR }}` over raw user-config strings for secrets — buildx
93    /// records build-args in image history by default, so plaintext values
94    /// here propagate into the image metadata.
95    pub build_args: Option<HashMap<String, String>>,
96    /// Retry configuration for docker push operations.
97    pub retry: Option<DockerRetryConfig>,
98    /// Arbitrary extra flags passed to the docker build command.
99    pub flags: Option<Vec<String>>,
100    /// When truthy, skip this docker build entirely. Supports templates.
101    /// Accepts the legacy `disable:` spelling via serde alias for back-compat.
102    #[serde(
103        default,
104        alias = "disable",
105        deserialize_with = "deserialize_string_or_bool_opt"
106    )]
107    pub skip: Option<StringOrBool>,
108    /// When truthy, adds `--sbom=true` to buildx. Supports templates.
109    #[serde(deserialize_with = "deserialize_string_or_bool_opt", default)]
110    pub sbom: Option<StringOrBool>,
111    /// Pre/post hooks for this dockers_v2 config. Each hook accepts the same
112    /// `cmd`/`dir`/`env`/`output` shape as build/archive hooks. `pre` hooks
113    /// run after the staging directory is prepared but before `docker buildx
114    /// build`; `post` hooks run after the image digest is captured. Hook
115    /// commands, working directories, and env values are template-expanded;
116    /// in addition to the standard template surface, hooks see:
117    ///
118    /// - `{{ Images }}` — list of `image:tag` references for this build.
119    ///   Iterate via `{% for img in Images %}{{ img }}{% endfor %}` to mirror
120    ///   a list exposure of the same field; `{{ Images | join(sep=",") }}`
121    ///   reproduces a flat comma-separated string for legacy templates.
122    /// - `{{ Dockerfile }}` — path to the rendered Dockerfile
123    /// - `{{ ContextDir }}` — path to the buildx context staging directory
124    /// - `{{ Digest }}` — image manifest digest (post hooks only)
125    /// - `{{ BaseImage }}` / `{{ BaseImageDigest }}` — final-stage base image
126    ///   (the `BaseImage` / `BaseImageDigest` overlay)
127    pub hooks: Option<BuildHooksConfig>,
128    /// Docker backend for build commands: `"buildx"` (default) or `"podman"`.
129    ///
130    /// The default `"buildx"` invokes `docker buildx build` with the full set
131    /// of BuildKit features (multi-platform, attestations, `--rewrite-timestamp`,
132    /// SBOM, OCI exporter). Setting `use: podman` swaps the binary to
133    /// `podman build` and disables every buildx-only flag — anodizer rejects
134    /// configs that mix `use: podman` with `sbom: true`, `--rewrite-timestamp`,
135    /// `--provenance`, `--attest`, `--cache-from`, `--cache-to`, `--output`,
136    /// or `--sbom` because plain podman does not recognise them.
137    ///
138    /// **Linux-only.** The podman backend is
139    /// restricted to Linux hosts. Configs setting `use: podman` on macOS or
140    /// Windows fail at config-validation time with a clear error rather than
141    /// blowing up later when `podman` is not on `PATH`.
142    #[serde(rename = "use")]
143    pub use_backend: Option<String>,
144    // No `skip_push` field — use the canonical `skip:` to suppress
145    // the publish step (matches every other publisher / pipe in anodizer).
146}
147
148// ---------------------------------------------------------------------------
149// DockerDigestConfig
150// ---------------------------------------------------------------------------
151
152/// Controls docker image digest file creation.
153///
154/// After each docker image push, a digest file (containing the sha256 digest)
155/// is written to the dist directory. This config controls whether that happens
156/// and how the files are named.
157#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
158#[serde(default, deny_unknown_fields)]
159pub struct DockerDigestConfig {
160    /// When truthy, disable docker digest artifact creation.
161    /// Accepts the legacy `disable:` spelling via serde alias for back-compat.
162    #[serde(
163        alias = "disable",
164        deserialize_with = "deserialize_string_or_bool_opt",
165        default
166    )]
167    pub skip: Option<StringOrBool>,
168    /// Template for the digest artifact filename.
169    /// Default: tag-based naming (e.g., "ghcr.io_owner_app_v1.0.0.digest").
170    pub name_template: Option<String>,
171}
172
173// ---------------------------------------------------------------------------
174// DockerManifestConfig
175// ---------------------------------------------------------------------------
176
177/// Deprecated: prefer `dockers_v2` (which produces multi-arch manifests via
178/// the `platforms:` field automatically). `DockerManifestConfig` is retained
179/// for back-compat with imported configs and for the niche case
180/// of stitching together manifest lists from images that were not built by
181/// `dockers_v2` in the same run.
182///
183/// The v1 docker / docker
184/// manifest pipes deprecated in favour of the v2 buildx flow. The rustdoc
185/// here is the load-bearing surface for the deprecation: it flows into the
186/// schemars-generated JSON Schema (consumed by IDEs / editor tooling) and
187/// rustdoc HTML, both of which are how downstream config authors discover
188/// that the v2 pipe is the preferred entry point.
189#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
190#[serde(default, deny_unknown_fields)]
191pub struct DockerManifestConfig {
192    /// Template for the manifest name, e.g. "ghcr.io/owner/app:{{ Version }}".
193    pub name_template: String,
194    /// Image references to include in the manifest.
195    pub image_templates: Vec<String>,
196    /// Extra flags for `docker manifest create`.
197    pub create_flags: Option<Vec<String>>,
198    /// Extra flags for `docker manifest push`.
199    pub push_flags: Option<Vec<String>>,
200    /// Skip push: true, false, or "auto" (skip for prereleases).
201    #[schemars(schema_with = "skip_push_schema")]
202    pub skip_push: Option<SkipPushConfig>,
203    /// Unique identifier for this manifest config.
204    pub id: Option<String>,
205    /// Docker backend for manifest commands: `"docker"` (default) or
206    /// `"podman"`. The `"podman"` backend is **Linux-only** (per
207    /// Pro): configs on macOS or Windows fail at config-validation time with
208    /// a clear error rather than blowing up later when `podman` is not on
209    /// `PATH`.
210    #[serde(rename = "use")]
211    pub use_backend: Option<String>,
212    /// Retry configuration for manifest push (handles transient registry errors).
213    pub retry: Option<DockerRetryConfig>,
214}