{"meta":{"title":"PowerShell 빌드 및 테스트","intro":"CI(연속 통합) 워크플로를 만들어 PowerShell 프로젝트를 빌드하고 테스트하는 방법을 알아보세요.","product":"GitHub Actions","breadcrumbs":[{"href":"/ko/enterprise-cloud@latest/actions","title":"GitHub Actions"},{"href":"/ko/enterprise-cloud@latest/actions/tutorials","title":"자습서"},{"href":"/ko/enterprise-cloud@latest/actions/tutorials/build-and-test-code","title":"코드 빌드 및 테스트"},{"href":"/ko/enterprise-cloud@latest/actions/tutorials/build-and-test-code/powershell","title":"PowerShell"}],"documentType":"article"},"body":"# PowerShell 빌드 및 테스트\n\nCI(연속 통합) 워크플로를 만들어 PowerShell 프로젝트를 빌드하고 테스트하는 방법을 알아보세요.\n\n## 소개\n\n이 가이드에서는 CI용 PowerShell을 사용하는 방법을 보여 줍니다. Pester를 사용하고, 종속성을 설치하고, 모듈을 테스트하고, PowerShell Gallery 게시하는 방법을 설명합니다.\n\nGitHub 호스트 러너에는 PowerShell 및 Pester가 포함된 소프트웨어가 사전 설치된 도구 캐시가 있습니다.\n\n최신 소프트웨어 및 사전 설치된 버전의 PowerShell과 Pester에 대한 전체 목록은 [GitHub 호스팅 실행기](/ko/enterprise-cloud@latest/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software)을(를) 참조하세요.\n\n## 필수 조건\n\nYAML 및 GitHub Actions의 구문에 대해 잘 알고 있어야 합니다. 자세한 내용은 [워크플로 작성](/ko/enterprise-cloud@latest/actions/learn-github-actions)을(를) 참조하세요.\n\nPowerShell 및 Pester를 기본적으로 이해하는 것이 좋습니다. 자세한 내용은 다음을 참조하세요.\n\n* [PowerShell 시작](https://docs.microsoft.com/powershell/scripting/learn/ps101/01-getting-started)\n* [Pester](https://pester.dev)\n\n## Pester에 대한 워크플로 추가\n\nPowerShell 및 Pester를 사용하여 테스트를 자동화하려면 변경 내용이 리포지토리에 푸시될 때마다 실행되는 워크플로를 추가할 수 있습니다. 다음 예제에서 `Test-Path`는 `resultsfile.log`라는 파일이 있는지 확인하는 데 사용됩니다.\n\n이 예제 워크플로 파일을 리포지토리의 `.github/workflows/` 디렉터리에 추가해야 합니다.\n\n```yaml\nname: Test PowerShell on Ubuntu\non: push\n\njobs:\n  pester-test:\n    name: Pester test\n    runs-on: ubuntu-latest\n    steps:\n      - name: Check out repository code\n        uses: actions/checkout@v6\n      - name: Perform a Pester test from the command-line\n        shell: pwsh\n        run: Test-Path resultsfile.log | Should -Be $true\n      - name: Perform a Pester test from the Tests.ps1 file\n        shell: pwsh\n        run: |\n          Invoke-Pester Unit.Tests.ps1 -Passthru\n```\n\n* `shell: pwsh` - `run` 명령을 실행할 때 PowerShell을 사용하도록 작업을 구성합니다.\n\n* `run: Test-Path resultsfile.log` - `resultsfile.log`라는 파일이 리포지토리의 루트 디렉터리에 있는지 확인합니다.\n\n* `Should -Be $true` - Pester를 사용하여 예상 결과를 정의합니다. 예기치 않은 결과인 경우 GitHub Actions는 이를 실패한 테스트로 플래그를 지정합니다. 예시:\n\n  ![Pester 테스트에 대한 워크플로 실행 실패의 스크린샷. 테스트는 \"$true를 예상했지만 $false가 나옴\" 및 \"오류: 종료 코드 1로 완료된 프로세스\"를 보고합니다.](/assets/images/help/repository/actions-failed-pester-test-updated.png)\n\n* `Invoke-Pester Unit.Tests.ps1 -Passthru` - Pester를 사용하여 `Unit.Tests.ps1`이라는 파일에 정의된 테스트를 실행합니다. 예를 들어 위에서 설명한 것과 동일한 테스트를 수행하기 위해 `Unit.Tests.ps1`에는 다음이 포함됩니다.\n\n  ```powershell\n  Describe \"Check results file is present\" {\n      It \"Check results file is present\" {\n          Test-Path resultsfile.log | Should -Be $true\n      }\n  }\n  ```\n\n## PowerShell 모듈 위치\n\n아래 테이블은 각 GitHub 호스트 러너에서 다양한 PowerShell 모듈의 위치를 설명합니다.\n\n<div class=\"ghd-tool rowheaders\">\n\n|                         | Ubuntu                                           | macOS                                             | Windows                                               |\n| ----------------------- | ------------------------------------------------ | ------------------------------------------------- | ----------------------------------------------------- |\n| **PowerShell 시스템 모듈**   | `/opt/microsoft/powershell/7/Modules/*`          | `/usr/local/microsoft/powershell/7/Modules/*`     | `C:\\program files\\powershell\\7\\Modules\\*`             |\n| **PowerShell 추가 항목 모듈** | `/usr/local/share/powershell/Modules/*`          | `/usr/local/share/powershell/Modules/*`           | `C:\\Modules\\*`                                        |\n| **사용자가 설치한 모듈**         | `/home/runner/.local/share/powershell/Modules/*` | `/Users/runner/.local/share/powershell/Modules/*` | `C:\\Users\\runneradmin\\Documents\\PowerShell\\Modules\\*` |\n\n</div>\n\n> \\[!NOTE]\n> Ubuntu 실행기에서 Azure PowerShell 모듈은 PowerShell 추가 기능 모듈의 기본 위치(예: `/usr/share/`) 대신 `/usr/local/share/powershell/Modules/` 저장됩니다.\n\n## 종속성 설치\n\nGitHub 호스트 실행기에는 PowerShell 7 및 Pester가 설치되어 있습니다.\n`Install-Module` 사용하여 코드를 빌드하고 테스트하기 전에 PowerShell Gallery 추가 종속성을 설치할 수 있습니다.\n\n> \\[!NOTE]\n> GitHub 호스트 실행기가 사용하는 사전 설치된 패키지(예: Pester)는 정기적으로 업데이트되며 중요한 변경 내용이 있을 수 있습니다. 따라서 `Install-Module`과 함께 `-MaximumVersion`을 사용하여 항상 필요한 패키지 버전을 지정하는 것이 좋습니다.\n\n종속성을 캐시하여 워크플로 속도를 높일 수도 있습니다. 자세한 내용은 [종속성 캐싱 참조](/ko/enterprise-cloud@latest/actions/using-workflows/caching-dependencies-to-speed-up-workflows)을(를) 참조하세요.\n\n예를 들어 다음 작업은 `SqlServer` 및 `PSScriptAnalyzer` 모듈을 설치합니다.\n\n```yaml\njobs:\n  install-dependencies:\n    name: Install dependencies\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v6\n      - name: Install from PSGallery\n        shell: pwsh\n        run: |\n          Set-PSRepository PSGallery -InstallationPolicy Trusted\n          Install-Module SqlServer, PSScriptAnalyzer\n```\n\n> \\[!NOTE]\n> 기본값으로 PowerShell은 리포지토리를 신뢰하지 않습니다. PowerShell Gallery에서 모듈을 설치할 때, `PSGallery`에 대한 설치 정책을 반드시 `Trusted`으로 명시적으로 설정해야 합니다.\n\n### 종속성 캐시 처리\n\n고유한 키를 사용하여 PowerShell 종속성을 캐시할 수 있으므로 [`cache`](https://github.com/marketplace/actions/cache) 작업으로 향후 워크플로에 대한 종속성을 복원할 수 있습니다. 자세한 내용은 [종속성 캐싱 참조](/ko/enterprise-cloud@latest/actions/using-workflows/caching-dependencies-to-speed-up-workflows)을(를) 참조하세요.\n\nPowerShell은 실행기의 운영 체제에 따라 다른 위치에 종속성을 캐시합니다. 예를 들어 다음 Ubuntu 예제에서 사용되는 `path` 위치는 Windows 운영 체제에 대해 다릅니다.\n\n```yaml\nsteps:\n  - uses: actions/checkout@v6\n  - name: Setup PowerShell module cache\n    id: cacher\n    uses: actions/cache@v4\n    with:\n      path: \"~/.local/share/powershell/Modules\"\n      key: ${{ runner.os }}-SqlServer-PSScriptAnalyzer\n  - name: Install required PowerShell modules\n    if: steps.cacher.outputs.cache-hit != 'true'\n    shell: pwsh\n    run: |\n      Set-PSRepository PSGallery -InstallationPolicy Trusted\n      Install-Module SqlServer, PSScriptAnalyzer -ErrorAction Stop\n```\n\n## 코드 테스트\n\n코드를 빌드하고 테스트하기 위해 로컬에서 사용하는 것과 동일한 명령을 사용할 수 있습니다.\n\n### PSScriptAnalyzer를 사용하여 코드를 검사하다\n\n다음 예제에서는 `PSScriptAnalyzer`를 설치하고 이를 사용하여 리포지토리의 모든 `ps1` 파일을 린트합니다. 자세한 내용은 [PSScriptAnalyzer on GitHub](https://github.com/PowerShell/PSScriptAnalyzer) 참조하세요.\n\n```yaml\n  lint-with-PSScriptAnalyzer:\n    name: Install and run PSScriptAnalyzer\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v6\n      - name: Install PSScriptAnalyzer module\n        shell: pwsh\n        run: |\n          Set-PSRepository PSGallery -InstallationPolicy Trusted\n          Install-Module PSScriptAnalyzer -ErrorAction Stop\n      - name: Lint with PSScriptAnalyzer\n        shell: pwsh\n        run: |\n          Invoke-ScriptAnalyzer -Path *.ps1 -Recurse -Outvariable issues\n          $errors   = $issues.Where({$_.Severity -eq 'Error'})\n          $warnings = $issues.Where({$_.Severity -eq 'Warning'})\n          if ($errors) {\n              Write-Error \"There were $($errors.Count) errors and $($warnings.Count) warnings total.\" -ErrorAction Stop\n          } else {\n              Write-Output \"There were $($errors.Count) errors and $($warnings.Count) warnings total.\"\n          }\n```\n\n## 워크플로 데이터를 아티팩트로 패키지\n\n워크플로가 완료된 후 볼 아티팩트를 업로드할 수 있습니다. 예를 들어 로그 파일, 코어 덤프, 테스트 결과 또는 스크린샷을 저장해야 할 수 있습니다. 자세한 내용은 [워크플로 아티팩트와 데이터 저장 및 공유](/ko/enterprise-cloud@latest/actions/using-workflows/storing-workflow-data-as-artifacts)을(를) 참조하세요.\n\n다음 예제에서는 `upload-artifact` 작업을 사용하여 `Invoke-Pester`에서 받은 테스트 결과를 보관하는 방법을 보여 줍니다. 자세한 내용은 [`upload-artifact`작업](https://github.com/actions/upload-artifact)을 참조하세요.\n\n```yaml\nname: Upload artifact from Ubuntu\n\non: [push]\n\njobs:\n  upload-pester-results:\n    name: Run Pester and upload results\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v6\n      - name: Test with Pester\n        shell: pwsh\n        run: Invoke-Pester Unit.Tests.ps1 -Passthru | Export-CliXml -Path Unit.Tests.xml\n      - name: Upload test results\n        uses: actions/upload-artifact@v4\n        with:\n          name: ubuntu-Unit-Tests\n          path: Unit.Tests.xml\n    if: ${{ always() }}\n```\n\n```\n          `always()` 함수는 테스트가 실패하더라도 계속 처리하도록 작업을 구성합니다. 자세한 내용은 [AUTOTITLE](/actions/learn-github-actions/contexts#always)을(를) 참조하세요.\n```\n\n## PowerShell Gallery에 게시하기\n\nCI 테스트가 통과하면 PowerShell 모듈을 PowerShell Gallery 게시하도록 워크플로를 구성할 수 있습니다. 비밀을 사용하여 패키지를 게시하는 데 필요한 모든 토큰 또는 자격 증명을 저장할 수 있습니다. 자세한 내용은 [GitHub Actions에서 비밀 사용](/ko/enterprise-cloud@latest/actions/security-guides/using-secrets-in-github-actions)을(를) 참조하세요.\n\n다음 예제에서는 패키지를 만들고 `Publish-Module` 사용하여 PowerShell Gallery 게시합니다.\n\n```yaml\nname: Publish PowerShell Module\n\non:\n  release:\n    types: [created]\n\njobs:\n  publish-to-gallery:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v6\n      - name: Build and publish\n        env:\n          NUGET_KEY: ${{ secrets.NUGET_KEY }}\n        shell: pwsh\n        run: |\n          ./build.ps1 -Path /tmp/samplemodule\n          Publish-Module -Path /tmp/samplemodule -NuGetApiKey $env:NUGET_KEY -Verbose\n```"}