build

package
v0.14.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package build is the engine behind `patchy build`. It builds the first-party artifacts listed in the patchy config's build: block (Dockerfile images and Flux manifest directories), scans them before anything is pushed, publishes them to every configured registry, and signs them with that registry's signing block. It is mirror's sibling for artifacts this project produces rather than vendors, and reuses these mirror packages: ocireg (pushes and existence probes), scan (archive scanners, the blocking policy, kubescape), allowlist (lint and regeneration), sign (the verify-then-sign Ensurer) and spec (registries, signing, the one scan block).

The flows: an image entry expands its tags and args (failing fast on an unset variable or an invalid tag), lints its allowlist (security/allowlists/<name>.yaml), builds an OCI layout with docker buildx, scans each platform image as a docker archive, and fails with nothing pushed if a blocking finding survives the allowlist. A manifests entry stages its filtered tree, sweeps it with kubescape, and packages it as a Flux OCI artifact (fluxartifact). Publishing never moves a tag: an absent tag is pushed, one already at this digest is skipped, and one at another digest is left alone with a warning. Dry runs build and scan but never contact a registry; UpdateAllowlist rebuilds and rescans to regenerate an allowlist without publishing anything.

The engine never discovers config files and never touches git: the CLI hands it a resolved config, and the calling pipeline owns commits.

Index

Constants

View Source
const (
	// ActionPromoted means the target tag was absent and now points at the
	// promoted build.
	ActionPromoted = "promoted"
	// ActionWouldPromote is the promote dry-run row: nothing was tagged.
	ActionWouldPromote = "would-promote"
)
View Source
const (
	// ActionPushed means the tag was absent and now points at the build.
	ActionPushed = "pushed"
	// ActionSkippedCurrent means the tag already points at this digest.
	ActionSkippedCurrent = "skipped-current"
	// ActionSkippedTagExists means the tag points at a different digest
	// and was left alone: tags never move.
	ActionSkippedTagExists = "skipped-tag-exists"
	// ActionWouldPush is the dry-run row: no registry was contacted.
	ActionWouldPush = "would-push"
)

Publish actions, one per (registry, tag) record.

View Source
const (
	// KindImage is a Dockerfile build published as an OCI image (index).
	KindImage = "image"
	// KindManifests is a Flux manifests directory published as a Flux OCI
	// artifact.
	KindManifests = "manifests"
)

The build entry kinds.

View Source
const AllowlistDir = "security/allowlists"

AllowlistDir is where image entries' allowlists live, relative to the working directory: <AllowlistDir>/<name>.yaml.

Variables

This section is empty.

Functions

func Platforms

func Platforms(archs []string) []string

Platforms normalizes an architectures list: a bare architecture means linux/<arch>; anything with a slash (os/arch[/variant]) is kept verbatim.

Types

type AllowlistResult

type AllowlistResult struct {
	Name string `json:"name"`
	// Path is the allowlist file, relative to the working directory.
	Path string `json:"path,omitempty"`
	// Generated is false when the entry was skipped (Note says why).
	Generated bool     `json:"generated"`
	Kept      int      `json:"kept"`
	Renewed   []string `json:"renewed,omitempty"`
	Added     int      `json:"added"`
	Dropped   []string `json:"dropped,omitempty"`
	// Note explains a skip.
	Note string `json:"note,omitempty"`
	// Err carries a per-entry failure in --all summaries.
	Err string `json:"error,omitempty"`
}

AllowlistResult reports one --update-allowlists regeneration.

type Builder

type Builder interface {
	Build(ctx context.Context, r buildx.Request) error
}

Builder builds one image into an OCI layout directory; buildx.Builder in production, a fake writing a layout in tests.

type Config

type Config struct {
	// Workdir is the directory relative entry paths and the allowlists
	// (security/allowlists/<name>.yaml) resolve against (default: the
	// process working directory).
	Workdir string
	// Global is the resolved registries, signing and scan blocks.
	Global *spec.Config
	// Spec is the validated build block.
	Spec *Spec
	// Registry performs OCI operations (nil: ambient keychain).
	Registry *ocireg.Client
	// Tools runs external binaries: docker, scanners, cosign (nil: the
	// real PATH).
	Tools scan.ToolRunner
	// ArchiveScanners overrides the image scanner roster (nil: built from
	// the scan config).
	ArchiveScanners []scan.ArchiveScanner
	// Verify checks one signature subject (nil: cosign).
	Verify func(context.Context, verify.Subject) error
	// NewSigner builds a signer for a resolved signing config (nil: the
	// real keyless/KMS signer).
	NewSigner func(*spec.Signing) (sign.ArtifactSigner, error)
	// Builder builds images (nil: docker buildx through Tools).
	Builder Builder
	// Now is the wall clock: the allowlist expiry horizon and the default
	// PATCHY_TIMESTAMP.
	Now func() time.Time
	// LookupEnv resolves ${VAR} references and SOURCE_DATE_EPOCH (nil:
	// os.LookupEnv).
	LookupEnv func(string) (string, bool)
	// OnEvent receives narration (nil discards it).
	OnEvent func(Event)
}

