deploy

package
v0.124.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultReference = "refs/heads/main"

Variables

View Source
var (
	DefaultDeploymentConfigFileNames = []string{".doco-cd.yaml", ".doco-cd.yml"}
	CustomDeploymentConfigFileNames  = []string{".doco-cd.%s.yaml", ".doco-cd.%s.yml"}
	ErrConfigFileNotFound            = errors.New("configuration file not found in repository")
	ErrDuplicateProjectName          = errors.New("duplicate project/stack name found in configuration file")
	ErrInvalidConfig                 = errors.New("invalid deploy configuration")
	ErrKeyNotFound                   = errors.New("key not found")
	ErrInvalidFilePath               = errors.New("invalid file path")
	ErrMultipleYAMLDocuments         = errors.New("nested .doco-cd configuration file must contain only a single YAML document")
	ErrSwarmModeUnavailable          = errors.New("docker swarm mode is not available for this deployment")
)

Functions

func LoadExternalSecretsFiles added in v0.119.0

func LoadExternalSecretsFiles(config *Config, basePath string) error

LoadExternalSecretsFiles processes local ExternalSecretsFiles entries and accumulates their contents into Config.Internal.ExternalSecretsFromFiles. Each file is a YAML document holding a map of env var name to external secret reference, using the same shape as ExternalSecrets. Files prefixed with "remote:" are left in Config.ExternalSecretsFiles (without the prefix) for later processing once a remote repository has been checked out, mirroring LoadLocalDotEnv. Call MergeExternalSecretsFromFiles once all local and remote files have been loaded.

func LoadLocalDotEnv

func LoadLocalDotEnv(config *Config, basePath string) error

LoadLocalDotEnv processes local dotenv files and loads their variables into the Config.Internal Environment map. Remote dotenv files (prefixed with "remote:") are collected and left in Config.EnvFiles for later processing.

Env files are parsed with compose-go's lookup-aware parser so that variable references (e.g. VAR=${VAR}) resolve against values already accumulated from previously processed files, matching the cascading resolution behavior compose-go itself uses when loading multiple --env-file entries. Without this, a later, more specific env file using a self-referencing placeholder (common in gitops setups that declare an env var per-directory to be filled in from a broader-scope .env file) would resolve to an empty string and silently overwrite a correctly resolved value from an earlier file.

func MergeExternalSecretsFromFiles added in v0.119.0

func MergeExternalSecretsFromFiles(config *Config)

MergeExternalSecretsFromFiles merges secrets accumulated by LoadExternalSecretsFiles into Config.ExternalSecrets. Inline ExternalSecrets entries take precedence over file-sourced ones on key collision.

func SetAutoDiscoveryCacheObserver added in v0.89.0

func SetAutoDiscoveryCacheObserver(observer func(repository, result string))

SetAutoDiscoveryCacheObserver configures an observer for auto-discovery cache lookups. The callback receives repository and result labels (for example: hit/miss).

func ValidateUniqueProjectNames

func ValidateUniqueProjectNames(configs []*Config) error

ValidateUniqueProjectNames checks if project names are unique within each Docker context.

Types

type AutoDiscoveryConfig

type AutoDiscoveryConfig struct {
	ScanDepth     int  `yaml:"depth" json:"depth" default:"0"`                       // ScanDepth is the maximum depth of subdirectories to scan for docker-compose files
	Enabled       bool `yaml:"enabled" json:"enabled" default:"false"`               // Enabled enables autodiscovery of services to deploy in the working directory
	Delete        bool `yaml:"delete" json:"delete" default:"false"`                 // Delete removes obsolete auto-discovered deployments that are no longer present in the repository
	RemoveVolumes bool `yaml:"remove_volumes" json:"remove_volumes" default:"false"` // RemoveVolumes removes the volumes of an auto-discovered deployment when it is deleted
	RemoveImages  bool `yaml:"remove_images" json:"remove_images" default:"true"`    // RemoveImages removes the images of an auto-discovered deployment when it is deleted
}

AutoDiscoveryConfig holds auto-discovery settings for a deployment.

func (*AutoDiscoveryConfig) UnmarshalJSON

func (c *AutoDiscoveryConfig) UnmarshalJSON(data []byte) error

func (*AutoDiscoveryConfig) UnmarshalYAML

