sharpliner / sharpliner

Use C# instead of YAML to define your Azure DevOps pipelines
https://www.nuget.org/packages/Sharpliner/
MIT License
285 stars 21 forks source link

Validate used parameters in template definitions #221

Open premun opened 1 year ago

premun commented 1 year ago

When we define a template, we specify the parameters it has. Inside of the templates, we use the parameters[] notation to reference these. We can possibly validate that each referenced parameter has been defined.

Example:

class InstallDotNetTemplate : StepTemplateDefinition
{
    public override string TargetFile => "templates/build-csproj.yml";

    public override List<TemplateParameter> Parameters => new()
    {
        StringParameter("project"),
        StringParameter("version", allowedValues: new[] { "5.0.100", "6.0.302" }),
    };

    public override ConditionedList<Step> Definition => new()
    {
        DotNet.Install.Sdk(parameters["version"]),

        If.Equal(parameters["restore"], "true")                <--- Exception "Parameter `restore` not specified"
            .Step(DotNet.Restore.Projects(parameters["project"])),

        DotNet.Build(parameters["project"]),
    };
}