Skip to main content

PowerShell 빌드 및 테스트

CI(연속 통합) 워크플로를 만들어 PowerShell 프로젝트를 빌드하고 테스트할 수 있습니다.

소개

이 가이드에서는 CI용 PowerShell을 사용하는 방법을 보여 줍니다. Pester를 사용하고, 종속성을 설치하고, 모듈을 테스트하고, PowerShell 갤러리 게시하는 방법을 설명합니다.

GitHub 호스트 러너에는 PowerShell 및 Pester가 포함된 소프트웨어가 사전 설치된 도구 캐시가 있습니다.

최신 소프트웨어 및 사전 설치된 버전의 Python과 Pester에 대한 전체 목록은 "GitHub 호스팅 실행기 사용"을(를) 참조하세요.

필수 조건

YAML 및 GitHub Actions의 구문에 대해 잘 알고 있어야 합니다. 자세한 내용은 "GitHub Actions 알아보기"을(를) 참조하세요.

PowerShell 및 Pester를 기본적으로 이해하는 것이 좋습니다. 자세한 내용은 다음을 참조하세요.

Pester에 대한 워크플로 추가

PowerShell 및 Pester를 사용하여 테스트를 자동화하려면 변경 내용이 리포지토리에 푸시될 때마다 실행되는 워크플로를 추가할 수 있습니다. 다음 예제에서 Test-Pathresultsfile.log라는 파일이 있는지 확인하는 데 사용됩니다.

이 예제 워크플로 파일을 리포지토리의 .github/workflows/ 디렉터리에 추가해야 합니다.

name: Test PowerShell on Ubuntu
on: push

jobs:
  pester-test:
    name: Pester test
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Perform a Pester test from the command-line
        shell: pwsh
        run: Test-Path resultsfile.log | Should -Be $true
      - name: Perform a Pester test from the Tests.ps1 file
        shell: pwsh
        run: |
          Invoke-Pester Unit.Tests.ps1 -Passthru
  • shell: pwsh - run 명령을 실행할 때 PowerShell을 사용하도록 작업을 구성합니다.

  • run: Test-Path resultsfile.log - resultsfile.log라는 파일이 리포지토리의 루트 디렉터리에 있는지 확인합니다.

  • Should -Be $true - Pester를 사용하여 예상 결과를 정의합니다. 예기치 않은 결과인 경우 GitHub Actions는 이를 실패한 테스트로 플래그를 지정합니다. 예시:

    Pester 테스트에 대한 워크플로 실행 실패의 스크린샷. 테스트는 "$true를 예상했지만 $false가 나옴" 및 "오류: 종료 코드 1로 완료된 프로세스"를 보고합니다.

  • Invoke-Pester Unit.Tests.ps1 -Passthru - Pester를 사용하여 Unit.Tests.ps1이라는 파일에 정의된 테스트를 실행합니다. 예를 들어 위에서 설명한 것과 동일한 테스트를 수행하기 위해 Unit.Tests.ps1에는 다음이 포함됩니다.

    Describe "Check results file is present" {
        It "Check results file is present" {
            Test-Path resultsfile.log | Should -Be $true
        }
    }
    

PowerShell 모듈 위치

아래 테이블은 각 GitHub 호스트 러너에서 다양한 PowerShell 모듈의 위치를 설명합니다.

