2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-23 14:35:24 +00:00
|
|
|
"errors"
|
2021-11-25 19:43:31 +00:00
|
|
|
"strings"
|
2022-07-31 15:12:15 +00:00
|
|
|
"sync"
|
2017-03-05 07:56:08 +00:00
|
|
|
"time"
|
|
|
|
|
2023-12-21 19:23:51 +00:00
|
|
|
"github.com/oklog/ulid/v2"
|
2022-06-15 16:11:20 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-03-05 07:56:08 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
backend "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
// TODO: move runtime into "runtime" subpackage
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
type (
|
|
|
|
// State defines the pipeline and process state.
|
|
|
|
State struct {
|
|
|
|
// Global state of the pipeline.
|
|
|
|
Pipeline struct {
|
|
|
|
// Pipeline time started
|
|
|
|
Time int64 `json:"time"`
|
|
|
|
// Current pipeline step
|
|
|
|
Step *backend.Step `json:"step"`
|
|
|
|
// Current pipeline error state
|
|
|
|
Error error `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Current process state.
|
|
|
|
Process *backend.State
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runtime is a configuration runtime.
|
|
|
|
type Runtime struct {
|
|
|
|
err error
|
|
|
|
spec *backend.Config
|
2023-12-14 18:20:47 +00:00
|
|
|
engine backend.Backend
|
2017-03-05 07:56:08 +00:00
|
|
|
started int64
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
tracer Tracer
|
|
|
|
logger Logger
|
2022-06-15 16:11:20 +00:00
|
|
|
|
2023-07-20 18:39:20 +00:00
|
|
|
taskUUID string
|
|
|
|
|
2022-06-15 16:11:20 +00:00
|
|
|
Description map[string]string // The runtime descriptors.
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new runtime using the specified runtime
|
|
|
|
// configuration and runtime engine.
|
|
|
|
func New(spec *backend.Config, opts ...Option) *Runtime {
|
|
|
|
r := new(Runtime)
|
2022-06-15 16:11:20 +00:00
|
|
|
r.Description = map[string]string{}
|
2017-03-05 07:56:08 +00:00
|
|
|
r.spec = spec
|
|
|
|
r.ctx = context.Background()
|
2023-12-21 19:23:51 +00:00
|
|
|
r.taskUUID = ulid.Make().String()
|
2017-03-05 07:56:08 +00:00
|
|
|
for _, opts := range opts {
|
|
|
|
opts(r)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-06-15 16:11:20 +00:00
|
|
|
func (r *Runtime) MakeLogger() zerolog.Logger {
|
|
|
|
logCtx := log.With()
|
|
|
|
for key, val := range r.Description {
|
|
|
|
logCtx = logCtx.Str(key, val)
|
|
|
|
}
|
|
|
|
return logCtx.Logger()
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Run starts the execution of a workflow and waits for it to complete.
|
2022-11-23 14:35:24 +00:00
|
|
|
func (r *Runtime) Run(runnerCtx context.Context) error {
|
2022-06-15 16:11:20 +00:00
|
|
|
logger := r.MakeLogger()
|
2024-01-11 18:17:07 +00:00
|
|
|
logger.Debug().Msgf("executing %d stages, in order of:", len(r.spec.Stages))
|
2024-01-09 14:22:59 +00:00
|
|
|
for stagePos, stage := range r.spec.Stages {
|
|
|
|
stepNames := []string{}
|
2022-06-15 16:11:20 +00:00
|
|
|
for _, step := range stage.Steps {
|
2024-01-09 14:22:59 +00:00
|
|
|
stepNames = append(stepNames, step.Name)
|
2022-06-15 16:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug().
|
2024-01-09 14:22:59 +00:00
|
|
|
Int("StagePos", stagePos).
|
|
|
|
Str("Steps", strings.Join(stepNames, ",")).
|
2022-06-15 16:11:20 +00:00
|
|
|
Msg("stage")
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
defer func() {
|
2023-07-20 18:39:20 +00:00
|
|
|
if err := r.engine.DestroyWorkflow(runnerCtx, r.spec, r.taskUUID); err != nil {
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Error().Err(err).Msg("could not destroy engine")
|
2021-11-23 14:36:52 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
r.started = time.Now().Unix()
|
2024-01-11 21:15:15 +00:00
|
|
|
if err := r.engine.SetupWorkflow(runnerCtx, r.spec, r.taskUUID); err != nil {
|
2017-03-05 07:56:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stage := range r.spec.Stages {
|
|
|
|
select {
|
|
|
|
case <-r.ctx.Done():
|
|
|
|
return ErrCancel
|
|
|
|
case err := <-r.execAll(stage.Steps):
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Updates the current status of a step.
|
2022-05-11 11:40:44 +00:00
|
|
|
func (r *Runtime) traceStep(processState *backend.State, err error, step *backend.Step) error {
|
|
|
|
if r.tracer == nil {
|
|
|
|
// no tracer nothing to trace :)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if processState == nil {
|
|
|
|
processState = new(backend.State)
|
|
|
|
if err != nil {
|
|
|
|
processState.Error = err
|
|
|
|
processState.Exited = true
|
|
|
|
processState.OOMKilled = false
|
|
|
|
processState.ExitCode = 126 // command invoked cannot be executed.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state := new(State)
|
|
|
|
state.Pipeline.Time = r.started
|
|
|
|
state.Pipeline.Step = step
|
|
|
|
state.Process = processState // empty
|
|
|
|
state.Pipeline.Error = r.err
|
|
|
|
|
2022-06-15 16:11:20 +00:00
|
|
|
if traceErr := r.tracer.Trace(state); traceErr != nil {
|
|
|
|
return traceErr
|
|
|
|
}
|
|
|
|
return err
|
2022-05-11 11:40:44 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Executes a set of parallel steps.
|
2022-05-11 11:40:44 +00:00
|
|
|
func (r *Runtime) execAll(steps []*backend.Step) <-chan error {
|
2017-03-05 07:56:08 +00:00
|
|
|
var g errgroup.Group
|
|
|
|
done := make(chan error)
|
2022-06-15 16:11:20 +00:00
|
|
|
logger := r.MakeLogger()
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
for _, step := range steps {
|
2024-01-14 09:54:02 +00:00
|
|
|
// Required since otherwise the loop variable
|
2022-05-11 11:40:44 +00:00
|
|
|
// will be captured by the function. This will
|
|
|
|
// recreate the step "variable"
|
|
|
|
step := step
|
2017-03-05 07:56:08 +00:00
|
|
|
g.Go(func() error {
|
2022-05-11 11:40:44 +00:00
|
|
|
// Case the pipeline was already complete.
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Debug().
|
2024-01-14 09:54:02 +00:00
|
|
|
Str("step", step.Name).
|
|
|
|
Msg("prepare")
|
2022-06-15 16:11:20 +00:00
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
switch {
|
|
|
|
case r.err != nil && !step.OnFailure:
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Debug().
|
2024-01-14 09:54:02 +00:00
|
|
|
Str("step", step.Name).
|
2022-06-15 16:11:20 +00:00
|
|
|
Err(r.err).
|
2024-01-14 09:54:02 +00:00
|
|
|
Msgf("skipped due to OnFailure=%t", step.OnFailure)
|
2022-05-11 11:40:44 +00:00
|
|
|
return nil
|
|
|
|
case r.err == nil && !step.OnSuccess:
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Debug().
|
2024-01-14 09:54:02 +00:00
|
|
|
Str("step", step.Name).
|
|
|
|
Msgf("skipped due to OnSuccess=%t", step.OnSuccess)
|
2022-05-11 11:40:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace started.
|
|
|
|
err := r.traceStep(nil, nil, step)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-22 22:57:12 +00:00
|
|
|
// add compatibility for drone-ci plugins
|
2023-06-04 22:15:07 +00:00
|
|
|
metadata.SetDroneEnviron(step.Environment)
|
2022-11-22 22:57:12 +00:00
|
|
|
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Debug().
|
2024-01-14 09:54:02 +00:00
|
|
|
Str("step", step.Name).
|
|
|
|
Msg("executing")
|
2022-06-15 16:11:20 +00:00
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
processState, err := r.exec(step)
|
|
|
|
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Debug().
|
2024-01-14 09:54:02 +00:00
|
|
|
Str("step", step.Name).
|
|
|
|
Msg("complete")
|
2022-05-11 11:40:44 +00:00
|
|
|
|
2022-06-15 16:11:20 +00:00
|
|
|
// Return the error after tracing it.
|
2022-11-15 18:47:27 +00:00
|
|
|
err = r.traceStep(processState, err, step)
|
2023-06-04 22:15:07 +00:00
|
|
|
if err != nil && step.Failure == metadata.FailureIgnore {
|
2022-11-15 18:47:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2017-03-05 07:56:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
done <- g.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
return done
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
// Executes the step and returns the state and error.
|
|
|
|
func (r *Runtime) exec(step *backend.Step) (*backend.State, error) {
|
2023-07-20 18:39:20 +00:00
|
|
|
if err := r.engine.StartStep(r.ctx, step, r.taskUUID); err != nil {
|
2022-05-11 11:40:44 +00:00
|
|
|
return nil, err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:12:15 +00:00
|
|
|
var wg sync.WaitGroup
|
2017-03-05 07:56:08 +00:00
|
|
|
if r.logger != nil {
|
2023-07-20 18:39:20 +00:00
|
|
|
rc, err := r.engine.TailStep(r.ctx, step, r.taskUUID)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
2022-05-11 11:40:44 +00:00
|
|
|
return nil, err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:12:15 +00:00
|
|
|
wg.Add(1)
|
2017-03-05 07:56:08 +00:00
|
|
|
go func() {
|
2022-07-31 15:12:15 +00:00
|
|
|
defer wg.Done()
|
2022-06-15 16:11:20 +00:00
|
|
|
logger := r.MakeLogger()
|
|
|
|
|
2024-01-14 09:54:02 +00:00
|
|
|
if err := r.logger(step, rc); err != nil {
|
2022-06-15 16:11:20 +00:00
|
|
|
logger.Error().Err(err).Msg("process logging failed")
|
2021-11-23 14:36:52 +00:00
|
|
|
}
|
|
|
|
_ = rc.Close()
|
2017-03-05 07:56:08 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
// nothing else to do, this is a detached process.
|
|
|
|
if step.Detached {
|
|
|
|
return nil, nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 15:12:15 +00:00
|
|
|
// Some pipeline backends, such as local, will close the pipe from Tail on Wait,
|
|
|
|
// so first make sure all reading has finished.
|
|
|
|
wg.Wait()
|
2023-07-20 18:39:20 +00:00
|
|
|
waitState, err := r.engine.WaitStep(r.ctx, step, r.taskUUID)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
2022-11-23 14:35:24 +00:00
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
return waitState, ErrCancel
|
|
|
|
}
|
2022-05-11 11:40:44 +00:00
|
|
|
return nil, err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 08:35:11 +00:00
|
|
|
if err := r.engine.DestroyStep(r.ctx, step, r.taskUUID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
if waitState.OOMKilled {
|
|
|
|
return waitState, &OomError{
|
2024-01-09 04:43:03 +00:00
|
|
|
UUID: step.UUID,
|
2022-05-11 11:40:44 +00:00
|
|
|
Code: waitState.ExitCode,
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2022-05-11 11:40:44 +00:00
|
|
|
} else if waitState.ExitCode != 0 {
|
|
|
|
return waitState, &ExitError{
|
2024-01-09 04:43:03 +00:00
|
|
|
UUID: step.UUID,
|
2022-05-11 11:40:44 +00:00
|
|
|
Code: waitState.ExitCode,
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-11 11:40:44 +00:00
|
|
|
|
|
|
|
return waitState, nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|