---
title: "Resolve CircleCI YAML anchors in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/resolve-anchors-in-circleci-config/
description: "Resolve CircleCI YAML anchors and merge keys in C# with OneYaml. Inspect inherited job settings locally without running or validating a CircleCI pipeline."
version: "1.2.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/resolve-anchors-in-circleci-config/resolve-anchors-in-circleci-config.zip)

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.

```shell
dotnet add package OneYaml
```

The library targets .NET 8 and later; the example project uses [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).

```csharp
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](https://recursiveai.org/oneyaml/csharp/cookbook/resolve-anchors-in-circleci-config/resolve-anchors-in-circleci-config.zip) 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.

```csharp
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.

## Related recipes

- [Read YAML merge keys in C#](https://recursiveai.org/oneyaml/csharp/cookbook/anchors-aliases-merge-keys/)
- [Read YAML without a class](https://recursiveai.org/oneyaml/csharp/cookbook/read-yaml-without-a-class/)

---

## 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](https://circleci.com/docs/guides/getting-started/introduction-to-yaml-configurations/#anchors-and-aliases).

## 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](https://yaml.org/type/merge.html), 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](https://circleci.com/docs/guides/orchestrate/pipeline-variables/). 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.