func (c *AutoDiscoveryConfig) UnmarshalYAML(node *yaml.Node) error

type AutoDiscoveryOrigin added in v0.124.0

type AutoDiscoveryOrigin struct {
	// WorkingDirectory is the directory the auto-discovery config scans, relative to the source root.
	WorkingDirectory string
	// Reference is the reference the auto-discovery config scans.
	Reference string
	// RepositoryURL is the repository the auto-discovery config scans. Empty means the job's own source.
	RepositoryURL string
	// WebhookEventFilter is the webhook event filter of the auto-discovery config. Nested configs can
	// override the filter of a discovered deployment, so it is kept separately.
	WebhookEventFilter string
	// Revision is the Git revision the deployment was discovered in. Empty if unknown.
	Revision string
	// MirrorDir is the Git mirror Revision was resolved in. Empty if Revision is.
	MirrorDir string
	// Settings are the auto-discovery settings of the config.
	Settings AutoDiscoveryConfig
}

AutoDiscoveryOrigin describes the auto-discovery config that discovered a deployment config.

func (*AutoDiscoveryOrigin) Owns added in v0.124.0

func (o *AutoDiscoveryOrigin) Owns(relDir string) bool

Owns reports whether relDir, a stack's working directory relative to the source root, lies within the directories o scans.

type BuildConfig

type BuildConfig struct {
	Args           map[string]string `yaml:"args" json:"args"`                                         // BuildArgs is a map of build-time arguments to pass to the build process
	ForceImagePull bool              `yaml:"force_image_pull" json:"force_image_pull" default:"false"` // ForceImagePull always attempt to pull a newer version of the image
	Quiet          bool              `yaml:"quiet" json:"quiet" default:"false"`                       // Quiet suppresses the build output
	NoCache        bool              `yaml:"no_cache" json:"no_cache" default:"false"`                 // NoCache disables the use of the cache when building images
}

BuildConfig holds build options for a deployment.

type Config

