This guide shows you how to use PowerShell for CI. It describes how to use Pester, install dependencies, test your module, and publish to the PowerShell Gallery.
GitHub-hosted runners have a tools cache with pre-installed software, which includes PowerShell and Pester.
For a full list of up-to-date software and the pre-installed versions of PowerShell and Pester, see GitHub-hosted runners.
To automate your testing with PowerShell and Pester, you can add a workflow that runs every time a change is pushed to your repository. In the following example, Test-Path is used to check that a file called resultsfile.log is present.
This example workflow file must be added to your repository's .github/workflows/ directory:
shell: pwsh - Configures the job to use PowerShell when running the run commands.
run: Test-Path resultsfile.log - Check whether a file called resultsfile.log is present in the repository's root directory.
Should -Be $true - Uses Pester to define an expected result. If the result is unexpected, then GitHub Actions flags this as a failed test. For example:
Screenshot of a workflow run failure for a Pester test. Test reports "Expected $true, but got $false" and "Error: Process completed with exit code 1."
Invoke-Pester Unit.Tests.ps1 -Passthru - Uses Pester to execute tests defined in a file called Unit.Tests.ps1. For example, to perform the same test described above, the Unit.Tests.ps1 will contain the following:
Describe "Check results file is present" {
It "Check results file is present" {
Test-Path resultsfile.log | Should -Be$true
}
}
On Ubuntu runners, Azure PowerShell modules are stored in /usr/share/ instead of the default location of PowerShell add-on modules (i.e. /usr/local/share/powershell/Modules/).
GitHub-hosted runners have PowerShell 7 and Pester installed. You can use Install-Module to install additional dependencies from the PowerShell Gallery before building and testing your code.
Note
The pre-installed packages (such as Pester) used by GitHub-hosted runners are regularly updated, and can introduce significant changes. As a result, it is recommended that you always specify the required package versions by using Install-Module with -MaximumVersion.
You can also cache dependencies to speed up your workflow. For more information, see Dependency caching reference.
For example, the following job installs the SqlServer and PSScriptAnalyzer modules:
By default, no repositories are trusted by PowerShell. When installing modules from the PowerShell Gallery, you must explicitly set the installation policy for PSGallery to Trusted.
You can cache PowerShell dependencies using a unique key, which allows you to restore the dependencies for future workflows with the cache action. For more information, see Dependency caching reference.
PowerShell caches its dependencies in different locations, depending on the runner's operating system. For example, the path location used in the following Ubuntu example will be different for a Windows operating system.
The following example installs PSScriptAnalyzer and uses it to lint all ps1 files in the repository. For more information, see PSScriptAnalyzer on GitHub.
You can upload artifacts to view after a workflow completes. For example, you may need to save log files, core dumps, test results, or screenshots. For more information, see Store and share data with workflow artifacts.
The following example demonstrates how you can use the upload-artifact action to archive the test results received from Invoke-Pester. For more information, see the upload-artifact action.
You can configure your workflow to publish your PowerShell module to the PowerShell Gallery when your CI tests pass. You can use secrets to store any tokens or credentials needed to publish your package. For more information, see Using secrets in GitHub Actions.
The following example creates a package and uses Publish-Module to publish it to the PowerShell Gallery: