microsoft / ps-rule

Validate infrastructure as code (IaC) and DevOps repositories using GitHub Actions.
https://github.com/marketplace/actions/psrule
MIT License
73 stars 13 forks source link

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

Open o-l-a-v opened 3 months ago

o-l-a-v commented 3 months ago

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.

Relevant code section:

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 commented 3 months ago

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 commented 3 months ago

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

o-l-a-v commented 3 months ago

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/ps-rule@v2.9.0' (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 commented 3 months ago

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