Skip to content

Correct way to work with default values when using PSDocs and PSRule #221

@teemukom

Description

@teemukom

Description of the issue

Not sure should this be an issue on PSRule or PSDocs side but I'm testing out both PSRule and PSDocs but have come to an issue how to deal with parameter default values. I'm using Bicep with PSRule and before used in PSDocs it's built to JSON. I have a separate parameters file.

For an example I have parameter adminUsername which is set in the Bicep file like this:

@description('Username for the Virtual Machine.')
param adminUsername string = 'placeholder'

And in the parameters file:

      "adminUsername": {
        "value": "adminUser"
      },

This way PSRule works ok but in the PSDocs stage this parameter is set as Optional

If I remove the default value from the Bicep and set it like this:

@description('Username for the Virtual Machine.')
param adminUsername string

The PSRule fails with error

##[error]Failed to expand bicep source '/home/vsts/work/1/s/main.bicep'. Exception calling "GetBicepResources" with "3" argument(s): "Unable to expand resources because the source file '/home/vsts/work/1/s/main.bicep' was not valid. An error occurred evaluating expression '[parameters('adminUsername')]' line 448. The parameter named 'adminUsername' was not set or a defaultValue was defined."

So what would be the correct way to set these values for both PSRule and PSDocs to work?
To reproduce

Steps to reproduce the issue:

  • Remove default value from the Bicep template

Expected behaviour

  • Value should be coming from the parameters file and set as required in the PSDocs.

Error output

##[error]Failed to expand bicep source '/home/vsts/work/1/s/main.bicep'. Exception calling "GetBicepResources" with "3" argument(s): "Unable to expand resources because the source file '/home/vsts/work/1/s/main.bicep' was not valid. An error occurred evaluating expression '[parameters('adminUsername')]' line 448. The parameter named 'adminUsername' was not set or a defaultValue was defined."

Module in use and version:

  • Module: PSDocs.Azure
  • Version: 0.3.0]

Captured output from $PSVersionTable:

Name                           Value
----                           -----
PSVersion                      7.2.7
PSEdition                      Core
GitCommitId                    7.2.7
OS                             Linux 5.15.0-1024-azure #30-Ubuntu SMP Wed Nov …
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions