Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use name in backend types instead of alias #3142

Merged
merged 11 commits into from
Jan 9, 2024
2 changes: 1 addition & 1 deletion agent/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (r *Runner) createLogger(logger zerolog.Logger, uploads *sync.WaitGroup, wo
return func(step *backend.Step, rc multipart.Reader) error {
loglogger := logger.With().
Str("image", step.Image).
Str("stage", step.Alias).
Str("workflowID", workflow.ID).
Logger()

part, rerr := rc.NextPart()
Expand Down
4 changes: 2 additions & 2 deletions agent/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func (r *Runner) createTracer(ctxmeta context.Context, logger zerolog.Logger, wo
return func(state *pipeline.State) error {
steplogger := logger.With().
Str("image", state.Pipeline.Step.Image).
Str("stage", state.Pipeline.Step.Alias).
Str("workflowID", workflow.ID).
Err(state.Process.Error).
Int("exit_code", state.Process.ExitCode).
Bool("exited", state.Process.Exited).
Logger()

stepState := rpc.State{
Step: state.Pipeline.Step.Alias,
Step: state.Pipeline.Step.Name,
Exited: state.Process.Exited,
ExitCode: state.Process.ExitCode,
Started: time.Now().Unix(), // TODO do not do this
Expand Down
2 changes: 1 addition & 1 deletion cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ var defaultLogger = pipeline.LogFunc(func(step *backendTypes.Step, rc multipart.
return err
}

logStream := NewLineWriter(step.Alias, step.UUID)
logStream := NewLineWriter(step.Name, step.UUID)
_, err = io.Copy(logStream, part)
return err
})
2 changes: 0 additions & 2 deletions pipeline/backend/types/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ package types

// Stage denotes a collection of one or more steps.
type Stage struct {
Name string `json:"name,omitempty"`
Alias string `json:"alias,omitempty"`
Steps []*Step `json:"steps,omitempty"`
}
1 change: 0 additions & 1 deletion pipeline/backend/types/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type Step struct {
Name string `json:"name"`
UUID string `json:"uuid"`
Type StepType `json:"type,omitempty"`
Alias string `json:"alias,omitempty"`
Image string `json:"image,omitempty"`
Pull bool `json:"pull,omitempty"`
Detached bool `json:"detach,omitempty"`
Expand Down
36 changes: 9 additions & 27 deletions pipeline/frontend/yaml/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

const (
defaultCloneName = "clone"

nameServices = "services"
)

// Registry represents registry credentials
Expand Down Expand Up @@ -161,32 +159,26 @@
Settings: cloneSettings,
Environment: c.cloneEnv,
}
name := fmt.Sprintf("%s_clone", c.prefix)
step, err := c.createProcess(name, container, backend_types.StepTypeClone)
step, err := c.createProcess(container, backend_types.StepTypeClone)
if err != nil {
return nil, err
}

stage := new(backend_types.Stage)
stage.Name = name
stage.Alias = defaultCloneName
stage.Steps = append(stage.Steps, step)

config.Stages = append(config.Stages, stage)
} else if !c.local && !conf.SkipClone {
for i, container := range conf.Clone.ContainerList {
for _, container := range conf.Clone.ContainerList {

Check warning on line 172 in pipeline/frontend/yaml/compiler/compiler.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/compiler.go#L172

Added line #L172 was not covered by tests
if match, err := container.When.Match(c.metadata, false, c.env); !match && err == nil {
continue
} else if err != nil {
return nil, err
}

stage := new(backend_types.Stage)
stage.Name = fmt.Sprintf("%s_clone_%v", c.prefix, i)
stage.Alias = container.Name

name := fmt.Sprintf("%s_clone_%d", c.prefix, i)
step, err := c.createProcess(name, container, backend_types.StepTypeClone)
step, err := c.createProcess(container, backend_types.StepTypeClone)

Check warning on line 181 in pipeline/frontend/yaml/compiler/compiler.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/compiler.go#L181

Added line #L181 was not covered by tests
if err != nil {
return nil, err
}
Expand All @@ -212,18 +204,15 @@
// add services steps
if len(conf.Services.ContainerList) != 0 {
stage := new(backend_types.Stage)
stage.Name = fmt.Sprintf("%s_%s", c.prefix, nameServices)
stage.Alias = nameServices

for i, container := range conf.Services.ContainerList {
for _, container := range conf.Services.ContainerList {

Check warning on line 208 in pipeline/frontend/yaml/compiler/compiler.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/compiler.go#L208

Added line #L208 was not covered by tests
if match, err := container.When.Match(c.metadata, false, c.env); !match && err == nil {
continue
} else if err != nil {
return nil, err
}

name := fmt.Sprintf("%s_%s_%d", c.prefix, nameServices, i)
step, err := c.createProcess(name, container, backend_types.StepTypeService)
step, err := c.createProcess(container, backend_types.StepTypeService)

Check warning on line 215 in pipeline/frontend/yaml/compiler/compiler.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/compiler.go#L215

Added line #L215 was not covered by tests
if err != nil {
return nil, err
}
Expand All @@ -247,12 +236,11 @@
return nil, err
}

name := fmt.Sprintf("%s_step_%d", c.prefix, pos)
stepType := backend_types.StepTypeCommands
if container.IsPlugin() {
stepType = backend_types.StepTypePlugin
}
step, err := c.createProcess(name, container, stepType)
step, err := c.createProcess(container, stepType)
if err != nil {
return nil, err
}
Expand All @@ -274,7 +262,7 @@
}

// generate stages out of steps
stepStages, err := newDAGCompiler(steps, c.prefix).compile()
stepStages, err := newDAGCompiler(steps).compile()
if err != nil {
return nil, err
}
Expand All @@ -295,15 +283,12 @@
}

container := c.cacher.Restore(path.Join(c.metadata.Repo.Owner, c.metadata.Repo.Name), c.metadata.Curr.Commit.Branch, conf.Cache)
name := fmt.Sprintf("%s_restore_cache", c.prefix)
step, err := c.createProcess(name, container, backend_types.StepTypeCache)
step, err := c.createProcess(container, backend_types.StepTypeCache)

Check warning on line 286 in pipeline/frontend/yaml/compiler/compiler.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/compiler.go#L286

Added line #L286 was not covered by tests
if err != nil {
return err
}

stage := new(backend_types.Stage)
stage.Name = name
stage.Alias = "restore_cache"
stage.Steps = append(stage.Steps, step)

ir.Stages = append(ir.Stages, stage)
Expand All @@ -317,15 +302,12 @@
}
container := c.cacher.Rebuild(path.Join(c.metadata.Repo.Owner, c.metadata.Repo.Name), c.metadata.Curr.Commit.Branch, conf.Cache)

name := fmt.Sprintf("%s_rebuild_cache", c.prefix)
step, err := c.createProcess(name, container, backend_types.StepTypeCache)
step, err := c.createProcess(container, backend_types.StepTypeCache)

Check warning on line 305 in pipeline/frontend/yaml/compiler/compiler.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/compiler.go#L305

Added line #L305 was not covered by tests
if err != nil {
return err
}

stage := new(backend_types.Stage)
stage.Name = name
stage.Alias = "rebuild_cache"
stage.Steps = append(stage.Steps, step)

ir.Stages = append(ir.Stages, stage)
Expand Down
36 changes: 8 additions & 28 deletions pipeline/frontend/yaml/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,8 @@ func TestCompilerCompile(t *testing.T) {
}}

defaultCloneStage := &backend_types.Stage{
Name: "test_clone",
Alias: "clone",
Steps: []*backend_types.Step{{
Name: "test_clone",
Alias: "clone",
Name: "clone",
Type: backend_types.StepTypeClone,
Image: constant.DefaultCloneImage,
OnSuccess: true,
Expand Down Expand Up @@ -127,11 +124,8 @@ func TestCompilerCompile(t *testing.T) {
Networks: defaultNetworks,
Volumes: defaultVolumes,
Stages: []*backend_types.Stage{defaultCloneStage, {
Name: "test_stage_0",
Alias: "dummy",
Steps: []*backend_types.Step{{
Name: "test_step_0",
Alias: "dummy",
Name: "dummy",
Type: backend_types.StepTypePlugin,
Image: "dummy_img",
OnSuccess: true,
Expand Down Expand Up @@ -164,11 +158,8 @@ func TestCompilerCompile(t *testing.T) {
Networks: defaultNetworks,
Volumes: defaultVolumes,
Stages: []*backend_types.Stage{defaultCloneStage, {
Name: "test_stage_0",
Alias: "echo env",
Steps: []*backend_types.Step{{
Name: "test_step_0",
Alias: "echo env",
Name: "echo env",
Type: backend_types.StepTypeCommands,
Image: "bash",
Commands: []string{"env"},
Expand All @@ -179,11 +170,8 @@ func TestCompilerCompile(t *testing.T) {
ExtraHosts: []backend_types.HostAlias{},
}},
}, {
Name: "test_stage_1",
Alias: "parallel echo 1",
Steps: []*backend_types.Step{{
Name: "test_step_1",
Alias: "parallel echo 1",
Name: "parallel echo 1",
Type: backend_types.StepTypeCommands,
Image: "bash",
Commands: []string{"echo 1"},
Expand All @@ -193,8 +181,7 @@ func TestCompilerCompile(t *testing.T) {
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 1"}}},
ExtraHosts: []backend_types.HostAlias{},
}, {
Name: "test_step_2",
Alias: "parallel echo 2",
Name: "parallel echo 2",
Type: backend_types.StepTypeCommands,
Image: "bash",
Commands: []string{"echo 2"},
Expand Down Expand Up @@ -227,11 +214,8 @@ func TestCompilerCompile(t *testing.T) {
Networks: defaultNetworks,
Volumes: defaultVolumes,
Stages: []*backend_types.Stage{defaultCloneStage, {
Name: "test_stage_0",
Alias: "test_stage_0",
Steps: []*backend_types.Step{{
Name: "test_step_0",
Alias: "echo env",
Name: "echo env",
Type: backend_types.StepTypeCommands,
Image: "bash",
Commands: []string{"env"},
Expand All @@ -241,8 +225,7 @@ func TestCompilerCompile(t *testing.T) {
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}},
ExtraHosts: []backend_types.HostAlias{},
}, {
Name: "test_step_2",
Alias: "echo 2",
Name: "echo 2",
Type: backend_types.StepTypeCommands,
Image: "bash",
Commands: []string{"echo 2"},
Expand All @@ -253,11 +236,8 @@ func TestCompilerCompile(t *testing.T) {
ExtraHosts: []backend_types.HostAlias{},
}},
}, {
Name: "test_stage_1",
Alias: "test_stage_1",
Steps: []*backend_types.Step{{
Name: "test_step_1",
Alias: "echo 1",
Name: "echo 1",
Type: backend_types.StepTypeCommands,
Image: "bash",
Commands: []string{"echo 1"},
Expand Down
5 changes: 2 additions & 3 deletions pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/utils"
)

func (c *Compiler) createProcess(name string, container *yaml_types.Container, stepType backend_types.StepType) (*backend_types.Step, error) {
func (c *Compiler) createProcess(container *yaml_types.Container, stepType backend_types.StepType) (*backend_types.Step, error) {
var (
uuid = ulid.Make()

Expand Down Expand Up @@ -171,10 +171,9 @@ func (c *Compiler) createProcess(name string, container *yaml_types.Container, s
}

return &backend_types.Step{
Name: name,
Name: container.Name,
UUID: uuid.String(),
Type: stepType,
Alias: container.Name,
Image: container.Image,
Pull: container.Pull,
Detached: detached,
Expand Down
20 changes: 6 additions & 14 deletions pipeline/frontend/yaml/compiler/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package compiler

import (
"fmt"
"sort"

backend_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
Expand All @@ -30,14 +29,12 @@ type dagCompilerStep struct {
}

type dagCompiler struct {
steps []*dagCompilerStep
prefix string
steps []*dagCompilerStep
}

func newDAGCompiler(steps []*dagCompilerStep, prefix string) dagCompiler {
func newDAGCompiler(steps []*dagCompilerStep) dagCompiler {
return dagCompiler{
steps: steps,
prefix: prefix,
steps: steps,
}
}

Expand Down Expand Up @@ -68,8 +65,6 @@ func (c dagCompiler) compileByGroup() ([]*backend_types.Stage, error) {
currentGroup = s.group

currentStage = new(backend_types.Stage)
currentStage.Name = fmt.Sprintf("%s_stage_%v", c.prefix, s.position)
currentStage.Alias = s.name
stages = append(stages, currentStage)
}

Expand All @@ -85,7 +80,7 @@ func (c dagCompiler) compileByDependsOn() ([]*backend_types.Stage, error) {
for _, s := range c.steps {
stepMap[s.name] = s
}
return convertDAGToStages(stepMap, c.prefix)
return convertDAGToStages(stepMap)
}

func dfsVisit(steps map[string]*dagCompilerStep, name string, visited map[string]struct{}, path []string) error {
Expand All @@ -107,7 +102,7 @@ func dfsVisit(steps map[string]*dagCompilerStep, name string, visited map[string
return nil
}

func convertDAGToStages(steps map[string]*dagCompilerStep, prefix string) ([]*backend_types.Stage, error) {
func convertDAGToStages(steps map[string]*dagCompilerStep) ([]*backend_types.Stage, error) {
addedSteps := make(map[string]struct{})
stages := make([]*backend_types.Stage, 0)

Expand All @@ -128,10 +123,7 @@ func convertDAGToStages(steps map[string]*dagCompilerStep, prefix string) ([]*ba

for len(steps) > 0 {
addedNodesThisLevel := make(map[string]struct{})
stage := &backend_types.Stage{
Name: fmt.Sprintf("%s_stage_%d", prefix, len(stages)),
Alias: fmt.Sprintf("%s_stage_%d", prefix, len(stages)),
}
stage := new(backend_types.Stage)

var stepsToAdd []*dagCompilerStep
for name, step := range steps {
Expand Down
Loading
Loading