workflow

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType = errors.New("invalid type")
)
View Source
var ErrSkipped = errors.New("skipped")

ErrSkipped is returned when a Job or Step is skipped when invoked.

Functions

func Always

func Always(ctx Context) (bool, error)

Always will always run.

func GetAnyInput

func GetAnyInput(ctx Context, name string, required bool) (any, error)

GetAnyInput returns a value or output in the ctx. If a value does not exist, nil is returned, with a nil error unless required is true.

func GetAnyValue

func GetAnyValue(ctx Context, name string) (any, bool)

GetAnyValue returns a value in the ctx.

func GetInput

func GetInput[T any](ctx Context, name string, required bool) (T, error)

GetInput returns a value or output in the ctx. If a value does not exist, the type's zero value is returned, with a nil error unless required is true.

func GetValue

func GetValue[T any](ctx Context, name string) (T, error)

GetValue returns a value in the ctx. If a value does not exist, the type's zero value is returned, with a nil error.

Types

type Command

type Command func(ctx Context)

Command represents a side effect of a step. A step can return a command, which will then be executed after the stes completes. This enables a step to define outputs, values and more.

func Batch

func Batch(commands ...Command) Command

func SetOutput

func SetOutput(key string, value any) Command

SetOutput returns a Command that will store an output. Setting an output from a step without an ID is a no-op. Outputs are named depending on their scope. Within the same job, a step will be named like so:

"step.<stepId>.<key>"

From another job, the output can be referenced like so:

"job.<jobId>.step.<stepId>.<key>"

type Condition

type Condition = any

Condition controls whether or not a job or step (or post step) should run. A condition can be either a ConditionFunc, or a string which references a value retrievable using GetValue.

func ValueExists added in v0.13.0

func ValueExists(key string) Condition

ValueExists will evaluate to true if the given key has a value AND the job has not already failed.

type ConditionFunc

type ConditionFunc = func(ctx Context) (bool, error)

ConditionFunc is an adapter to allow the use of ordinary functions as Conditions.

type Context

type Context struct {
	context.Context

	// Workflow is the current workflow.
	Workflow Workflow
	// WorkflowRun describes the current invocation.
	WorkflowRun models.WorkflowRun
	// Job is the current job.
	Job      Job
	JobIndex int
	// Step is the current step.
	Step      Step
	StepIndex int

	// Outputs holds the outputs of steps and jobs, mapped by their path.
	// Outputs should not be written directly, as they are managed by the workflow
	// runtime.
	// Example:
	//   ctx.Outputs["step.getManifests.manifests"]
	//   ctx.Outputs["jobs.oci.step.getManifests.manifests"]
	Outputs map[string]any

	// Error holds any current error of the context.
	Error error
}

type Input

type Input = any

Input is an input value to a step. A value can be either a Ref, or any verbatim value.

type Job

type Job struct {
	ID        string
	Name      string
	Steps     []Step
	DependsOn []string
	If        Condition
}

func (Job) Describe

func (j Job) Describe(namespace string) string

func (Job) Run

func (j Job) Run(ctx Context) (Context, error)

Run runs the job. Returns the modified context and any error. Returns ErrSkipped if the job was not run.

type Ref

type Ref struct {
	Key string
}

Ref is an Input that refers to a value retrievable by GetValue.

type Step

type Step struct {
	ID   string
	Name string

	// Inputs holds a map of named inputs that can be used by the step.
	Inputs map[string]Input

	Main func(ctx Context) (Command, error)
	If   Condition

	Post   func(ctx Context) error
	PostIf Condition
}

func Run

func Run(f StepFunc) Step

func (Step) Describe

func (s Step) Describe(namespace string) string

func (Step) Run added in v0.17.0

func (s Step) Run(ctx Context) (Context, error)

Run runs a step. Returns the modified context. Returns ErrSkipped if the job was not run. It's the caller's responsibility to ensure [Step.Main] is defined.

func (Step) RunPost added in v0.17.0

func (s Step) RunPost(ctx Context) error

RunPost runs a step's post step. Returns ErrSkipped if the job was not run. It's the caller's responsibility to ensure [Step.Post] is defined.

func (Step) With

func (s Step) With(key string, input Input) Step

func (Step) WithCondition

func (s Step) WithCondition(condition Condition) Step

func (Step) WithID

func (s Step) WithID(id string) Step

type StepFunc

type StepFunc func(ctx Context) (Command, error)

type Workflow

type Workflow struct {
	Name string
	Jobs []Job
}

func (Workflow) Describe

func (w Workflow) Describe() string

func (Workflow) Run

Run executes a workflow and (always) returns a run description and any error that caused the workflow to fail.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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