Documentation
¶
Overview ¶
Package conditional validates and evaluates Buildkite conditional expressions.
The public API is the root package. Use Context to provide Buildkite values, set Context.EntryPoint to the place where the conditional runs, then call Validate or Evaluate. Optional variadic options can register caller-owned functions without changing default Buildkite server-parity behavior. Use NewEvaluator to reuse options across multiple validations or evaluations.
Validate always returns parse and validation errors. Evaluate returns errors for build condition entrypoints. Notification entrypoints model Buildkite notification delivery, so Evaluate converts parse, validation, and evaluation errors to false for those entrypoints.
Index ¶
- func Evaluate(expression string, ctx Context, opts ...Option) (bool, error)
- func IsErrorKind(err error, kind ErrorKind) bool
- func Validate(expression string, ctx Context, opts ...Option) error
- type Actor
- type Build
- type Context
- type EntryPoint
- type Error
- type ErrorKind
- type Evaluator
- type Function
- type MergeQueue
- type Option
- type Organization
- type Pipeline
- type PullRequest
- type RebuiltFrom
- type SCM
- type Step
- type TriggeredFrom
- type Value
- func (v Value) AsBool() (bool, bool)
- func (v Value) AsNumber() (int64, bool)
- func (v Value) AsRegexp() (pattern string, flags string, ok bool)
- func (v Value) AsString() (string, bool)
- func (v Value) AsStringArray() ([]string, bool)
- func (v Value) IsNull() bool
- func (v Value) String() string
- func (v Value) Type() ValueType
- type ValueType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsErrorKind ¶
IsErrorKind reports whether err contains a conditional Error with kind.
Types ¶
type Actor ¶
Actor contains server-resolved author or creator values. Email should contain the value exposed through the server's build.*.email assignments, including organization-preferred creator email resolution when applicable.
type Build ¶
type Build struct {
ID *string
State *string
Fixed *bool
BlockedState *string
Source *string
SourceEvent *string
SourceAction *string
Branch *string
Tag *string
Message *string
Commit *string
Number *int
Creator Actor
Author Actor
SCM SCM
PullRequest PullRequest
MergeQueue MergeQueue
TriggeredFrom TriggeredFrom
RebuiltFrom RebuiltFrom
}
Build contains build values exposed to conditionals.
type Context ¶
type Context struct {
EntryPoint EntryPoint
Build Build
Pipeline Pipeline
Organization Organization
Step *Step
// BuildEnv is build-scoped environment. ProjectEnv is pipeline/project
// environment. Matching Build::PipelineEnvironment, ProjectEnv is applied
// first, then BuildEnv overrides it.
BuildEnv map[string]string
ProjectEnv map[string]string
}
Context contains the Buildkite values available to a conditional.
type EntryPoint ¶
type EntryPoint string
EntryPoint identifies the server path that is evaluating a conditional.
const ( // EntryPointBuildCondition evaluates a Build::Condition without a step. EntryPointBuildCondition EntryPoint = "build_condition" // EntryPointBuildConditionWithStep evaluates a Build::Condition with a step. EntryPointBuildConditionWithStep EntryPoint = "build_condition_with_step" // EntryPointBuildNotification evaluates build notification deliverability. EntryPointBuildNotification EntryPoint = "build_notification" // EntryPointStepNotification evaluates step notification deliverability. EntryPointStepNotification EntryPoint = "step_notification" )
type Error ¶
Error is a typed conditional error. Cause contains a lower-level error when one is useful to expose through Unwrap.
type ErrorKind ¶
type ErrorKind string
ErrorKind classifies conditional failures without depending on exact server error text.
const ( // ErrorKindParse indicates that the expression could not be parsed. ErrorKindParse ErrorKind = "parse" // ErrorKindValidation indicates that validation failed before evaluation. ErrorKindValidation ErrorKind = "validation" // ErrorKindEvaluation indicates that evaluation failed. ErrorKindEvaluation ErrorKind = "evaluation" // ErrorKindResult indicates that the expression did not evaluate to a bool. ErrorKindResult ErrorKind = "result" )
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator validates and evaluates conditionals with reusable options.
The zero value is a Buildkite-parity evaluator with no caller-owned functions.
func NewEvaluator ¶
NewEvaluator returns an evaluator with reusable options.
Example ¶
package main
import (
"errors"
"fmt"
"strings"
"github.com/buildkite/conditional"
)
func main() {
branch := "release/2026-06-07"
startsWith := conditional.WithFunction("starts_with", conditional.Function{
Args: []conditional.ValueType{conditional.StringType, conditional.StringType},
Return: conditional.BoolType,
Eval: func(args []conditional.Value) (conditional.Value, error) {
value, ok := args[0].AsString()
if !ok {
return conditional.NullValue(), errors.New("value must be a string")
}
prefix, ok := args[1].AsString()
if !ok {
return conditional.NullValue(), errors.New("prefix must be a string")
}
return conditional.BoolValue(strings.HasPrefix(value, prefix)), nil
},
})
evaluator, err := conditional.NewEvaluator(startsWith)
if err != nil {
panic(err)
}
ok, err := evaluator.Evaluate(
`starts_with(build.branch, "release/")`,
conditional.Context{
EntryPoint: conditional.EntryPointBuildCondition,
Build: conditional.Build{
Branch: &branch,
},
},
)
if err != nil {
panic(err)
}
fmt.Println(ok)
}
Output: true
type MergeQueue ¶
type MergeQueue struct {
// Active reports whether this build is a merge queue build. The server uses
// this state to gate BUILDKITE_GIT_DIFF_BASE independently of the base values.
Active bool
BaseBranch *string
BaseCommit *string
}
MergeQueue contains merge queue values exposed to conditionals.
type Option ¶
type Option func(*optionSet) error
Option configures conditional validation and evaluation.
func WithFunction ¶
WithFunction registers an opt-in conditional function.
Example ¶
package main
import (
"errors"
"fmt"
"strings"
"github.com/buildkite/conditional"
)
func main() {
branch := "release/2026-06-07"
startsWith := conditional.WithFunction("starts_with", conditional.Function{
Args: []conditional.ValueType{conditional.StringType, conditional.StringType},
Return: conditional.BoolType,
Eval: func(args []conditional.Value) (conditional.Value, error) {
value, ok := args[0].AsString()
if !ok {
return conditional.NullValue(), errors.New("value must be a string")
}
prefix, ok := args[1].AsString()
if !ok {
return conditional.NullValue(), errors.New("prefix must be a string")
}
return conditional.BoolValue(strings.HasPrefix(value, prefix)), nil
},
})
ok, err := conditional.Evaluate(
`starts_with(build.branch, "release/")`,
conditional.Context{
EntryPoint: conditional.EntryPointBuildCondition,
Build: conditional.Build{
Branch: &branch,
},
},
startsWith,
)
if err != nil {
panic(err)
}
fmt.Println(ok)
}
Output: true
type Organization ¶
Organization contains organization values exposed to conditionals.
type Pipeline ¶
type Pipeline struct {
ID *string
Name *string
Slug *string
DefaultBranch *string
Repository *string
StartedPassing *bool
StartedFailing *bool
NextFinishedBuildExists *bool
UseMergeQueueBaseCommitForGitDiffBase *bool
}
Pipeline contains pipeline values exposed to conditionals.
type PullRequest ¶
type PullRequest struct {
ID *string
BaseBranch *string
Draft *bool
Label *string
Labels []string
Repository *string
RepositoryFork *bool
UsingMergeRefspec *bool
}
PullRequest contains pull request values exposed to conditionals.
type RebuiltFrom ¶
RebuiltFrom contains values for the build this build was rebuilt from.
type SCM ¶
type SCM struct {
AuthorName *string
AuthorEmail *string
CommitterName *string
CommitterEmail *string
}
SCM contains source control author and committer values.
type Step ¶
type Step struct {
ID *string
Key *string
Type *string
Label *string
State *string
Outcome *string
}
Step contains step values exposed to step-aware conditionals.
type TriggeredFrom ¶
TriggeredFrom contains values for the build/job that triggered this build.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a conditional runtime value.
The zero value represents null.
func RegexpValue ¶
RegexpValue returns a regular expression value.
func StringArrayValue ¶
StringArrayValue returns a string array value.
func (Value) AsRegexp ¶
AsRegexp returns the regular expression pattern and flags, if this value is a regular expression.
func (Value) AsStringArray ¶
AsStringArray returns a copy of the array values, if this value is a string array.
type ValueType ¶
type ValueType string
ValueType describes a conditional value type.
const ( // StringType is the conditional string type. StringType ValueType = "string" // NumberType is the conditional integer type. NumberType ValueType = "number" // BoolType is the conditional boolean type. BoolType ValueType = "boolean" // NullType is the conditional null type. NullType ValueType = "null" // RegexpType is the conditional regular expression type. RegexpType ValueType = "regular expression" // StringArrayType is the conditional string array type. StringArrayType ValueType = "string array" )