1
0
Fork 0

Add pipeline services

This commit is contained in:
Lauris BH 2022-07-30 03:48:03 +03:00
parent ac85763829
commit 080c2089fd
No known key found for this signature in database
GPG Key ID: DFDE60A0093EB926
10 changed files with 149 additions and 54 deletions

View File

@ -17,6 +17,14 @@ Currently supports converting only from Drone CI pipeline format with limited fu
* `arch`
* `version` - not supported by Woodpecker CI
* `node`
* `services`
* `name`
* `image`
* `pull`
* `privileged` - not supported by Woodpecker CI
* `environment` - including `from_secret`
* `entrypoint`
* `commands`
* `steps`
* `name`
* `image`

View File

@ -4,6 +4,10 @@
package transform
import (
"codeberg.org/lafriks/woodpecker-pipeline-transform/core"
)
type Conditions struct {
Conditions []string `yaml:"-"`
Include []string `yaml:"include,omitempty"`
@ -64,3 +68,29 @@ func (c PathConditions) MarshalYAML() (interface{}, error) {
IgnoreMessage: c.IgnoreMessage,
}, nil
}
type When struct {
Repo core.Strings `yaml:"repo,omitempty"`
Branch *Conditions `yaml:"branch,omitempty"`
Event core.Strings `yaml:"event,omitempty"`
Tag string `yaml:"tag,omitempty"`
Status []string `yaml:"status,omitempty"`
Platform core.Strings `yaml:"platform,omitempty"`
Environment string `yaml:"environment,omitempty"`
Matrix map[string]string `yaml:"matrix,omitempty"`
Instance string `yaml:"instance,omitempty"`
Path *PathConditions `yaml:"path,omitempty"`
}
func (s When) IsEmpty() bool {
return len(s.Repo) == 0 &&
s.Branch.IsEmpty() &&
len(s.Event) == 0 &&
s.Tag == "" &&
len(s.Status) == 0 &&
len(s.Platform) == 0 &&
s.Environment == "" &&
len(s.Matrix) == 0 &&
s.Instance == "" &&
s.Path.IsEmpty()
}

View File

@ -37,3 +37,14 @@ func (c *Conditions) UnmarshalYAML(unmarshal func(interface{}) error) error {
c.Conditions = v2
return nil
}
type When struct {
Branch *Conditions `yaml:"branch"`
Event core.Strings `yaml:"event"`
Refs *Conditions `yaml:"refs"`
Repositories *Conditions `yaml:"repo"`
Instance *Conditions `yaml:"instance"`
Status core.Strings `yaml:"status"`
Target *Conditions `yaml:"target"`
Cron *Conditions `yaml:"cron"`
}

View File

