Documentation
¶
Overview ¶
Package cisink reads the CI export-sink and variable context of the current environment: which CI provider the process runs under, and where that provider expects variable/output exports to be written.
It is the READER half of the CI-export pattern. Its sibling cisink-write-go is the WRITER half — it consumes a Sink from here to emit variable-set/output lines in the right dialect. Splitting reader from writer keeps this package pure (no file mutation, only environment inspection) and lets a tool that merely needs to know "am I in CI, and which one?" depend on the small reader without pulling the emitters.
The model is borealis-style (BOREALIS.md §3.5): construction is New(required…, opts…); a config-consuming constructor is FromConfig(cfg). Errors are typed and code-carrying via errors-go. The package is pure stdlib plus errors-go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ForceProvider, when non-empty, pins the provider instead of detecting it
// from the environment, equivalent to [WithForcedProvider]. Valid values
// are the [Provider] string constants ("github"/"gitlab"/"bitbucket"/
// "buildkite"). An unrecognised value surfaces as a codeUnsupported error
// at [Reader.Read] time, not here.
ForceProvider string `yaml:"force_provider"`
}
Config is the typed, shikumi-loadable knob surface for a Reader. Every field is optional: an empty Config yields the same default os-backed reader as New() with no options. It exists so a tool that already loads a config file can drive detection declaratively rather than imperatively.
type Context ¶
type Context struct {
// Provider is the detected (or forced) CI system, ProviderUnknown locally.
Provider Provider
// Sink is where exports must be written for Provider.
Sink Sink
// Variables is the variable namespace: the CI-injected environment as a
// map, captured at read time. It is a copy, so mutating it does not touch
// os.Environ. A reader populates it from os.Environ (or the configured
// lookup) so callers read CI variables uniformly rather than scattering
// os.Getenv calls.
Variables map[string]string
}
Context is the full read CI context: the detected provider, its export sink, and the variable namespace (the CI-injected environment as seen by this process). It is what a tool inspects to decide how to behave under CI.
func Read ¶
Read is the package-level convenience that constructs a default os-backed Reader and reads once. It is the one-liner most tools want:
ctx, err := cisink.Read()
func (Context) InCI ¶
InCI reports whether a real CI provider was detected (Provider is not ProviderUnknown). It is the canonical "am I running under CI?" predicate.
func (Context) Lookup ¶
Lookup returns the value of a CI variable and whether it was present, reading from the captured Context.Variables namespace rather than the live process environment, so reads are deterministic against the snapshot taken at Read.
type Option ¶
type Option func(*Reader)
Option configures a Reader during New / [NewFromEnv]. Options are applied in order, so a later option of the same kind overrides an earlier one. This is the borealis-style functional-options constructor shape (§3.5).
func WithEnviron ¶
WithEnviron overrides the full-environment snapshot source used to populate Context.Variables. The default is os.Environ. Supplying a fixed slice (of "KEY=VALUE" strings) makes variable capture hermetic and testable.
func WithForcedProvider ¶
WithForcedProvider pins the provider instead of detecting it from the environment. Use it when a tool already knows its CI context (or a test wants a fixed dialect). A forced provider whose required sink env vars are absent surfaces a codeMissingSink error from Reader.Read.
func WithLookup ¶
WithLookup overrides the environment lookup used for detection and variable capture. The default is os.LookupEnv. Supplying a map-backed lookup makes a Reader fully hermetic and table-testable without touching the real environment.
type Provider ¶
type Provider string
Provider identifies a continuous-integration system. It is the discriminator the writer dialect keys off: each provider exposes variables and step outputs through a different mechanism (an env-file, a magic stdout line, a CLI), and the Sink read here records the concrete path/handle for the active one.
const ( // ProviderUnknown is the value when no recognised CI environment is // detected — typically a developer's local machine. A reader returns it // without error; whether that is acceptable is the caller's policy // (see [Context.InCI]). ProviderUnknown Provider = "unknown" // ProviderGitHub is GitHub Actions. Variables and outputs are exported by // appending lines to the files named by $GITHUB_ENV and $GITHUB_OUTPUT. ProviderGitHub Provider = "github" // ProviderGitLab is GitLab CI. There is no per-step env-append file; the // canonical export channel is dotenv artifacts written to a job-local file // and declared via artifacts:reports:dotenv. ProviderGitLab Provider = "gitlab" // ProviderBitbucket is Bitbucket Pipelines. Variables are exported by // appending shell `export` lines to $BITBUCKET_PIPE_STORAGE_DIR or, more // portably, written to stdout as `export NAME=value`. ProviderBitbucket Provider = "bitbucket" // ProviderBuildkite is Buildkite. Metadata is exported via the // `buildkite-agent meta-data set` CLI; for env there is no append-file, so // the writer emits shell export lines on stdout. ProviderBuildkite Provider = "buildkite" )
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads the CI export-sink and variable context. Construct it with New (default os-backed) or [NewFromEnv]; read with Reader.Read. A Reader is stateless across reads and safe to reuse.
func FromConfig ¶
FromConfig constructs a Reader from an already-loaded Config (§3.5). It does NOT call shikumi.Load — the caller loads cfg once at main and hands this the sub-struct. Additional options compose after the config-derived ones, so a test can still inject a hermetic lookup over a config-driven reader.
func New ¶
New constructs a Reader with the given options. With no options it reads the real process environment via os.LookupEnv / os.Environ. This is the canonical borealis Construct shape: New(required…, opts…) — there are no required positional args here, so it is New(opts…).
func (*Reader) Read ¶
Read inspects the environment and returns the full CI Context: the detected (or forced) provider, its export Sink, and the captured variable namespace.
Detection is conservative: a provider is reported only when its canonical marker variable is present (GitHub: $GITHUB_ACTIONS; GitLab: $GITLAB_CI; Bitbucket: $BITBUCKET_BUILD_NUMBER; Buildkite: $BUILDKITE). Absent any marker the provider is ProviderUnknown and Read returns no error — being outside CI is not a failure. A forced provider (via WithForcedProvider) skips detection but still resolves the sink, and a forced provider whose required sink path env var is missing returns a codeMissingSink error.
type Sink ¶
type Sink struct {
// Provider is the detected (or forced) CI system. The writer dialect is
// selected from this field.
Provider Provider
// EnvFile is the path whose appended lines become environment variables for
// later steps, or "" when the provider has no such file.
EnvFile string
// OutputFile is the path whose appended lines become named step outputs, or
// "" when the provider has no such file.
OutputFile string
// PathFile is the path whose appended lines are prepended to $PATH for later
// steps (GitHub's $GITHUB_PATH), or "" when unsupported.
PathFile string
// StepSummaryFile is the path whose appended Markdown becomes the step's
// rendered summary (GitHub's $GITHUB_STEP_SUMMARY), or "" when unsupported.
StepSummaryFile string
}
Sink records where the active provider expects exports to be written. A provider may expose up to two distinct channels:
- EnvFile — a file whose appended lines become environment variables for subsequent steps (GitHub's $GITHUB_ENV).
- OutputFile — a file whose appended lines become named step outputs consumed by later steps (GitHub's $GITHUB_OUTPUT).
A channel that the provider does not offer as a file (GitLab dotenv, Buildkite meta-data, Bitbucket stdout export) has an empty path; the writer keys off Sink.Provider for those dialects rather than a path. A Sink is pure data — reading it never opens or mutates a file.
func (Sink) HasEnvFile ¶
HasEnvFile reports whether the sink offers a file-backed environment-variable channel. The writer uses this to choose between file-append and stdout-export dialects.
func (Sink) HasOutputFile ¶
HasOutputFile reports whether the sink offers a file-backed step-output channel.