type Config struct {
	Name               string            `yaml:"name" json:"name" doco:"allowOverride"`                                // Name of the docker-compose deployment / stack
	Source             config.SourceType `yaml:"source" json:"source" default:"git"`                                   // Source selects the deployment source backend (git or oci)
	Version            string            `yaml:"version" json:"version" default:"doco.v1" doco:"allowOverride"`        // Version declares the deployment config schema/artifact version for OCI-backed deployments
	RepositoryUrl      config.GitUrl     `yaml:"repository_url" json:"repository_url" default:"" validate:"gitUrl"`    // RepositoryUrl is the Git clone URL of the repository to deploy
	WebhookEventFilter string            `yaml:"webhook_filter" json:"webhook_filter" default:"" doco:"allowOverride"` // WebhookEventFilter is a regular expression to whitelist deployment triggers based on the webhook event payload (e.g., branch like "^refs/heads/main$" or "main", tag like "^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$" or "v[0-9]+\.[0-9]+\.[0-9]+")
	Reference          string            `yaml:"reference" json:"reference" default:""`                                // Reference is the Git reference to the deployment, e.g., refs/heads/main, main, refs/tags/v1.0.0 or v1.0.0
	WorkingDirectory   string            `yaml:"working_dir" json:"working_dir" default:"." doco:"allowOverride"`      // WorkingDirectory is the working directory for the deployment
	Context            string            `yaml:"context" json:"context" default:"" doco:"allowOverride"`               // Context is the Docker context to use for this deployment (empty uses the default Docker context)
	ComposeFiles       []string          ``                                                                            // ComposeFiles is the list of docker-compose files to use
	/* 157-byte string literal not displayed */
	Environment          map[string]string                        `yaml:"environment" json:"environment" doco:"allowOverride"`                                    // Environment is a map of environment variables to use for variable interpolation in the compose files
	EnvFiles             []string                                 `yaml:"env_files" json:"env_files" default:"[\".env\"]" doco:"allowOverride"`                   // EnvFiles is the list of dotenv files to use for variable interpolation
	RemoveOrphans        bool                                     `yaml:"remove_orphans" json:"remove_orphans" default:"true" doco:"allowOverride"`               // RemoveOrphans removes containers for services not defined in the Compose file
	PruneImages          bool                                     `yaml:"prune_images" json:"prune_images" default:"true" doco:"allowOverride"`                   // PruneImages removes images that are no longer used by any service
	Swarm                SwarmConfig                              `yaml:"swarm" json:"swarm" doco:"allowOverride"`                                                // Swarm contains Docker Swarm-specific deployment settings
	WaitRunningJobs      bool                                     `yaml:"wait_running_jobs" json:"wait_running_jobs" default:"true" doco:"allowOverride"`         // WaitRunningJobs waits for currently running scheduled job containers/services to finish before deployment
	ForceRecreate        bool                                     `yaml:"force_recreate" json:"force_recreate" default:"false" doco:"allowOverride"`              // ForceRecreate forces the recreation/redeployment of containers even if the configuration has not changed
	ForceImagePull       bool                                     `yaml:"force_image_pull" json:"force_image_pull" default:"false" doco:"allowOverride"`          // ForceImagePull always pulls the latest version of the image tags you've specified if a newer version is available
	Timeout              int                                      `yaml:"timeout" json:"timeout" default:"180" doco:"allowOverride"`                              // Timeout is the time in seconds to wait for the deployment to finish before timing out
	BuildOpts            BuildConfig                              `yaml:"build" json:"build" doco:"allowOverride"`                                                // BuildOpts is the build options for the deployment
	GitDepth             int                                      `yaml:"git_depth" json:"git_depth" default:"0"`                                                 // GitDepth limits the number of commits to fetch. 0 means use global GIT_CLONE_DEPTH. A positive value overrides the global setting.
	Destroy              DestroyConfig                            `yaml:"destroy" json:"destroy" doco:"allowOverride"`                                            // Destroy configures destruction of the deployment and related resources
	Profiles             []string                                 `yaml:"profiles" json:"profiles" default:"[]" doco:"allowOverride"`                             // Profiles is a list of profiles to use for the deployment, e.g., ["dev", "prod"]. See https://docs.docker.com/compose/how-tos/profiles/
	ExternalSecrets      map[string]secrettypes.ExternalSecretRef `yaml:"external_secrets" json:"external_secrets" doco:"allowOverride"`                          // ExternalSecrets maps env vars to legacy string references or structured references (e.g. webhook store_ref/remote_ref).
	ExternalSecretsFiles []string                                 `yaml:"external_secrets_files" json:"external_secrets_files" default:"[]" doco:"allowOverride"` // ExternalSecretsFiles is a list of YAML files, each holding a map of env var name to external secret reference (same shape as ExternalSecrets). Files are merged first, then ExternalSecrets entries are applied on top (inline wins on key collision).
	AutoDiscovery        AutoDiscoveryConfig                      `yaml:"auto_discovery" json:"auto_discovery"`                                                   // AutoDiscovery configures autodiscovery of services to deploy in the working directory
	Reconciliation       ReconciliationConfig                     `yaml:"reconciliation" json:"reconciliation" doco:"allowOverride"`                              // Reconciliation is the configuration for the reconciliation feature
	Oci                  config.OciTrustPolicyOverride            `yaml:"oci" json:"oci" doco:"allowOverride"`                                                    // Oci allows per-target overrides for OCI signature verification policy
	Internal             struct {
		File                          string                                   `yaml:"-"` // File is the path to the deployment configuration file
		ConfigTarget                  string                                   `yaml:"-"` // ConfigTarget is the target suffix from the deployment config filename (e.g., "nas" for .doco-cd.nas.yml)
		Environment                   map[string]string                        // Environment stores environment variables for variable interpolation in the compose project
		ExternalSecretsFromFiles      map[string]secrettypes.ExternalSecretRef // ExternalSecretsFromFiles accumulates entries loaded from ExternalSecretsFiles across local and remote loads, merged into ExternalSecrets by MergeExternalSecretsFromFiles
		Hash                          string                                   `yaml:"-"`          // Hash is a hash of the Config struct
		OciTrustPolicyOverrideTrusted bool                                     `yaml:"-" json:"-"` // true only for trusted config sources (e.g. POLL_CONFIG inline deployments)
		ConfigSourceFilesLoaded       bool                                     `yaml:"-" json:"-"` // Config-source env and external-secret files have been loaded
		ConfigSourceRevision          string                                   `yaml:"-" json:"-"` // Immutable revision containing this deployment config
		ConfigSourceWorkingDir        string                                   `yaml:"-" json:"-"` // Host path to the config source artifact
		AutoDiscoveryOrigin           *AutoDiscoveryOrigin                     `yaml:"-" json:"-"` // Auto-discovery config that discovered this config, nil if it was not discovered
	} `json:"-"` // Internal holds internal configuration values that are not set by the user
}

