You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Go的goccy/go-yaml库生成带锚点与合并语法的BitBucket Pipeline YAML遇到的问题

使用Go的goccy/go-yaml库生成带锚点与合并语法的BitBucket Pipeline YAML遇到的问题

我现在正尝试用github.com/goccy/go-yaml这个Go的YAML库来编写BitBucket Pipeline文件,麻烦的是我想用到YAML的锚点和合并特性来复用通用步骤。

我想要的简化版输出是这样的:

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

这是我目前写的结构体和逻辑:

package main

import (
    "fmt"

    "github.com/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))
}

我感觉已经快搞定了,但还有两个问题搞不清楚:

  • 怎么去掉&run-task锚点后面的null
  • 怎么在pipeline.tags块里输出<<: *run-task这个合并语法

备注:内容来源于stack exchange,提问作者richid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:34:52