@ -53,7 +53,7 @@ func (d DronePipeline) ConvertEvents(events core.Strings) (core.Strings, error)
return ev, nil
}
func (d DronePipeline) ConvertConditions(when *StepWhen) (*transform.StepWhen, error) {
func (d DronePipeline) ConvertConditions(when *When) (*transform.When, error) {
if when == nil {
return nil, nil
}
@ -151,7 +151,7 @@ func (d DronePipeline) ConvertConditions(when *StepWhen) (*transform.StepWhen, e
target = when.Target.Include[0]
}
}
r := &transform.StepWhen{
r := &transform.When{
Branch: branch,
Event: ev,
Tag: tags,
@ -190,6 +190,41 @@ func (d DronePipeline) Convert(pipeline *Pipeline) (*transform.Pipeline, error)
p.Platform = pipeline.Platform.OS + "/" + pipeline.Platform.Arch
}
// Services
for _, service := range pipeline.Services {
if service.Privileged {
return nil, errors.New("unsupported service property: privileged")
}
if len(service.WorkingDir) > 0 {
return nil, errors.New("unsupported service property: working_dir")
}
env := make([]string, 0, len(service.Environment))
for k, v := range service.Environment {
if v.Secret != "" {
continue
}
env = append(env, k+"="+v.Value)
}
secrets := make(transform.Secrets, 0)
for k, v := range service.Environment {
if v.Secret == "" {
continue
}
secrets = append(secrets, transform.Secret{
Target: k,
Source: v.Secret,
})
}
p.Services = append(p.Services, &transform.Service{
Name: service.Name,
Image: service.Image,
Pull: service.Pull == "always",
Environment: env,
Secrets: secrets,
Commands: append(service.Entrypoint, service.Commands...),
})
}
// Steps
for _, step := range pipeline.Steps {
if len(step.DependsOn) > 0 {

View File

@ -42,6 +42,12 @@ func TestTransformSimple(t *testing.T) {
pipeline := getPipelineByName(pipelines, "build")
require.NotNil(t, pipeline, "build pipeline not found")
require.Len(t, pipeline.Services, 1)
assert.Equal(t, "database", pipeline.Services[0].Name)
assert.Equal(t, "postgres:latest", pipeline.Services[0].Image)
assert.True(t, pipeline.Services[0].Pull)
assert.Equal(t, []string{"docker-entrypoint.sh postgres"}, pipeline.Services[0].Commands)
assert.Equal(t, "linux/amd64", pipeline.Platform)
assert.Equal(t, map[string]string{"location": "europe"}, pipeline.Labels)

View File

@ -15,17 +15,6 @@ type Platform struct {
Version string `yaml:"version"`
}
type StepWhen struct {
Branch *Conditions `yaml:"branch"`
Event core.Strings `yaml:"event"`
Refs *Conditions `yaml:"refs"`
Repositories *Conditions `yaml:"repo"`
Instance *Conditions `yaml:"instance"`
Status core.Strings `yaml:"status"`
Target *Conditions `yaml:"target"`
Cron *Conditions `yaml:"cron"`
}
type Step struct {
Name string `yaml:"name"`
Image string `yaml:"image"`
@ -37,7 +26,18 @@ type Step struct {
Detach bool `yaml:"detach"`
Privileged bool `yaml:"privileged"`
Failure string `yaml:"failure"`
When *StepWhen `yaml:"when"`
When *When `yaml:"when"`
}
type Service struct {
Name string `yaml:"name"`
Image string `yaml:"image"`
Pull string `yaml:"pull"`
Environment core.MapOrEnvArray `yaml:"environment"`
Commands []string `yaml:"commands"`
Entrypoint []string `yaml:"entrypoint"`
Privileged bool `yaml:"privileged"`
WorkingDir string `yaml:"working_dir"`
}
type Pipeline struct {
@ -46,6 +46,7 @@ type Pipeline struct {
Name string `yaml:"name"`
Platform *Platform `yaml:"platform"`
Node map[string]string `yaml:"node"`
Services []*Service `yaml:"services"`
Steps []*Step `yaml:"steps"`
DependsOn []string `yaml:"depends_on"`
}

View File

@ -10,6 +10,13 @@ platform:
node:
location: europe
services:
- name: database
image: postgres:latest
pull: always
commands:
- "docker-entrypoint.sh postgres"
steps:
- name: test
image: golang:1.18

View File

@ -18,7 +18,7 @@ func FormatWoodpeckerYAML(data []byte) ([]byte, error) {
var i int
for scanner.Scan() {
line := scanner.Text()
if line == "pipeline:" {
if line == "pipeline:" || line == "services:" {
isPipeline = true
if i > 0 {
out.WriteByte('\n')

View File

@ -7,50 +7,22 @@ package transform
import (
"errors"
"codeberg.org/lafriks/woodpecker-pipeline-transform/core"
"github.com/goccy/go-yaml"
)
var UnsupportedError = errors.New("unsupported pipeline")
type StepWhen struct {
Repo core.Strings `yaml:"repo,omitempty"`
Branch *Conditions `yaml:"branch,omitempty"`
Event core.Strings `yaml:"event,omitempty"`
Tag string `yaml:"tag,omitempty"`
Status []string `yaml:"status,omitempty"`
Platform core.Strings `yaml:"platform,omitempty"`
Environment string `yaml:"environment,omitempty"`
Matrix map[string]string `yaml:"matrix,omitempty"`
Instance string `yaml:"instance,omitempty"`
Path *PathConditions `yaml:"path,omitempty"`
}
func (s StepWhen) IsEmpty() bool {
return len(s.Repo) == 0 &&
s.Branch.IsEmpty() &&
len(s.Event) == 0 &&
s.Tag == "" &&
len(s.Status) == 0 &&
len(s.Platform) == 0 &&
s.Environment == "" &&
len(s.Matrix) == 0 &&
s.Instance == "" &&
s.Path.IsEmpty()
}
type Step struct {
Name string `yaml:"-"`
Image string `yaml:"image"`
Pull bool `yaml:"pull,omitempty"`
Environment []string `yaml:"environment,omitempty"`
Secrets Secrets `yaml:"secrets,omitempty"`
Commands []string `yaml:"commands,omitempty"`
Settings Settings `yaml:"settings,omitempty"`
Detach bool `yaml:"detach,omitempty"`
Privileged bool `yaml:"privileged,omitempty"`
When *StepWhen `yaml:"when,omitempty"`
Name string `yaml:"-"`
Image string `yaml:"image"`
Pull bool `yaml:"pull,omitempty"`
Environment []string `yaml:"environment,omitempty"`
Secrets Secrets `yaml:"secrets,omitempty"`
Commands []string `yaml:"commands,omitempty"`
Settings Settings `yaml:"settings,omitempty"`
Detach bool `yaml:"detach,omitempty"`
Privileged bool `yaml:"privileged,omitempty"`
When *When `yaml:"when,omitempty"`
}
type Steps []*Step
@ -66,6 +38,30 @@ func (s Steps) MarshalYAML() (interface{}, error) {
return v, nil
}
type Service struct {
Name string `yaml:"-"`
Image string `yaml:"image"`
Pull bool `yaml:"pull,omitempty"`
Environment []string `yaml:"environment,omitempty"`
Secrets Secrets `yaml:"secrets,omitempty"`
Commands []string `yaml:"commands,omitempty"`
Settings Settings `yaml:"settings,omitempty"`
When *When `yaml:"when,omitempty"`
}
type Services []*Service
func (s Services) MarshalYAML() (interface{}, error) {
v := make(yaml.MapSlice, len(s))
for i, step := range s {
v[i] = yaml.MapItem{
Key: step.Name,
Value: step,
}
}
return v, nil
}
type Workspace struct {
Base string `yaml:"base"`
Path string `yaml:"path"`
@ -76,6 +72,7 @@ type Pipeline struct {
Workspace *Workspace `yaml:"workspace,omitempty"`
Platform string `yaml:"platform,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Services Services `yaml:"services,omitempty"`
Steps Steps `yaml:"pipeline"`
DependsOn []string `yaml:"depends_on,omitempty"`
}

View File

@ -20,7 +20,7 @@ func TestPipelineUnmarshal(t *testing.T) {
Name: "step1",
Image: "alpine:latest",
Commands: []string{"echo Step 1", "echo $${CI_JOB_ID}"},
When: &transform.StepWhen{
When: &transform.When{
Event: []string{"push", "pull_request"},
Branch: &transform.Conditions{
Conditions: []string{"main"},