Using Go to create BitBucket Pipeline YAML with anchors and merging - Stack Overflow

I am trying to use the githubgoccygo-yaml Go YAML library to write BitBucket Pipeline files. The comp

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 the pipeline.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 the pipeline.tags block
Share Improve this question edited Nov 19, 2024 at 17:42 richid asked Nov 19, 2024 at 17:31 richidrichid 6493 gold badges8 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It 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))
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742410124a4438644.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信