Use C# instead of YAML to define your Azure DevOps pipelines
Use C# instead of YAML to define your Azure DevOps pipelines
Sharpliner is a .NET library that lets you use C# for Azure DevOps pipeline definitions instead of YAML. Exchange YAML indentation problems for the type-safe environment of C# and let IntelliSense speed up your work!
All you have to do is reference our NuGet package in your project, override a class with your definition and dotnet build the project! Dead simple!
For more detailed steps, check our documentation.
If you prefer using .NET project templates, you can get started quickly with the following commands:
# Install the Sharpliner.Templates NuGet package
dotnet new install Sharpliner.Templates
# Create a new pipeline project with your preferred .NET version (6.0-11.0, default is 11.0)
dotnet new sharpliner-pipeline -n MyProject.Pipelines -F net11.0
# Navigate to your new project
cd MyProject.Pipelines
# Build the project to generate your first pipeline YAML
dotnet build[!NOTE] The samples produced by the template expect to be built within a git repository when generating the YAMLs. This is configured by the
TargetPathType.RelativeToGitRootsetting which tells Sharpliner where to produce the YAMLs.
This creates a new project with a sample pipeline definition in pipelines/SamplePipeline.cs.
The generated sample-pipeline.yml file will be ready to use in your repository!
Sharpliner includes an agent skill that helps compatible AI
coding agents create and modify pipeline definitions using the public API instead of reverse
engineering the NuGet package. Install the sharpliner skill from this repository or copy its
directory into your repository's .github/skills directory.
…Apart from the obvious benefits of using a static type language with IDE support, not having to have to deal with indentation problems ever again, being able to split the code easily or the ability to generate YAML programmatically, there are several other benefits of using Sharpliner.
One of the best things when using Sharpliner is that you won't have to go to the YAML reference every time you're adding a new piece of your pipeline. Having everything strongly typed will allow your IDE to give you hints all the way!
Imagine you want to install the .NET SDK. For that, Azure Pipelines have the DotNetCoreCLI@2 task.
However, this task's specification is quite long since the task does many things:
…Notice how some of the properties are only valid in a specific combination with another. With Sharpliner, we remove some of this complexity using nice fluent APIs:
DotNet.Install.Sdk(parameters["version"]),
DotNet.Restore.FromFeed("dotnet-7-preview-feed", includeNuGetOrg: false) with
{
ExternalFeedCredentials = "feeds/dotnet-7",
NoCache = true,
RestoreDirectory = ".packages",
},
DotNet.Build("src/MyProject.csproj") with
{
Timeout = TimeSpan.FromMinutes(20)
},Some very common pipeline patterns such as comparing the current branch name or detecting pull requests are very cumbersome to do in YAML (long conditions full of complicated ${{ if }} syntax).
For many of these, we have handy macros so that you get more readable and shorter code.
For example this YAML
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/production') }}:
- name: rg-suffix
value: -pr
- ${{ else }}:
- name: rg-suffix
value: -prodcan become this C#
If.IsBranch("production")
.Variable("rg-suffix", "-pr")
.Else
.Variable("rg-suffix", "-prod")Sharpliner lets you reuse code more easily than YAML templates do. Apart from obvious C# code reuse, you can also define sets of C# building blocks and reuse them in your pipelines:
class ProjectBuildSteps : StepLibrary
{
public override List> Steps =>
[
DotNet.Install.Sdk("6.0.100"),
If.IsBranch("main")
.Step(DotNet.Restore.Projects("src/MyProject.sln")),
DotNet.Build("src/MyProject.sln"),
];
}You can then reference this library in between build steps and it will get expanded into the pipeline's YAML:
new Job("Build")
{
Steps =
{
Script.Inline("echo 'Hello World'"),
StepLibrary(),
Script.Inline("echo 'Goodbye World'"),
}
}More about this feature can be found here (DefinitionLibraries.md).
When you need to add cmd, PowerShell or bash steps into your pipeline, maintaining these bits inside YAML can be error prone.
With Sharpliner you can keep scripts in their own files (.ps1, .sh..) where you get the natural environment you're used to such as syntax highlighting.
Sharpliner gives you APIs to load these at build time and include them inline:
Steps =
{
Bash.FromResourceFile("embedded-script.sh") with
{
DisplayName = "Run post-build clean-up",
Timeout = TimeSpan.FromMinutes(5),
}
}A frequent struggle people have with Azure pipelines is using the right type of variable in the right context.
Be it a ${{ compile time parameter }}, a variable['used in runtime'] or a $(macro) syntax, with Sharpliner you won't have to worry about which one to pick as it understands the context and selects the right one for you.
Your pipeline definition can be validated during publishing and you can uncover issues, such as typos inside dependsOn, you would only find by trying to run the pipeline in CI.
This gives you a faster dev loop and greater productivity.
We are continuously adding new validations as we find new error-prone spots. Each validation can be individually configured/silenced in case you don't wish to take advantage of these:
…If you find a missing feature / API / property / use case, file an issue in the project's repository. We try to be very responsive and for small asks can deliver you a new version very fast.
If you want to start contributing, either you already know about something missing or you can choose from some of the open issues. The task support matrix also lists which of Azure DevOps' built-in tasks have a strongly typed API in Sharpliner and which ones are still missing. We will help you review your first change so that you can continue with something advanced!
Another way to start is to try out Sharpliner to define your own, already existing pipeline. This way you can uncover missing features or you can introduce shortcuts for definitions of build tasks or similar that you use frequently. Contributions like these are also very welcome! In these cases, it is worth starting with describing your intent in an issue first.
Contributions are very welcome and if you find yourself opening the codebase there are couple of things you should know. The repository layout is quite simple:
…Developing is quite easy - open the Sharpliner.slnx solution in VS. However, the solution won't build 100% the first time.
This is because of the Sharpliner.CI project.
This projects uses Sharpliner and defines pipelines for the Sharpliner repository - the YAML is published to eng/pipelines.
This way we test quite many Sharpliner features right in the PR build.
The Sharpliner.CI project expects that a package Sharpliner.43.43.43.nupkg is built locally which it then references it simulating the real usage of Sharpliner from nuget.org.
To build all of the solution 100%, you have to build Sharpliner.CI from console as building inside VS won't work on cold checkout.
This will package Sharpliner.csproj first and produce the 43.43.43 package:
dotnet build eng/Sharpliner.CI/Sharpliner.CI.csprojIf you make changes to the main library and want to test it using Sharpliner.CI, clean and then build the CI project from console:
dotnet clean eng/Sharpliner.CI/Sharpliner.CI.csproj
dotnet build eng/Sharpliner.CI/Sharpliner.CI.csprojThe documentation markdown files are generated using the eng/DocsGenerator tool.
This tool uses snippets from a real C# code to make sure the samples in the docs always build.
To regenerate the documentation files, build and run the tool:
dotnet run --project .\eng\DocsGeneratorThen commit the changed files.
The PR pipeline automatically runs the documentation generator and fails the build if there are any changes, so you don't have to worry about forgetting to do this step.
The public API of Sharpliner is defined in tests\Sharpliner.Tests\PublicApiExport.txt.verified.txt.
Any changes made to the public API of the library then have to be reflected in this file.
This helps the maintainers to be aware of the changes made to the library that could impact compatibility.
No open issues yet, or sync has not completed.