์๊ฐ
์ด ๊ฐ์ด๋์์๋ CI์ฉ PowerShell์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค. Pester๋ฅผ ์ฌ์ฉํ๊ณ , ์ข ์์ฑ์ ์ค์นํ๊ณ , ๋ชจ๋์ ํ ์คํธํ๊ณ , PowerShell ๊ฐค๋ฌ๋ฆฌ ๊ฒ์ํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค.
GitHub ํธ์คํธ ๋ฌ๋์๋ PowerShell ๋ฐ Pester๊ฐ ํฌํจ๋ ์ํํธ์จ์ด๊ฐ ์ฌ์ ์ค์น๋ ๋๊ตฌ ์บ์๊ฐ ์์ต๋๋ค.
์ต์ ์ํํธ์จ์ด ๋ฐ ์ฌ์ ์ค์น๋ ๋ฒ์ ์ Python๊ณผ Pester์ ๋ํ ์ ์ฒด ๋ชฉ๋ก์ GitHub ํธ์คํ ์คํ๊ธฐ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
ํ์ ์กฐ๊ฑด
YAML ๋ฐ GitHub Actions์ ๊ตฌ๋ฌธ์ ๋ํด ์ ์๊ณ ์์ด์ผ ํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ ์ํฌํ๋ก ์์ฑ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
PowerShell ๋ฐ Pester๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ดํดํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ๋ค์์ ์ฐธ์กฐํ์ธ์.
Pester์ ๋ํ ์ํฌํ๋ก ์ถ๊ฐ
PowerShell ๋ฐ Pester๋ฅผ ์ฌ์ฉํ์ฌ ํ
์คํธ๋ฅผ ์๋ํํ๋ ค๋ฉด ๋ณ๊ฒฝ ๋ด์ฉ์ด ๋ฆฌํฌ์งํ ๋ฆฌ์ ํธ์๋ ๋๋ง๋ค ์คํ๋๋ ์ํฌํ๋ก๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค. ๋ค์ ์์ ์์ Test-Path
๋ resultsfile.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๋ ์ด๋ฅผ ์คํจํ ํ ์คํธ๋ก ํ๋๊ทธ๋ฅผ ์ง์ ํฉ๋๋ค. ์์: -
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 ๋ชจ๋์ ์์น๋ฅผ ์ค๋ช ํฉ๋๋ค.
Ubuntu | macOS | Windows | |
---|---|---|---|
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
์ ์ฌ์ฉํ์ฌ ํญ์ ํ์ํ ํจํค์ง ๋ฒ์ ์ ์ง์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
์ข ์์ฑ์ ์บ์ํ์ฌ ์ํฌํ๋ก ์๋๋ฅผ ๋์ผ ์๋ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ์ข ์์ฑ ์บ์ฑ ์ฐธ์กฐ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
์๋ฅผ ๋ค์ด ๋ค์ ์์
์ SqlServer
๋ฐ PSScriptAnalyzer
๋ชจ๋์ ์ค์นํฉ๋๋ค.
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@v4
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()
ํจ์๋ ํ
์คํธ๊ฐ ์คํจํ๋๋ผ๋ ๊ณ์ ์ฒ๋ฆฌํ๋๋ก ์์
์ ๊ตฌ์ฑํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ ์ปจํ
์คํธ ์ฐธ์กฐ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
PowerShell ๊ฐค๋ฌ๋ฆฌ์ ๊ฒ์
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