microsoft / PSRule-pipelines

Validate infrastructure as code (IaC) and DevOps repositories using Azure Pipelines.
https://marketplace.visualstudio.com/items?itemName=bewhite.ps-rule
MIT License
80 stars 22 forks source link

Task fails on self-hosted agent when PSRule and/or PSRule.Rules.Azure already installed #477

Open silverl opened 2 years ago

silverl commented 2 years ago

Description of the issue

My tasks were working fine in the Azure DevOps hosted agent.

I just installed the self-hosted agent locally on Windows 11 x64. I already have PSRule 2.2.0 and PSRule.Rules.Azure 1.17.1 installed.

The self-hosted agent runs as the NETWORK SERVICE user.

It looks like it might be trying to uninstall or reinstall the modules, and it's erroring out.

Configuration

Yaml configuration of the extension.

      - task: ps-rule-install@2
        displayName: Install PSRule.Rules.Azure PS module
        condition: and(succeeded(), eq('${{parameters.runPSRuleTests}}', true))
        inputs:
          module: PSRule.Rules.Azure
          latest: false
          prerelease: false

Expected behaviour

Expected it to work as it did in a hosted agent.

Error output

Starting: Install PSRule.Rules.Azure PS module
==============================================================================
Task         : Install PSRule module
Description  : Install a PowerShell module containing rules.
Version      : 1.0.2207007
Author       : Bernie White
Help         : https://github.com/Microsoft/PSRule-pipelines/blob/main/docs/tasks.md#ps-rule-install
==============================================================================

[info] Using PreRelease: False
[info] Checking PowerShellGet
[info] Checking module: PSRule.Rules.Azure
[info] Installing module: PSRule.Rules.Azure
##[warning]The version '2.2.0' of module 'PSRule' is currently in use. Retry the operation after closing the applications.
##[warning]The version '1.17.1' of module 'PSRule.Rules.Azure' is currently in use. Retry the operation after closing the applications.
##[error]No match was found for the specified search criteria and module names 'PSRule.Rules.Azure'.
Finishing: Install PSRule.Rules.Azure PS module

Task in use and version:

Additional context

I tried commenting out the ps-rule-install task, as I already have the modules installed locally.

The next task, ps-rule-assert, failed in a similar manner as it too attempted to install the modules.

I next attempted to uninstall the modules from my machine to see if I could progress. I was blocked again with a similar message that they were in use.

I closed all terminals and powershell sessions and stopped VSCode (in which I have the PSRule extension installed). This seemed to allow me to uninstall the modules and run the job again.

This time I'm getting the following:

Starting: Run PSRule validation tests on Azure template files
==============================================================================
Task         : PSRule analysis
Description  : Run analysis with PSRule.
Version      : 2.0.2207007
Author       : Bernie White
Help         : https://github.com/Microsoft/PSRule-pipelines/blob/main/docs/tasks.md#ps-rule-assert
==============================================================================
[info] Source 'C:\agent\_work\1\s\.ps-rule\' does not exist.

Checking PSRule
[info] Using repository: PSGallery
##[error]The 'Get-InstalledModule' command was found in the module 'PowerShellGet', but the module could not be loaded. For more information, run 'Import-Module PowerShellGet'.
Finishing: Run PSRule validation tests on Azure template file
silverl commented 2 years ago

Also installed locally:

❯ Get-InstalledModule PackageManagement

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
1.4.8.1              PackageManagement                   PSGallery            PackageManagement (a.k.a. OneGet) is a new way to discover and install software packages from around the web.…

   lsilverman@dev-lsilverman  ~ 
❯ Get-InstalledModule powershellget

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
2.2.5                PowerShellGet                       PSGallery            PowerShell module with commands for discovering, installing, updating and publishing the PowerShell artifacts like Modules, DSC Resources, Role Capabilities and Scripts.
silverl commented 2 years ago

I threw in a task before ps-rule-assert@2

      - task: AzurePowerShell@5
        displayName: "Import-Module PowerShellGet"
        condition: succeeded()
        inputs:
          azureSubscription: ${{parameters.subscriptionId}}
          scriptType: inlineScript
          errorActionPreference: 'Stop'
          targetAzurePs: latestVersion
          pwsh: true
          inline: |
            Import-Module PowerShellGet -Verbose
            Get-InstalledModule PowerShellGet

The output:


VERBOSE: Populating RepositorySourceLocation property for module PowerShellGet.
VERBOSE: Loading module from path 'C:\Program Files\PowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1'.
VERBOSE: Importing function 'Find-Command'.
VERBOSE: Importing function 'Find-DscResource'.
VERBOSE: Importing function 'Find-Module'.
VERBOSE: Importing function 'Find-RoleCapability'.
VERBOSE: Importing function 'Find-Script'.
VERBOSE: Importing function 'Get-CredsFromCredentialProvider'.
VERBOSE: Importing function 'Get-InstalledModule'.
VERBOSE: Importing function 'Get-InstalledScript'.
VERBOSE: Importing function 'Get-PSRepository'.
VERBOSE: Importing function 'Install-Module'.
VERBOSE: Importing function 'Install-Script'.
VERBOSE: Importing function 'New-ScriptFileInfo'.
VERBOSE: Importing function 'Publish-Module'.
VERBOSE: Importing function 'Publish-Script'.
VERBOSE: Importing function 'Register-PSRepository'.
VERBOSE: Importing function 'Save-Module'.
VERBOSE: Importing function 'Save-Script'.
VERBOSE: Importing function 'Set-PSRepository'.
VERBOSE: Importing function 'Test-ScriptFileInfo'.
VERBOSE: Importing function 'Uninstall-Module'.
VERBOSE: Importing function 'Uninstall-Script'.
VERBOSE: Importing function 'Unregister-PSRepository'.
VERBOSE: Importing function 'Update-Module'.
VERBOSE: Importing function 'Update-ModuleManifest'.
VERBOSE: Importing function 'Update-Script'.
VERBOSE: Importing function 'Update-ScriptFileInfo'.
VERBOSE: Importing variable 'PSGetPath'.
VERBOSE: Importing alias 'fimo'.
VERBOSE: Importing alias 'inmo'.
VERBOSE: Importing alias 'pumo'.
VERBOSE: Importing alias 'upmo'.

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
2.2.5                PowerShellGet                       PSGallery            PowerShell module with commands for disc…

Added TLS 1.2 in session.
Disconnect-AzureRmAccount -Scope Process -ErrorAction Stop
Clear-AzureRmContext -Scope Process -ErrorAction Stop
Finishing: Import-Module PowerShellGet
BernieWhite commented 2 years ago

@silverl Thanks for reporting the issue.

The task currently ships with support for PSRule v2.1.0. And will try to install this version.

To use PSRule v2.2.0.

Set the version: property to 2.2.0.

We can probably do better to provide an additional switch to accept newer versions. Thanks for the feedback.

If this doesn't solve the issue, let us know.

Otherwise let's keep the issue open and I'll look at providing an addition option to avoid pinning the version.