Config wires an Engine. Its injectable hooks match mirror.Config's, so the two engines are tested the same way.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine builds, scans, publishes and signs the configured entries.

func New

func New(cfg Config) (*Engine, error)

New builds an Engine, fixing the run's PATCHY_TIMESTAMP once.

func (*Engine) AllowlistPath

func (e *Engine) AllowlistPath(name string) string

AllowlistPath is an image entry's allowlist file.

func (*Engine) Build

func (e *Engine) Build(ctx context.Context, name string, opts Options) (*Result, error)

Build builds and scans one entry, then publishes and signs it if the scan passes. The result is returned even on failure, carrying what was learned (the scan report, the records pushed before an error).

func (*Engine) Entries

func (e *Engine) Entries() []Entry

Entries lists every configured entry.

func (*Engine) Entry

func (e *Engine) Entry(name string) (Entry, error)

Entry looks one entry up by name.

func (*Engine) Promote added in v0.12.1

func (e *Engine) Promote(ctx context.Context, entryName string, opts PromoteOptions) (*PromoteResult, error)

Promote adds a tag to an already-published build instead of rebuilding it: in every selected registry it picks the newest tag matching From, requires the build's digest to carry a valid signature under that registry's signing config, and adds Tag to the same digest. Tags still never move: a Tag already on that digest is skipped-current, one on another digest fails the entry. Nothing is signed: the build already is, and that signature is what was verified.

func (*Engine) SelectRegistries

func (e *Engine) SelectRegistries(names []string) ([]spec.Registry, error)

SelectRegistries resolves registry names (nil: all); the CLI validates --registry through it.

func (*Engine) UpdateAllowlist

func (e *Engine) UpdateAllowlist(ctx context.Context, name string) (*AllowlistResult, error)

UpdateAllowlist rebuilds and rescans an image entry and regenerates its security/allowlists/<name>.yaml from the blocking findings, for entries whose resolved scan.allowlist.generate is true. It never pushes or signs and never fails on findings: reviewing the allowlist diff in the PR is where the risk gets accepted.

type Entry

type Entry struct {
	Name       string
	Kind       string
	Repository string
	// Tags are the unexpanded tag templates (entry over build default).
	Tags []string
	// Scan is the entry's own scan block.
	Scan spec.ScanOverride
	// Image is set for KindImage, Manifests for KindManifests.
	Image     *Image
	Manifests *Manifests
	// Platforms is the normalized platform list (images only).
	Platforms []string
}

Entry is one build entry of either kind, resolved against the build-level defaults.

type Event

type Event struct {
	// Kind is "note" or "warn".
	Kind  string `json:"kind"`
	Entry string `json:"entry,omitempty"`
	Stage string `json:"stage,omitempty"`
	// Message is the human-readable line.
	Message string `json:"message"`
}

Event narrates engine progress for stderr; results are the data.

type Image

type Image struct {
	// Name is the entry's identity: CLI selection, results, and the
	// allowlist file security/allowlists/<name>.yaml. Unique across images
	// and manifests.
	Name string `yaml:"name"`
	// Repository is the path published under every registry URL:
	// <registry.url>/<repository>:<tag>.
	Repository string `yaml:"repository"`
	// Dockerfile is relative to the working directory (default
	// <context>/Dockerfile).
	Dockerfile string `yaml:"dockerfile"`
	// Context is the build context, relative to the working directory
	// (default ".").
	Context string `yaml:"context"`
	// Target optionally names the build stage.
	Target string `yaml:"target"`
	// Architectures overrides the build-level default.
	Architectures []string `yaml:"architectures"`
	// Args are build args; values expand ${ENV}, keys keep their case.
	Args map[string]string `yaml:"args"`
	// Secrets are BuildKit secret mounts (RUN --mount=type=secret,id=…).
	// Each names where the value lives (an environment variable or a
	// file), so the value never reaches the config, the argv or the image.
	Secrets []Secret `yaml:"secrets"`
	// Tags overrides the build-level default list.
	Tags []string `yaml:"tags"`
	// Scan overrides the config's scan block for this image.
	Scan spec.ScanOverride `yaml:"scan"`
}

