microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
.github/workflows/ps-script-analyzer.yml
72lines · modecode
| 1 | name: PSScriptAnalyzer |
| 2 | |
| 3 | on: |
| 4 | workflow_call: |
| 5 | inputs: |
| 6 | soft-fail: |
| 7 | description: 'Whether to continue on PSScriptAnalyzer violations' |
| 8 | required: false |
| 9 | type: boolean |
| 10 | default: false |
| 11 | changed-files-only: |
| 12 | description: 'Only analyze changed PowerShell files' |
| 13 | required: false |
| 14 | type: boolean |
| 15 | default: true |
| 16 | |
| 17 | permissions: |
| 18 | contents: read |
| 19 | |
| 20 | jobs: |
| 21 | psscriptanalyzer: |
| 22 | name: PowerShell Lint |
| 23 | runs-on: ubuntu-latest |
| 24 | permissions: |
| 25 | contents: read |
| 26 | steps: |
| 27 | - name: Harden Runner |
| 28 | uses: step-security/harden-runner@95d9a5deda9de15063e7595e9719c11c38c90ae2 # v2.10.2 |
| 29 | with: |
| 30 | egress-policy: audit |
| 31 | |
| 32 | - name: Checkout code |
| 33 | uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v4.2.2 |
| 34 | with: |
| 35 | persist-credentials: false |
| 36 | fetch-depth: 0 |
| 37 | |
| 38 | - name: Run PSScriptAnalyzer |
| 39 | id: analyze |
| 40 | shell: pwsh |
| 41 | run: | |
| 42 | New-Item -ItemType Directory -Force -Path logs | Out-Null |
| 43 | $params = @{} |
| 44 | if ('${{ inputs.changed-files-only }}' -eq 'true') { |
| 45 | $params['ChangedFilesOnly'] = $true |
| 46 | } |
| 47 | |
| 48 | & scripts/linting/Invoke-PSScriptAnalyzer.ps1 @params |
| 49 | if ($LASTEXITCODE -ne 0) { |
| 50 | "PSSCRIPTANALYZER_FAILED=true" | Out-File -FilePath $env:GITHUB_ENV -Append |
| 51 | } |
| 52 | continue-on-error: ${{ inputs.soft-fail }} |
| 53 | |
| 54 | - name: Upload results |
| 55 | if: always() |
| 56 | uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v4.4.3 |
| 57 | with: |
| 58 | name: psscriptanalyzer-results |
| 59 | path: | |
| 60 | logs/psscriptanalyzer-results.json |
| 61 | logs/psscriptanalyzer-summary.json |
| 62 | retention-days: 30 |
| 63 | if-no-files-found: ignore |
| 64 | |
| 65 | - name: Check results |
| 66 | if: "!inputs.soft-fail" |
| 67 | shell: pwsh |
| 68 | run: | |
| 69 | if ($env:PSSCRIPTANALYZER_FAILED -eq 'true') { |
| 70 | Write-Error "PSScriptAnalyzer found violations" |
| 71 | exit 1 |
| 72 | } |
| 73 | |