Config is the structure of the deployment configuration file.

func GetConfigFromYAML

func GetConfigFromYAML(f string, applyDefaults bool) ([]*Config, error)

GetConfigFromYAML reads a YAML file and unmarshals it into a slice of Config structs. When applyDefaults is true, default values are applied to each config (normal usage). When applyDefaults is false, omitted fields remain zero/nil for nested auto-discovery. overrides so unset fields do not accidentally replace base/discovered values during merge.

func GetConfigs

func GetConfigs(ctx context.Context, repoRoot, configBaseDir, customTarget, reference, gitMirrorRoot, primaryRevision string, gitOpts *GitOptions) ([]*Config, error)

GetConfigs returns deployment configurations discovered in repoRoot. gitMirrorRoot and primaryRevision identify the Git source when repoRoot is a published artifact. primaryRevision must be resolved from reference, or from gitOpts.PrimaryReference when the config source and deployment references differ.

func New

func New(name, reference string) *Config

New creates a Config with default values.

func ResolveConfigs

func ResolveConfigs(ctx context.Context, inlineDeployments []*Config, customTarget, reference, repoRoot, configBaseDir, gitMirrorRoot, primaryRevision string, gitOpts *GitOptions) ([]*Config, error)

ResolveConfigs returns inline deployment configs when supplied; otherwise it reads the repository config files. repoRoot is the absolute repository path, and configBaseDir is the relative config path. gitOpts is optional and only required for AutoDiscovery with a remote RepositoryUrl. gitMirrorRoot and primaryRevision are optional Git-only parameters; see GetConfigs.

func (*Config) Hash

func (c *Config) Hash() (string, error)

Hash returns a hash of the Config struct (without changing the order of its elements).

func (*Config) LogValue

func (c *Config) LogValue() slog.Value

LogValue implements the slog.LogValuer interface for Config.

func (*Config) ResolveGitDepth

func (c *Config) ResolveGitDepth(globalDepth int) int

ResolveGitDepth returns the effective git clone depth. If the deploy-level GitDepth is > 0, it overrides the global value. Otherwise, the global depth is used. 0 means full clone (no limit).

func (*Config) ResolveSwarmConfigRetention added in v0.107.0

func (c *Config) ResolveSwarmConfigRetention(globalRetention int) int

ResolveSwarmConfigRetention returns the effective old-revision retention for Swarm configs. If deploy-level value is set, it overrides the global value. If deploy-level value is unset, the global value is used.

func (*Config) ResolveSwarmMode added in v0.114.0

func (c *Config) ResolveSwarmMode(swarmAvailable bool) (bool, error)

ResolveSwarmMode returns the deployment mode selected by this config. When swarm.enabled is omitted, the mode detected for the Docker context is used. An explicit Swarm request must not silently fall back to Compose.

func (*Config) ResolveSwarmSecretRetention added in v0.107.0

func (c *Config) ResolveSwarmSecretRetention(globalRetention int) int

ResolveSwarmSecretRetention returns the effective old-revision retention for Swarm secrets. If deploy-level value is set, it overrides the global value. If deploy-level value is unset, the global value is used.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(unmarshal func(any) error) error

func (*Config) Validate

func (c *Config) Validate() error

Validate checks the Config for required fields and valid values.

type DestroyConfig

type DestroyConfig struct {
	Enabled       bool `yaml:"enabled" json:"enabled" default:"false"`              // Enabled removes the deployment and all its resources from the Docker host
	RemoveVolumes bool `yaml:"remove_volumes" json:"remove_volumes" default:"true"` // RemoveVolumes removes the volumes used by the deployment (always enabled in docker swarm mode)
	RemoveImages  bool `yaml:"remove_images" json:"remove_images" default:"true"`   // RemoveImages removes the images used by the deployment (currently not supported in docker swarm mode)
	RemoveRepoDir bool `yaml:"remove_dir" json:"remove_dir" default:"false"`        // Deprecated: RemoveRepoDir is ignored because the source directory is shared by all stacks of a repository
}