Image is one Dockerfile build.

func (*Image) ContextDir

func (i *Image) ContextDir() string

ContextDir resolves an image's build context (default ".").

func (*Image) DockerfilePath

func (i *Image) DockerfilePath() string

DockerfilePath resolves an image's Dockerfile (default <context>/Dockerfile).

type Manifests

type Manifests struct {
	// Name is the entry's identity, unique across images and manifests.
	Name string `yaml:"name"`
	// Repository is the path published under every registry URL.
	Repository string `yaml:"repository"`
	// Path is the directory packaged, relative to the working directory.
	Path string `yaml:"path"`
	// Ignore lists gitignore-syntax patterns excluded from the artifact
	// (.git/ is always excluded).
	Ignore []string `yaml:"ignore"`
	// Source expands into the org.opencontainers.image.source annotation.
	Source string `yaml:"source"`
	// Revision expands into the org.opencontainers.image.revision
	// annotation (Flux's <ref>@sha1:<sha> convention).
	Revision string `yaml:"revision"`
	// Tags overrides the build-level default list.
	Tags []string `yaml:"tags"`
	// Scan sets only enabled here: a manifests entry gets the kubescape
	// configuration scan, and the image-scan fields are rejected.
	Scan spec.ScanOverride `yaml:"scan"`
}

Manifests is one Flux manifests directory, published as-is.

type Options

type Options struct {
	// DryRun builds and scans, then reports would-push rows without
	// contacting any registry.
	DryRun bool
	// Registries restricts publishing to the named registries (nil: all).
	Registries []string
	// Tags replaces the configured tag templates for this run (still
	// expanded).
	Tags []string
}

Options tunes one Build call.

type PromoteOptions added in v0.12.1

type PromoteOptions struct {
	// From is a glob over the repository's tags (path.Match syntax, after
	// ${ENV} expansion) selecting the builds a promotion may start from,
	// e.g. "${VERSION}-edge.*". The newest match, in semver order, wins.
	From string
	// Tag is the tag to add to that build (after ${ENV} expansion).
	Tag string
	// Registries restricts promotion to the named registries (nil: all).
	Registries []string
	// DryRun resolves and verifies the build to promote, but tags nothing.
	DryRun bool
}

PromoteOptions tunes one Promote call.

type PromoteRecord added in v0.12.1

type PromoteRecord struct {
	// Registry is the configured registry name.
	Registry string `json:"registry"`
	// Source is the build promoted, <registry.url>/<repository>:<tag>.
	Source string `json:"source"`
	// Ref is the promoted tag, <registry.url>/<repository>:<tag>.
	Ref string `json:"ref"`
	// Digest is the build's manifest (or index) digest.
	Digest string `json:"digest"`
	// Revision is the build's org.opencontainers.image.revision annotation,
	// when it carries one (every manifests artifact does): what the build
	// was made from, for a caller to check against what it promotes.
	Revision string `json:"revision,omitempty"`
	// Action is promoted, skipped-current or would-promote.
	Action string `json:"action"`
}

PromoteRecord is one registry's promotion of one entry.

type PromoteResult added in v0.12.1

type PromoteResult struct {
	Name    string          `json:"name"`
	Kind    string          `json:"kind"`
	Records []PromoteRecord `json:"records,omitempty"`
	// Err carries a per-entry failure in --all summaries.
	Err string `json:"error,omitempty"`
}

PromoteResult is the outcome of promoting one entry.

type Record

type Record struct {
	// Registry is the configured registry name.
	Registry string `json:"registry"`
	// Ref is <registry.url>/<repository>:<tag>.
	Ref string `json:"ref"`
	// Digest is what the tag points at after the run (for
	// skipped-tag-exists, the other digest it already held).
	Digest string `json:"digest,omitempty"`
	// Action is pushed, skipped-current, skipped-tag-exists or would-push.
	Action string `json:"action"`
	// Signed reports a signature created this run for the digest.
	Signed bool `json:"signed"`
}

Record is one published tag's outcome at one registry.

type Result

type Result struct {
	Name string `json:"name"`
	Kind string `json:"kind"`
	// Digest is the built manifest (or index) digest.
	Digest string `json:"digest,omitempty"`
	// Tags are the expanded tags.
	Tags    []string    `json:"tags,omitempty"`
	Scan    *ScanReport `json:"scan,omitempty"`
	Records []Record    `json:"records,omitempty"`
	// Err carries a per-entry failure in --all summaries.
	Err string `json:"error,omitempty"`
}

Result is one entry's build outcome.

type ScanReport

