workflow

package
v0.22.1 Latest Latest
Warning

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

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

Documentation

Overview

Package workflow provides a generic way to represent running tasks with or without dependencies. The implementation mimics GitHub actions / workflows.

Index

Constants

This section is empty.

Variables

View Source
var ErrDependentJobFailed = errors.New("dependent job failed")
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

Batch batches several commands, running them in sequence.

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.

type ConditionFunc

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

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

func ValueExists added in v0.13.0

func ValueExists(key string) ConditionFunc

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

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
}

Context is a context.Context implementation holding additional context for a workflow. The value should be seen as being immutable.

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 is an optional ID by which to reference any outputs created by the job's
	// steps.
	ID string
	// Name is a human-readable name of the job.
	Name string
	// Steps holds all the steps that are run as part of the job.
	Steps []Step
	// DependsOn optionally holds the ids of jobs that must complete for this job
	// to run.
	// Jobs will not run if one of its dependencies fail.
	DependsOn []string
	// If holds a [Condition] that must pass for this job to run.
	If Condition
}

Job represents a series of steps. The implementation mimics GitHub actions / workflows.

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
}

Step represents a single unit of an operation. The implementation mimics GitHub actions / workflows.

func Run

func Run(f StepFunc) Step

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 is the human-readable name of the workflow.
	Name string
	// Jobs holds all the jobs that the workflow should run.
	// Jobs are started in the order defined by their dependencies.
	Jobs []Job
}

Workflow is a generic way to represent running tasks with or without dependencies. The implementation mimics GitHub actions / workflows.

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