Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
o-l-a-v opened this issue May 7, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@o-l-a-v
Copy link

o-l-a-v commented May 7, 2024

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

@o-l-a-v o-l-a-v added the enhancement New feature or request label May 7, 2024
@o-l-a-v o-l-a-v changed the title Switch to using PSResourceGet for module installation Switch to using PSResourceGet for module installation + add PSGallery as repository if not present May 7, 2024
@o-l-a-v
Copy link
Author

o-l-a-v commented May 7, 2024

We added this to our workflow before running PSRule.

      - name: Install required PowerShell modules
        continue-on-error: false
        shell: pwsh
        run: |
          # PowerShell preferences
          $ErrorActionPreference = 'Stop'
          $InformationPreference = 'Continue'
          # Import PSResourceGet
          Write-Information -MessageData 'Import module "Microsoft.PowerShell.PSResourceGet".'
          $null = Import-Module -Name 'Microsoft.PowerShell.PSResourceGet'
          # Add PSGallery if not present
          Write-Information -MessageData 'Add PSGallery as repository if not already present.'
          if ((Get-PSRepository -WarningAction 'SilentlyContinue').'Name' -notcontains 'PSGallery') {
              Write-Information -MessageData '  Was not already present, adding it now.'
              Register-PSRepository -Default
          }
          # Install latest version of required modules
          Write-Information -MessageData 'Install modules "PSRule" and "PSRule.Rules.Azure".'
          $null = Install-PSResource -Name 'PSRule', 'PSRule.Rules.Azure' -Scope 'CurrentUser' -Repository 'PSGallery' -TrustRepository -SkipDependencyCheck

@BernieWhite
Copy link
Member

Thanks for reporting the issue @o-l-a-v.

  • Is this a private/ self-hosted runner or a GitHub hosted runner?
  • Are you able to details from the Set up job section, including runner version, operating system, and runner image?

@o-l-a-v
Copy link
Author

o-l-a-v commented May 7, 2024

Public GitHub action worker.

Set up job section from the run that failed:

Current runner version: '2.316.0'
Operating System
  Ubuntu
  22.04.4
  LTS
Runner Image
  Image: ubuntu-22.04
  Version: 20240422.1.0
  Included Software: https://github.com/actions/runner-images/blob/ubuntu22/20240422.1/images/ubuntu/Ubuntu2204-Readme.md
  Image Release: https://github.com/actions/runner-images/releases/tag/ubuntu22%2F20240422.1
Runner Image Provisioner
  2.0.369.1
GITHUB_TOKEN Permissions
  Actions: read
  Contents: read
  Metadata: read
  SecurityEvents: write
Secret source: Actions
Prepare workflow directory
Prepare all required actions
Getting action download info
Download action repository 'actions/checkout@v4' (SHA:0ad4b8fadaa221de15dcec353f45205ec38ea70b)
Download action repository 'microsoft/[email protected]' (SHA:46451b8f5258c41beb5ae69ed7190ccbba84112c)
Download action repository 'github/codeql-action@v3' (SHA:d39d31e687223d841ef683f52467bd88e9b21c14)
Uses: <repo>/.github/workflows/ps-rule-reusable-workflow.yml@refs/heads/main (9b8d9b51e947c03f268f662da22f9d0f7687b85b)
Complete job name: ps-rule-job / Analyze Azure template files

This job runs on multiple repos once a week, have never seen that specific error before. So looks like a one off. That maybe could be guarded against by adding PSGallery if not already added as module repository.

@BernieWhite
Copy link
Member

@o-l-a-v Thanks for the additional information.

@BernieWhite BernieWhite added bug Something isn't working and removed enhancement New feature or request labels May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants