mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-23 00:46:30 +00:00
Add support for steps to be a list (instead of dict) (#826)
- Support for pipeline/containers as list - Support for container name in logs (step.Name) Co-authored-by: Zav Shotan <zshotan@bloomberg.net> Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
cdbba4c306
commit
81dcdea2be
4 changed files with 73 additions and 26 deletions
|
@ -145,8 +145,15 @@ func (c *Compiler) createProcess(name string, container *yaml.Container, section
|
|||
cpuSet = c.reslimit.CPUSet
|
||||
}
|
||||
|
||||
stepName := container.Name
|
||||
if len(stepName) == 0 {
|
||||
stepName = name
|
||||
} else {
|
||||
stepName = section + "." + stepName
|
||||
}
|
||||
|
||||
return &backend.Step{
|
||||
Name: name,
|
||||
Name: stepName,
|
||||
Alias: container.Name,
|
||||
Image: image,
|
||||
Pull: container.Pull,
|
||||
|
|
|
@ -65,26 +65,47 @@ type (
|
|||
|
||||
// UnmarshalYAML implements the Unmarshaler interface.
|
||||
func (c *Containers) UnmarshalYAML(value *yaml.Node) error {
|
||||
containers := map[string]Container{}
|
||||
err := value.Decode(&containers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch value.Kind {
|
||||
// We support mapps ...
|
||||
case yaml.MappingNode:
|
||||
c.Containers = make([]*Container, 0, len(value.Content)/2+1)
|
||||
// We cannot use decode on specific values
|
||||
// since if we try to load from a map, the order
|
||||
// will not be kept. Therefore use value.Content
|
||||
// and take the map values i%2=1
|
||||
for i, n := range value.Content {
|
||||
if i%2 == 1 {
|
||||
container := &Container{}
|
||||
if err := n.Decode(container); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, n := range value.Content {
|
||||
if i%2 == 1 {
|
||||
container := Container{}
|
||||
err := n.Decode(&container)
|
||||
if err != nil {
|
||||
if container.Name == "" {
|
||||
container.Name = fmt.Sprintf("%v", value.Content[i-1].Value)
|
||||
}
|
||||
|
||||
c.Containers = append(c.Containers, container)
|
||||
}
|
||||
}
|
||||
|
||||
// ... and lists
|
||||
case yaml.SequenceNode:
|
||||
c.Containers = make([]*Container, 0, len(value.Content))
|
||||
for i, n := range value.Content {
|
||||
container := &Container{}
|
||||
if err := n.Decode(container); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if container.Name == "" {
|
||||
container.Name = fmt.Sprintf("%v", value.Content[i-1].Value)
|
||||
container.Name = fmt.Sprintf("step-%d", i)
|
||||
}
|
||||
|
||||
c.Containers = append(c.Containers, &container)
|
||||
c.Containers = append(c.Containers, container)
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("yaml node type[%d]: '%s' not supported", value.Kind, value.Tag)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func TestLint(t *testing.T) {
|
||||
testdata := `
|
||||
testdatas := []struct{ Title, Data string }{{Title: "map", Data: `
|
||||
pipeline:
|
||||
build:
|
||||
image: docker
|
||||
|
@ -28,14 +28,34 @@ services:
|
|||
image: redis
|
||||
entrypoint: [ /bin/redis-server ]
|
||||
command: [ -v ]
|
||||
`
|
||||
`}, {Title: "list", Data: `
|
||||
pipeline:
|
||||
- name: build
|
||||
image: docker
|
||||
privileged: true
|
||||
network_mode: host
|
||||
volumes:
|
||||
- /tmp:/tmp
|
||||
commands:
|
||||
- go build
|
||||
- go test
|
||||
- name: publish
|
||||
image: plugins/docker
|
||||
repo: foo/bar
|
||||
settings:
|
||||
foo: bar
|
||||
`}}
|
||||
|
||||
conf, err := yaml.ParseString(testdata)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot unmarshal yaml %q. Error: %s", testdata, err)
|
||||
}
|
||||
if err := New(WithTrusted(true)).Lint(conf); err != nil {
|
||||
t.Errorf("Expected lint returns no errors, got %q", err)
|
||||
for _, testd := range testdatas {
|
||||
t.Run(testd.Title, func(t *testing.T) {
|
||||
conf, err := yaml.ParseString(testd.Data)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot unmarshal yaml %q. Error: %s", testd, err)
|
||||
}
|
||||
if err := New(WithTrusted(true)).Lint(conf); err != nil {
|
||||
t.Errorf("Expected lint returns no errors, got %q", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -90,11 +90,10 @@
|
|||
},
|
||||
"pipeline": {
|
||||
"description": "The pipeline section defines a list of steps which will be executed serially, in the order in which they are defined. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax",
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"$ref": "#/definitions/step"
|
||||
},
|
||||
"minProperties": 1
|
||||
"oneOf": [
|
||||
{ "type": "object", "additionalProperties": { "$ref": "#/definitions/step" }, "minProperties": 1 },
|
||||
{ "type": "array", "items": { "$ref": "#/definitions/step" }, "minLength": 1 }
|
||||
]
|
||||
},
|
||||
"step": {
|
||||
"description": "Every step of your pipeline executes arbitrary commands inside a specified docker container. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#steps",
|
||||
|
|
Loading…
Reference in a new issue