cisink

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 2 Imported by: 1 module, 1 package

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.

README

ci-sink-go

The fleet's one typed reader for the CI export-sink and variable context — so no tool re-implements "which CI am I in, and where do exports go?" with scattered os.Getenv checks.

What

A Reader (built with functional options) whose Read() returns a typed Context: the detected Provider (GitHub Actions / GitLab CI / Bitbucket Pipelines / Buildkite, or unknown locally), the export Sink (the provider-specific env/output/path/summary file paths), and the captured variable namespace. It is the reader half of the CI-export pattern; its sibling cisink-write-go is the writer that consumes a Sink to emit variable-set/output lines in the right dialect.

Detection is conservative — a provider is reported only when its canonical marker variable is present. A forced provider missing its required sink path becomes a typed, code-carrying error via errors-go (cisink_missing_sink / cisink_unsupported_provider).

Why

Every CI-aware tool starts by asking the same two questions: am I under CI, and which one? Splitting that read into one typed primitive means a tool that merely needs to branch on CI does not pull the emitters, and the writer library has one agreed source of truth for the sink paths — never a hand-rolled os.Getenv ladder again.

Install

go get github.com/pleme-io/ci-sink-go

Usage

ctx, err := cisink.Read()
if err != nil { return errs.Exit(err) }
if ctx.InCI() {
    log.Printf("running under %s", ctx.Provider)
    // ctx.Sink → hand to cisink-write-go to emit a variable.
}

// Hermetic / forced:
r := cisink.New(cisink.WithForcedProvider(cisink.ProviderGitHub))
ctx, err = r.Read()

Configuration

A typed Config (yaml-tagged) drives detection declaratively when a tool already loads a config file via shikumi-go. Load once at main, then cisink.FromConfig(cfg.CISink):

force_provider: github   # optional; empty → autodetect

FromConfig consumes the already-loaded sub-struct and never calls shikumi.Load itself (BOREALIS §3.5).

Release

Pull-model (Go modules): an annotated vX.Y.Z tag is the release; pkg.go.dev indexes it. See the GSDS module delivery FSM.

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

func Read() (Context, error)

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

func (c Context) InCI() bool

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

func (c Context) Lookup(key string) (string, bool)

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

func WithEnviron(environ func() []string) Option

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

func WithForcedProvider(p Provider) Option

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

func WithLookup(lookup func(string) (string, bool)) Option

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"
)

func (Provider) String

func (p Provider) String() string

String reports the provider's stable lowercase identifier, suitable for logging and for a writer to switch on.

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

func FromConfig(cfg Config, opts ...Option) *Reader

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

func New(opts ...Option) *Reader

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

func (r *Reader) Read() (Context, error)

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

func (s Sink) HasEnvFile() bool

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

func (s Sink) HasOutputFile() bool

HasOutputFile reports whether the sink offers a file-backed step-output channel.

Jump to

Keyboard shortcuts

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