UbuntumacOSWindows
PowerShell 시스템 모듈/opt/microsoft/powershell/7/Modules/*/usr/local/microsoft/powershell/7/Modules/*C:\program files\powershell\7\Modules\*
PowerShell 추가 항목 모듈/usr/local/share/powershell/Modules/*/usr/local/share/powershell/Modules/*C:\Modules\*
사용자가 설치한 모듈/home/runner/.local/share/powershell/Modules/*/Users/runner/.local/share/powershell/Modules/*C:\Users\runneradmin\Documents\PowerShell\Modules\*

참고: Ubuntu 실행기에서 Azure PowerShell 모듈은 PowerShell 추가 기능 모듈(예: /usr/local/share/powershell/Modules/)의 기본 위치 대신 /usr/share/에 저장됩니다.

종속성 설치

GitHub 호스트 실행기에는 PowerShell 7 및 Pester가 설치되어 있습니다. 코드를 빌드하고 테스트하기 전에 Install-Module을 사용하여 PowerShell 갤러리에서 추가 종속성을 설치할 수 있습니다.

참고: GitHub 호스트 실행기가 사용하는 사전 설치된 패키지(예: Pester)는 정기적으로 업데이트되며 중요한 변경 내용이 있을 수 있습니다. 따라서 -MaximumVersion과 함께 Install-Module을 사용하여 항상 필요한 패키지 버전을 지정하는 것이 좋습니다.

종속성을 캐시하여 워크플로 속도를 높일 수도 있습니다. 자세한 내용은 "워크플로 속도를 높이기 위한 종속성 캐싱"을(를) 참조하세요.

예를 들어 다음 작업은 SqlServerPSScriptAnalyzer 모듈을 설치합니다.

jobs:
  install-dependencies:
    name: Install dependencies
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install from PSGallery
        shell: pwsh
        run: |
          Set-PSRepository PSGallery -InstallationPolicy Trusted
          Install-Module SqlServer, PSScriptAnalyzer

참고: 기본값으로 PowerShell은 리포지토리를 신뢰하지 않습니다. PowerShell 갤러리에서 모듈을 설치할 때 PSGallery에 대한 설치 정책을 Trusted로 명시적으로 설정해야 합니다.

종속성 캐싱

고유한 키를 사용하여 PowerShell 종속성을 캐시할 수 있으므로 cache 작업으로 향후 워크플로에 대한 종속성을 복원할 수 있습니다. 자세한 내용은 "워크플로 속도를 높이기 위한 종속성 캐싱"을(를) 참조하세요.

PowerShell은 실행기의 운영 체제에 따라 다른 위치에 종속성을 캐시합니다. 예를 들어 다음 Ubuntu 예제에서 사용된 path 위치는 Windows 운영 체제에서는 다릅니다.

steps:
  - uses: actions/checkout@v4
  - name: Setup PowerShell module cache
    id: cacher
    uses: actions/cache@v3
    with:
      path: "~/.local/share/powershell/Modules"
      key: ${{ runner.os }}-SqlServer-PSScriptAnalyzer
  - name: Install required PowerShell modules
    if: steps.cacher.outputs.cache-hit != 'true'
    shell: pwsh
    run: |
      Set-PSRepository PSGallery -InstallationPolicy Trusted
      Install-Module SqlServer, PSScriptAnalyzer -ErrorAction Stop

코드 테스트

코드를 빌드하고 테스트하기 위해 로컬에서 사용하는 것과 동일한 명령을 사용할 수 있습니다.

PSScriptAnalyzer를 사용하여 코드 린트

다음 예제에서는 PSScriptAnalyzer를 설치하고 이를 사용하여 리포지토리의 모든 ps1 파일을 린트합니다. 자세한 내용은 GitHub의 PSScriptAnalyzer를 참조하세요.

  lint-with-PSScriptAnalyzer:
    name: Install and run PSScriptAnalyzer
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install PSScriptAnalyzer module
        shell: pwsh
        run: |
          Set-PSRepository PSGallery -InstallationPolicy Trusted
          Install-Module PSScriptAnalyzer -ErrorAction Stop
      - name: Lint with PSScriptAnalyzer
        shell: pwsh
        run: |
          Invoke-ScriptAnalyzer -Path *.ps1 -Recurse -Outvariable issues
          $errors   = $issues.Where({$_.Severity -eq 'Error'})
          $warnings = $issues.Where({$_.Severity -eq 'Warning'})
          if ($errors) {
              Write-Error "There were $($errors.Count) errors and $($warnings.Count) warnings total." -ErrorAction Stop
          } else {
              Write-Output "There were $($errors.Count) errors and $($warnings.Count) warnings total."
          }

워크플로 데이터를 아티팩트로 패키지

워크플로가 완료된 후 볼 아티팩트를 업로드할 수 있습니다. 예를 들어 로그 파일, 코어 덤프, 테스트 결과 또는 스크린샷을 저장해야 할 수 있습니다. 자세한 내용은 "워크플로 데이터를 아티팩트로 저장"을(를) 참조하세요.

다음 예제에서는 upload-artifact 작업을 사용하여 Invoke-Pester에서 받은 테스트 결과를 보관하는 방법을 보여 줍니다. 자세한 내용은 upload-artifact 작업을 참조하세요.

name: Upload artifact from Ubuntu

on: [push]

jobs:
  upload-pester-results:
    name: Run Pester and upload results
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Test with Pester
        shell: pwsh
        run: Invoke-Pester Unit.Tests.ps1 -Passthru | Export-CliXml -Path Unit.Tests.xml
      - name: Upload test results
        uses: actions/upload-artifact@v4
        with:
          name: ubuntu-Unit-Tests
          path: Unit.Tests.xml
    if: ${{ always() }}

always() 함수는 테스트가 실패하더라도 계속 처리하도록 작업을 구성합니다. 자세한 내용은 "컨텍스트"을(를) 참조하세요.

CI 테스트에 통과하면 PowerShell 모듈을 PowerShell 갤러리 게시하도록 워크플로를 구성할 수 있습니다. 비밀을 사용하여 패키지를 게시하는 데 필요한 모든 토큰 또는 자격 증명을 저장할 수 있습니다. 자세한 내용은 "GitHub Actions에서 비밀 사용"을(를) 참조하세요.

다음 예제에서는 패키지를 만들고 Publish-Module을 사용하여 PowerShell 갤러리에 게시합니다.

name: Publish PowerShell Module

on:
  release:
    types: [created]

jobs:
  publish-to-gallery:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build and publish
        env:
          NUGET_KEY: ${{ secrets.NUGET_KEY }}
        shell: pwsh
        run: |
          ./build.ps1 -Path /tmp/samplemodule
          Publish-Module -Path /tmp/samplemodule -NuGetApiKey $env:NUGET_KEY -Verbose