type ScanReport struct {
	// Skipped names why the vulnerability scan did not run, if it did not.
	Skipped string `json:"skipped,omitempty"`
	// Scanned lists the platforms scanned (images).
	Scanned []string `json:"scanned,omitempty"`
	// Blocking are blocking-severity findings the allowlist did not
	// suppress; any entry here fails the build before anything is pushed.
	Blocking []scan.Finding `json:"blocking,omitempty"`
	// Suppressed counts findings the allowlist accepted.
	Suppressed int `json:"suppressed"`
	// Expired lists allowlist IDs whose expiry has lapsed; they no longer
	// suppress anything.
	Expired []string `json:"expired,omitempty"`
	// ConfigScan reports the kubescape sweep (manifests): "clean",
	// "failed", or "skipped: <reason>".
	ConfigScan string `json:"configScan,omitempty"`
	// ConfigScanFailed blocks under kubescape fail mode.
	ConfigScanFailed bool `json:"configScanFailed,omitempty"`
}

ScanReport is one entry's pre-push scan outcome.

func (*ScanReport) Failed

func (r *ScanReport) Failed() bool

Failed reports whether the scan gate blocks the publish.

type Secret added in v0.12.2

type Secret struct {
	// ID is what the Dockerfile mounts: RUN --mount=type=secret,id=<id>.
	// Unique within the image.
	ID string `yaml:"id"`
	// Env names the environment variable holding the value; docker reads
	// it from patchy's environment. A name, never expanded.
	Env string `yaml:"env"`
	// Src is the file holding the value, relative to the working
	// directory; it expands ${ENV} at build time.
	Src string `yaml:"src"`
	// Optional drops the secret (with a note) when its variable is unset
	// or its file is missing, instead of failing the build. Use it for
	// Dockerfiles that mount it with required=false.
	Optional bool `yaml:"optional"`
}

Secret is one BuildKit secret an image build mounts. Exactly one of Env or Src is set.

type Spec

type Spec struct {
	// Architectures is the default platform list for image entries
	// (amd64 means linux/amd64; os/arch[/variant] is kept verbatim).
	Architectures []string `yaml:"architectures"`
	// Tags is the default tag template list; an entry's own list replaces
	// it. Templates expand ${ENV} references at build time.
	Tags      []string    `yaml:"tags"`
	Images    []Image     `yaml:"images"`
	Manifests []Manifests `yaml:"manifests"`
	// Policy restricts the registries image builds may pull from. The CLI
	// resolves it (with every enforced layer's) into spec.Config.Policy,
	// which is what the engine enforces; this field is only its YAML home.
	Policy *spec.BuildPolicy `yaml:"policy"`
}

Spec is the patchy config's build: block: the first-party artifacts patchy build produces, scans, publishes and signs.

func (*Spec) Entries

func (s *Spec) Entries() []Entry

Entries lists every entry, images first, in declaration order.

func (*Spec) Entry

func (s *Spec) Entry(name string) (Entry, bool)

Entry looks one entry up by name.

func (*Spec) Validate

func (s *Spec) Validate(global spec.Scan) error

Validate checks the build block against the config's scan defaults: names (well-formed, unique across both kinds), repositories, platforms, manifests paths, and each entry's scan block (including the resolved allowlist horizon for images, and the image-only fields a manifests entry may not set).

Directories

Path Synopsis
Package buildx drives `docker buildx build` for patchy build.
Package buildx drives `docker buildx build` for patchy build.
Package dockerfile lists the images a Dockerfile build pulls (FROM, COPY --from, RUN --mount from= and the frontend, set by # syntax= or BUILDKIT_SYNTAX), expanded the way BuildKit expands them.
Package dockerfile lists the images a Dockerfile build pulls (FROM, COPY --from, RUN --mount from= and the frontend, set by # syntax= or BUILDKIT_SYNTAX), expanded the way BuildKit expands them.
Package expand substitutes environment references into build tag, arg and annotation templates.
Package expand substitutes environment references into build tag, arg and annotation templates.
Package fluxartifact packages a directory of Flux manifests as a Flux OCI artifact, the shape `flux push artifact` produces and an OCIRepository source consumes: an OCI image manifest whose config is application/vnd.cncf.flux.config.v1+json ({}) and whose single layer is the tree as application/vnd.cncf.flux.content.v1.tar+gzip, annotated with the source URL and revision.
Package fluxartifact packages a directory of Flux manifests as a Flux OCI artifact, the shape `flux push artifact` produces and an OCIRepository source consumes: an OCI image manifest whose config is application/vnd.cncf.flux.config.v1+json ({}) and whose single layer is the tree as application/vnd.cncf.flux.content.v1.tar+gzip, annotated with the source URL and revision.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL