admin管理员组文章数量:1336631
I am trying to use the github/goccy/go-yaml
Go YAML library to write BitBucket Pipeline files. The complicating fact is that I'm trying to use YAML's merge and anchor features to reuse common steps.
A condensed version of the output I'm looking for:
image: ubuntu:latest
definitions:
steps:
- step: &run-task
name: Run a task
oidc: true
runs-on:
- self.hosted
script:
- sbt test
pipelines:
tags:
'*.*.*':
- step:
<<: *run-task
name: Release
script:
- sbt publish
Here is the current struct/logic:
package main
import (
"fmt"
"github/goccy/go-yaml"
)
type Pipeline struct {
Image string `yaml:"image"`
Definitions Definitions `yaml:"definitions"`
Pipelines Pipelines `yaml:"pipelines"`
}
type Pipelines struct {
Tags *Tag `yaml:"tags"`
}
type Definitions struct {
Steps []*StepTemplate `yaml:"steps"`
}
type StepTemplate struct {
*StepTemplate `yaml:"step,anchor=run-task"`
Name string `yaml:"name,omitempty"`
Oidc bool `yaml:"oidc,omitempty"`
RunsOn []string `yaml:"runs-on,omitempty"`
Script []string `yaml:"script,omitempty"`
}
type StepReal struct {
*StepReal `yaml:"step,alias=run-task"`
Name string `yaml:"name,omitempty"`
Oidc bool `yaml:"oidc,omitempty"`
RunsOn []string `yaml:"runs-on,omitempty"`
Script []string `yaml:"script,omitempty"`
}
type Tag map[string][]*StepReal
func main() {
defaultStep := &StepTemplate{
Name: "Run step",
Oidc: true,
RunsOn: []string{"self.hosted", "default"},
Script: []string{"sbt test"},
}
releaseStep := StepReal{
//Step: defaultStep,
Name: "Release",
Script: []string{"sbt publish"},
}
tagSteps := &Tag{
"*.*.*": []*StepReal{&releaseStep},
}
pipe := Pipeline{
Image: "ubuntu:latest",
Definitions: Definitions{
Steps: []*StepTemplate{defaultStep},
},
Pipelines: Pipelines{
Tags: tagSteps,
},
}
bytes, _ := yaml.MarshalWithOptions(pipe, yaml.IndentSequence(true))
fmt.Println(string(bytes))
}
Go Playground
I think I'm pretty close but I cannot figure out a couple of things:
- How to remove the trailing
null
in the&run-task
anchor - How to actually output the
<<: *run-task
merge in thepipeline.tags
block
I am trying to use the github/goccy/go-yaml
Go YAML library to write BitBucket Pipeline files. The complicating fact is that I'm trying to use YAML's merge and anchor features to reuse common steps.
A condensed version of the output I'm looking for:
image: ubuntu:latest
definitions:
steps:
- step: &run-task
name: Run a task
oidc: true
runs-on:
- self.hosted
script:
- sbt test
pipelines:
tags:
'*.*.*':
- step:
<<: *run-task
name: Release
script:
- sbt publish
Here is the current struct/logic:
package main
import (
"fmt"
"github/goccy/go-yaml"
)
type Pipeline struct {
Image string `yaml:"image"`
Definitions Definitions `yaml:"definitions"`
Pipelines Pipelines `yaml:"pipelines"`
}
type Pipelines struct {
Tags *Tag `yaml:"tags"`
}
type Definitions struct {
Steps []*StepTemplate `yaml:"steps"`
}
type StepTemplate struct {
*StepTemplate `yaml:"step,anchor=run-task"`
Name string `yaml:"name,omitempty"`
Oidc bool `yaml:"oidc,omitempty"`
RunsOn []string `yaml:"runs-on,omitempty"`
Script []string `yaml:"script,omitempty"`
}
type StepReal struct {
*StepReal `yaml:"step,alias=run-task"`
Name string `yaml:"name,omitempty"`
Oidc bool `yaml:"oidc,omitempty"`
RunsOn []string `yaml:"runs-on,omitempty"`
Script []string `yaml:"script,omitempty"`
}
type Tag map[string][]*StepReal
func main() {
defaultStep := &StepTemplate{
Name: "Run step",
Oidc: true,
RunsOn: []string{"self.hosted", "default"},
Script: []string{"sbt test"},
}
releaseStep := StepReal{
//Step: defaultStep,
Name: "Release",
Script: []string{"sbt publish"},
}
tagSteps := &Tag{
"*.*.*": []*StepReal{&releaseStep},
}
pipe := Pipeline{
Image: "ubuntu:latest",
Definitions: Definitions{
Steps: []*StepTemplate{defaultStep},
},
Pipelines: Pipelines{
Tags: tagSteps,
},
}
bytes, _ := yaml.MarshalWithOptions(pipe, yaml.IndentSequence(true))
fmt.Println(string(bytes))
}
Go Playground
I think I'm pretty close but I cannot figure out a couple of things:
- How to remove the trailing
null
in the&run-task
anchor - How to actually output the
<<: *run-task
merge in thepipeline.tags
block
1 Answer
Reset to default 1It took me some time to figure out but maybe is this what you're looking for:
package main
import (
"fmt"
"github/goccy/go-yaml"
)
type Pipeline struct {
Image string `yaml:"image"`
Definitions Definitions `yaml:"definitions"`
Pipelines Pipelines `yaml:"pipelines"`
}
type Pipelines struct {
Tags *Tag `yaml:"tags"`
}
type Definitions struct {
Steps []*StepTemplate `yaml:"steps"`
}
type Step struct {
*Step `yaml:",omitempty,inline,alias"`
Name string `yaml:"name,omitempty"`
Oidc bool `yaml:"oidc,omitempty"`
RunsOn []string `yaml:"runs-on,omitempty"`
Script []string `yaml:"script,omitempty"`
}
type StepTemplate struct {
*Step `yaml:"step,anchor=run-task"`
}
type StepReal struct {
*Step
}
type Tag map[string][]*StepReal
func main() {
defaultStep := &StepTemplate{
&Step{
Name: "Run a task",
Oidc: true,
RunsOn: []string{"self.hosted"},
Script: []string{"sbt test"},
},
}
releaseStep := StepReal{
&Step{
Step: defaultStep.Step,
Name: "Release",
Script: []string{"sbt publish"},
},
}
tagSteps := &Tag{
"*.*.*": []*StepReal{&releaseStep},
}
pipe := Pipeline{
Image: "ubuntu:latest",
Definitions: Definitions{
Steps: []*StepTemplate{defaultStep},
},
Pipelines: Pipelines{
Tags: tagSteps,
},
}
bytes, err := yaml.MarshalWithOptions(pipe, yaml.IndentSequence(true))
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
本文标签: Using Go to create BitBucket Pipeline YAML with anchors and mergingStack Overflow
版权声明:本文标题:Using Go to create BitBucket Pipeline YAML with anchors and merging - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410124a2469618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论