DestroyConfig holds options for destroying a deployment.

func (*DestroyConfig) UnmarshalJSON

func (c *DestroyConfig) UnmarshalJSON(data []byte) error

func (*DestroyConfig) UnmarshalYAML

func (c *DestroyConfig) UnmarshalYAML(node *yaml.Node) error

type DiscoverySnapshot added in v0.124.0

type DiscoverySnapshot struct {
	RepositoryRoot string
	MirrorDir      string
	Revision       string
	Reference      string
}

DiscoverySnapshot identifies the materialized source of an existing deployment. RepositoryRoot can differ from the artifact containing its top-level deploy config. MirrorDir and a Git commit Revision enable Git-backed discovery; other snapshots are scanned from disk.

type GitOptions

type GitOptions struct {
	SSHPrivateKey           string
	SSHPrivateKeyPassphrase string
	GitAccessToken          string
	SkipTLSVerification     bool
	HttpProxy               transport.ProxyOptions
	GitCloneSubmodules      bool
	GitCloneDepth           int
	SourceURL               string
	// PrimaryReference overrides the reference known to resolve to primaryRevision.
	// Empty uses the job's reference. A config-source artifact's commit hash
	// identifies its content when the branch that supplied it is unknown.
	PrimaryReference string
	// DiscoverySnapshot restores only deployments discovered in the recorded source and reference.
	// When set, discovery reads this snapshot without resolving or fetching any references.
	DiscoverySnapshot *DiscoverySnapshot
	// SourceBaseDir is the directory every source's store lives under (DATA_MOUNT_PATH).
	// Auto-discovery of a config that names its own repository_url needs it to place that repository's store beside the
	// others, since repoRoot is a published artifact directory and nothing about that path leads back to the shared root.
	SourceBaseDir string
}

GitOptions holds git-related options for operations that require cloning or fetching remote repositories.

type ReconciliationConfig

type ReconciliationConfig struct {
	Enabled        bool     `yaml:"enabled" json:"enabled" default:"true"`               // Enabled enables the reconciliation feature
	Events         []string `yaml:"events" json:"events" default:"[\"unhealthy\"]"`      // Events is the list of Docker container actions that trigger reconciliation
	RestartTimeout int      `yaml:"restart_timeout" json:"restart_timeout" default:"10"` // RestartTimeout is the timeout in seconds to wait before killing a container during a restart
	RestartSignal  string   `yaml:"restart_signal" json:"restart_signal" default:""`     // RestartSignal is the signal sent to stop containers during a restart. If not set, the default of the Docker daemon is used (SIGTERM).
	RestartLimit   int      `yaml:"restart_limit" json:"restart_limit" default:"5"`      // RestartLimit suppresses further unhealthy-triggered restarts after this many restarts in the configured window. Set to 0 to disable suppression.
	RestartWindow  int      `yaml:"restart_window" json:"restart_window" default:"300"`  // RestartWindow is the time window in seconds used with RestartLimit.
}

ReconciliationConfig holds settings for the reconciliation feature.

func (*ReconciliationConfig) UnmarshalJSON

func (c *ReconciliationConfig) UnmarshalJSON(data []byte) error

func (*ReconciliationConfig) UnmarshalYAML

func (c *ReconciliationConfig) UnmarshalYAML(node *yaml.Node) error

type SwarmConfig added in v0.107.0

type SwarmConfig struct {
	Enabled         *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`                   // Enabled explicitly selects Swarm deployment mode. When unset, the Docker context determines the mode.
	ConfigRetention *int  `yaml:"config_retention,omitempty" json:"config_retention,omitempty"` // ConfigRetention is the number of old Swarm config revisions to keep per resource (excluding the active one). -1 disables automatic pruning. If unset, global DOCKER_SWARM_CONFIG_RETENTION is used.
	SecretRetention *int  `yaml:"secret_retention,omitempty" json:"secret_retention,omitempty"` // SecretRetention is the number of old Swarm secret revisions to keep per resource (excluding the active one). -1 disables automatic pruning. If unset, global DOCKER_SWARM_SECRET_RETENTION is used.
}

SwarmConfig contains Docker Swarm-specific deployment settings.

Jump to

Keyboard shortcuts

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