Skip to content

Commit

Permalink
Add workflow version (#2476)
Browse files Browse the repository at this point in the history
Closes #1834

---------

Co-authored-by: 6543 <[email protected]>
Co-authored-by: Anbraten <[email protected]>
Co-authored-by: Lauris BH <[email protected]>
  • Loading branch information
4 people authored Nov 6, 2023
1 parent 9e0e62a commit 1bc4415
Show file tree
Hide file tree
Showing 35 changed files with 152 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .woodpecker/binaries.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

depends_on:
- test
- web
Expand Down
2 changes: 2 additions & 0 deletions .woodpecker/docker.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

when:
- event: [pull_request, tag]
- event: push
Expand Down
2 changes: 2 additions & 0 deletions .woodpecker/docs.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

when:
- event: tag
- event: pull_request
Expand Down
2 changes: 2 additions & 0 deletions .woodpecker/release-helper.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
release-helper:
image: woodpeckerci/plugin-ready-release-go:0.7.0
Expand Down
2 changes: 2 additions & 0 deletions .woodpecker/securityscan.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

when:
- event: [pull_request, cron]
- event: push
Expand Down
2 changes: 2 additions & 0 deletions .woodpecker/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

when:
- event: [pull_request, tag]
- event: push
Expand Down
2 changes: 2 additions & 0 deletions .woodpecker/web.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

when:
- event: [pull_request, tag]
- event: push
Expand Down
1 change: 1 addition & 0 deletions contrib/woodpecker-test-repo/.woodpecker/demo.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 1
steps:
demo:
image: 'alpine'
Expand Down
1 change: 1 addition & 0 deletions contrib/woodpecker-test-repo/.woodpecker/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 1
steps:
test_1:
image: 'alpine'
Expand Down
5 changes: 4 additions & 1 deletion pipeline/frontend/yaml/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
func TestLint(t *testing.T) {
testdatas := []struct{ Title, Data string }{{
Title: "map", Data: `
version: 1
steps:
build:
image: docker
Expand All @@ -45,6 +46,7 @@ services:
`,
}, {
Title: "list", Data: `
version: 1
steps:
- name: build
image: docker
Expand All @@ -61,6 +63,7 @@ steps:
`,
}, {
Title: "merge maps", Data: `
version: 1
variables:
step_template: &base-step
image: golang:1.19
Expand Down Expand Up @@ -155,7 +158,7 @@ func TestLintErrors(t *testing.T) {
}

for _, test := range testdata {
conf, err := yaml.ParseString(test.from)
conf, err := yaml.ParseString("version: 1\n" + test.from)
if err != nil {
t.Fatalf("Cannot unmarshal yaml %q. Error: %s", test.from, err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

branches: [main, pages]

steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

branches:
include: main
exclude: [develop, feature/*]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

branches: main

steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

branches: main

matri:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
test:
image: alpine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

clone:
git:
image: plugins/git:next
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

labels:
location: europe
weather: sun
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
test:
image: golang:${GO_VERSION}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

variables:
step_template: &base-step
image: golang:1.19
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
deploy:
image: golang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

when:
- branch: [main, deploy]
event: push
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

platform: linux/amd64

steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
build:
image: golang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
build:
image: golang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
build:
image: golang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
image:
image: golang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

steps:
when-branch:
image: alpine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 1

workspace:
base: /go
path: src/github.com/octocat/hello-world
Expand Down
3 changes: 2 additions & 1 deletion pipeline/frontend/yaml/linter/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"type": "array",
"minLength": 1,
"items": { "type": "string" }
}
},
"version": { "type": "number", "default": 1 }
},
"definitions": {
"clone": {
Expand Down
30 changes: 29 additions & 1 deletion pipeline/frontend/yaml/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@
package yaml

import (
"errors"
"fmt"

"codeberg.org/6543/xyaml"

"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/constraint"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types/base"
"github.com/woodpecker-ci/woodpecker/shared/constant"
)

var ErrUnsuportedVersion = errors.New("unsuported pipeline config version detected")

// ParseBytes parses the configuration from bytes b.
func ParseBytes(b []byte) (*types.Workflow, error) {
yamlVersion, err := checkVersion(b)
if err != nil {
return nil, err
}

out := new(types.Workflow)
err := xyaml.Unmarshal(b, out)
err = xyaml.Unmarshal(b, out)
if err != nil {
return nil, err
}

// make sure detected version is set
out.Version = yamlVersion

// support deprecated branch filter
if out.BranchesDontUseIt != nil {
if out.When.Constraints == nil {
Expand Down Expand Up @@ -70,3 +82,19 @@ func ParseString(s string) (*types.Workflow, error) {
[]byte(s),
)
}

func checkVersion(b []byte) (int, error) {
ver := struct {
Version int `yaml:"version"`
}{}
_ = xyaml.Unmarshal(b, &ver)
if ver.Version == 0 {
// default: version 1
return constant.DefaultPipelineVersion, nil
}

if ver.Version != Version {
return 0, ErrUnsuportedVersion
}
return ver.Version, nil
}
19 changes: 19 additions & 0 deletions pipeline/frontend/yaml/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ func TestParse(t *testing.T) {
g.Assert(out.Steps.ContainerList[1].When.Constraints[0].Event.Include).Equal([]string{"success"})
})

g.It("Should unmarshal with default version", func() {
out, err := ParseString(sampleYamlDefaultVersion)
if err != nil {
g.Fail(err)
}
g.Assert(len(out.Steps.ContainerList)).Equal(1)
g.Assert(out.Steps.ContainerList[0].Name).Equal("notify_success")
g.Assert(out.Steps.ContainerList[0].Image).Equal("xyz")
})

matchConfig, err := ParseString(sampleYaml)
if err != nil {
g.Fail(err)
Expand Down Expand Up @@ -180,6 +190,7 @@ pipeline:
}

var sampleYaml = `
version: 1
image: hello-world
when:
- event:
Expand Down Expand Up @@ -231,7 +242,14 @@ runs_on:
- failure
`

var sampleYamlDefaultVersion = `
steps:
- name: notify_success
image: xyz
`

var simpleYamlAnchors = `
version: 1
vars:
image: &image plugins/slack
steps:
Expand All @@ -240,6 +258,7 @@ steps:
`

var sampleVarYaml = `
version: 1
_slack: &SLACK
image: plugins/slack
steps:
Expand Down
1 change: 1 addition & 0 deletions pipeline/frontend/yaml/types/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type (
DependsOn []string `yaml:"depends_on,omitempty"`
RunsOn []string `yaml:"runs_on,omitempty"`
SkipClone bool `yaml:"skip_clone"`
Version int `yaml:"version"`

// Undocumented
Cache base.StringOrSlice `yaml:"cache,omitempty"`
Expand Down
18 changes: 18 additions & 0 deletions pipeline/frontend/yaml/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

package yaml

// Version of this package and it's subpackages
const Version = 1
Loading

0 comments on commit 1bc4415

Please sign in to comment.