Skip to content

Switch to using PSResourceGet for module installation + add PSGallery as repository if not present #235

@o-l-a-v

Description

@o-l-a-v

Is your feature request related to a problem? Please describe.

Run /home/runner/work/_actions/microsoft/ps-rule/v2.9.0/powershell.ps1 -InputType 'repository' -InputPath '' -Modules 'PSRule.Rules.Azure' -Source '.ps-rule/' -Baseline '' -Conventions '' -Option '' -Outcome '' -OutputFormat 'Sarif' -OutputPath 'results.sarif' -Path '' -PreRelease 'false' -Repository 'PSGallery' -Summary 'true' -Version ''
[info] Using repository: PSGallery
[info] Installing PSRule: 2.9.0
Get-PackageSource: Unable to find repository 'PSGallery'. Use Get-PSRepository to see all
available repositories.
Error: Process completed with exit code 1

Describe the solution you'd like

PSResourceGet is the successor to PowerShellGet and PackageManagement. It's included in the GitHub workers. Use it.

  • Edit: Seems you are using it already through aliases? Better to use the native cmdlets then IMO. :)

Relevant code section:

  • ps-rule/powershell.ps1

    Lines 158 to 206 in 001a0fc

    Write-Host "[info] Using repository: $Repository";
    $installed = @(Get-InstalledModule -Name PSRule @checkParams -ErrorAction Ignore)
    if ($installed.Length -eq 0) {
    Write-Host "[info] Installing PSRule: $($checkParams.RequiredVersion)";
    $Null = Install-Module -Repository $Repository -Name PSRule @checkParams -Scope CurrentUser -Force;
    }
    foreach ($m in $installed) {
    Write-Host "[info] Using existing module $($m.Name): $($m.Version)";
    }
    # Look for existing modules
    Write-Host '';
    $moduleNames = @()
    if (![String]::IsNullOrEmpty($Modules)) {
    $moduleNames = $Modules.Split(',', [System.StringSplitOptions]::RemoveEmptyEntries);
    }
    $moduleParams = @{
    Scope = 'CurrentUser'
    Force = $True
    }
    if ($PreRelease -eq 'true') {
    $moduleParams['AllowPrerelease'] = $True;
    }
    # Install each module if not already installed
    foreach ($m in $moduleNames) {
    $m = $m.Trim();
    Write-Host "> Checking module: $m";
    try {
    if ($Null -eq (Get-InstalledModule -Name $m -ErrorAction Ignore)) {
    Write-Host ' - Installing module';
    $Null = Install-Module -Repository $Repository -Name $m @moduleParams -AllowClobber -ErrorAction Stop;
    }
    else {
    Write-Host ' - Already installed';
    }
    # Check
    if ($Null -eq (Get-InstalledModule -Name $m)) {
    Write-Host " - Failed to install";
    }
    else {
    Write-Host " - Using version: $((Get-InstalledModule -Name $m).Version)";
    }
    }
    catch {
    Write-Host "::error::An error occurred installing a dependency module '$m'. $($_.Exception.Message)";
    $Host.SetShouldExit(1);
    }
    }

As the error shows: Add PSGallery as repository if not already present. It can be done with Register-PSRepository -Default, ref:

Maybe something like this?

if ((Get-PSRepository -WarningAction 'SilentlyContinue').'Name' -notcontains 'PSGallery') {
    Register-PSRepository -Default
}

Describe alternatives you've considered

Install modules in the GitHub action before running the psrule step.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions