Recursive AI

Resolve CircleCI YAML anchors in C#

OneYaml is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

See which job settings are inherited without validating the workflow. &build names a YAML node, *build refers to it, and <<: *build copies its keys into this job.

Terminal
dotnet add package OneYaml

The library targets .NET 8 and later; the example project uses .NET 10.

C#
using Options = OneYaml.YamlSerializerOptions;
using static OneYaml.YamlSerializer;
var yaml = File.ReadAllText("config.yml");
var config = Deserialize<object>(yaml,
    new Options { MergeKeys = true });
Console.Write(Serialize(config));

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Use the sample config.yml below (included in the zip), or copy your .circleci/config.yml next to Program.cs. The snippet prints expanded YAML without changing the file. Here’s the test job in the output:

YAML
  test:
    docker:
      - image: cimg/base:stable
    working_directory: ~/tests
    steps:
      - run: pwd

config.yml

YAML
version: 2.1
jobs:
  build: &build
    docker:
      - image: cimg/base:stable
    working_directory: ~/project
    steps:
      - run: pwd
  test:
    <<: *build
    working_directory: ~/tests
workflows:
  checks:
    jobs:
      - build
      - test

The full program

The zip reads the same YAML into C# records and prints the image and working directory for each job. It doesn’t run pwd or start a container.

C#
using OneYaml;
 
var yaml = File.ReadAllText("config.yml");
var options = new YamlSerializerOptions
{
    MergeKeys = true
};
var config = YamlSerializer
    .Deserialize<Config>(yaml, options)!;
foreach (var name in new[] { "build", "test" })
{
    var job = config.Jobs[name];
    var image = job.Docker[0].Image;
    Console.WriteLine(
        $"{name}: {image}, {job.Directory}");
}
 
record Config(Dictionary<string, Job> Jobs);
record Job(List<Container> Docker,
    [property: YamlPropertyName(
        "working_directory")]
    string Directory);
record Container(string Image);
Text
build: cimg/base:stable, ~/project
test: cimg/base:stable, ~/tests

If you misspell *build or put the alias before its anchor, OneYaml throws a YamlException with the line, column, and YAML path.

Variations

Use a different container image in the test job. Replace the test job in config.yml with this mapping:

YAML
  test:
    <<: *build
    working_directory: ~/tests
    docker:
      - image: cimg/base:current

The program now prints test: cimg/base:current, ~/tests. The explicit docker key replaces the whole list, not just its first image.


The short answer

To inspect CircleCI YAML anchors locally, deserialize the config with OneYaml’s MergeKeys = true. The test job in this example gets docker and steps from the &build mapping but keeps its own working_directory. Print the expanded mappings to see what the YAML says, or bind only the settings you want to inspect into records as the downloadable example does.

How do YAML anchors and aliases work in CircleCI?

In .circleci/config.yml, &build gives a YAML node a name. *build refers to that node. A direct alias in place of a job mapping reuses the whole mapping as written. <<: *build goes inside a new mapping, so it can inherit keys and define keys of its own. Neither syntax invokes the build job. Which jobs run is a separate question for the workflows section. See CircleCI’s YAML configuration guide.

Can you override a YAML anchor or merge two lists?

You don’t change the anchor by setting a key in the receiving mapping. With <<, an explicit key in that mapping wins over the same key in the anchor. That’s how test keeps ~/tests without changing build’s ~/project. << merges mappings, not the lists they contain. If a job supplies its own steps, it replaces the inherited sequence; it doesn’t add steps to the end. The variation shows the same rule with docker.

OneYaml keeps merging off by default: << is a YAML 1.1 merge key, not part of YAML 1.2’s core schema. Without MergeKeys = true, OneYaml reads << as an ordinary key rather than filling in missing settings from the source mapping. If you want to keep the anchors and comments in the original file, don’t write the expanded output back over it.

Does resolving anchors also resolve CircleCI pipeline parameters?

No. This is a read of the YAML file, not CircleCI’s evaluation of it. For instance, <<: *build is a YAML merge, while << pipeline.parameters.name >> is CircleCI parameter syntax. OneYaml doesn’t substitute those parameters, expand orbs, or decide whether a workflow will run. Use the output to answer questions about inherited YAML values, not whether the config is ready to run on CircleCI.

All oneyaml C# recipes