A project containing the necessary tools to ease publishing of PowerShell modules.
This project provides PowerShell cmdlets and CI templates that other projects can utilize for building, testing, and publishing PowerShell modules.
PSModulePublisher
can be used as a submodule for building, testing, and publishing PowerShell modules.
PSModulePublisher
requires main projects to adopt the following directory structure:
/build/ # Directory containing build files
/build/PSModulePublisher/ # The root directory of PSModulePublisher as a submodule
/build/definitions/modulemanifest.ps1 # The module manifest definition file
/src/MyPowershellModule/ # The module's root directory
/src/MyPowershellModule/MyPowershellModule.psm1 # The module .psm1 file
/src/MyPowershellModule/MyPowershellModule.psd1 # The module .psd1 file (optional)
/test/ # Directory containing test files (optional)
/test/test.ps1 # The test entrypoint script (optional)
Add PSModulePublisher
as a submodule under the directory build
in your main project:
# Add the submodule
git submodule add https://github.com/theohbrothers/PSModulePublisher.git build/PSModulePublisher
# Checkout ref to use
git --git-dir build/PSModulePublisher/.git checkout vx.x.x
# Commit the submodule
git commit -am 'Add submodule PSModulePublisher vx.x.x'
Ensure the main project contains the module file at the location src/MyPowershellModule/MyPowershellModule.psm1
.
The project sources from a definition file to generate a manifest used for publishing the module. Ensure that the file exists in your main project at the location build/definitions/modulemanifest.ps1
and that it contains the right properties and values relevant to your PowerShell module. Remember to update the definition prior to publishing your module.
The definition template can be found here.
Decide on which CI provider to use in your main project based on those supported by this project. Then setup the CI file(s) for your main project, referencing relevant CI remote template(s) of this project from your main project's CI file(s).
Sample CI files can be found here.
The project optionally runs an entrypoint script for tests at the location test/test.ps1
. You can add the module's main tests steps in this file for tests to be run.
Sample test files can be found here.
Configure the following CI settings for your project.
Add a secret variable NUGET_API_KEY
containing your PSGallery API key to your main project's CI settings for publishing your module on PowerShell Gallery.
By default, PSModulePublisher
uses the main project's root directory as the path for execution. To override the default location, set the environment variable PROJECT_BASE_DIR
to contain a custom directory value in your CI environment before executing PSModulePublisher
.
The PowerShell cmdlet Invoke-PSModulePublisher
is used for executing the project's build, test, and publish steps for PowerShell modules.
Invoke-PSModulePublisher [[-Repository] <string>] [-DryRun] [<CommonParameters>]
To perform the project's build, test, and publish steps for a given module, simply define applicable environment variables for a given PowerShell module project before executing the cmdlet.
# Process applicable environment variables
$env:PROJECT_BASE_DIR="$(git rev-parse --show-toplevel)"
# Build and Test steps (Generates module manifest, Tests module via module manifest)
Invoke-PSModulePublisher
# Publish steps (Publishes module as a dry run)
Invoke-PSModulePublisher -Repository MyPSRepository -DryRun
# Publish steps (Publishes module)
Invoke-PSModulePublisher -Repository MyPSRepository
Note: Ensure the environment variable NUGET_API_KEY
is defined prior to publishing PowerShell modules.
The individual cmdlets may also be used for executing the project's build, test, and publish steps independently.
The project includes PowerShell cmdlets and CI remote templates for executing the project's build, test, and publish steps for PowerShell modules.
The CI process with the included CI templates is composed of the following steps:
- Display system info
- Get PowerShell version
- Process build variables
- Generate module manifest
- Test module via module's manifest
- Install publish dependencies (if applicable)
- Publish module
Build and Test steps can be executed for every commit pushed. Simply configure your main project's CI file(s) and/or settings to allow so.
Publish steps will run only for tag refs. Ensure your main project's CI file(s) and/or settings are configured to run CI jobs for tag refs.
Tags must follow Semantic Versioning and be prepended with a lowercase v
:
# Tag the commit to publish
git tag v1.0.12
# Push the tag
git push remotename v1.0.12
For a basic use case, the CI process could simply comprise a single stage containing all the steps from Build, Test, and Publish.
In cases where the module needs to be tested across multiple operating systems and/or versions of PowerShell, two stages can be configured: The 1st stage containing multiple jobs executing Build and Test steps for building and testing the module; the 2nd stage containing a single job executing Build and Publish steps for publishing the module.
Refer to the sample CI files for some working examples.
The following are the parameters and environment variables supported by the provided PowerShell cmdlets for building, testing, and publishing PowerShell modules.
Invoke-Build [<CommonParameters>]
Invoke-Test [-ModuleManifestPath] <string> [<CommonParameters>]
Invoke-Publish [-ModuleManifestPath] <string> [-Repository] <string> [-DryRun] [<CommonParameters>]
Name | Example value | Mandatory | Type |
---|---|---|---|
PROJECT_BASE_DIR |
/path/to/my-project |
false | string |
Name | Example value | Mandatory | Type |
---|---|---|---|
NUGET_API_KEY |
xxx |
true | string |
To execute build, test, and publish steps for a project, simply define applicable environment variables before executing the individual cmdlets within the CI environment to perform their respective functions.
# Process applicable environment variables
$env:PROJECT_BASE_DIR="$(git rev-parse --show-toplevel)"
# Build (Generates module manifest)
$moduleManifestPath = Invoke-Build
# Test (Tests module via module manifest)
Invoke-Test -ModuleManifestPath $moduleManifestPath
# Publish (Publishes module)
Invoke-Publish -ModuleManifestPath $moduleManifestPath -Repository PSGallery
Note: Ensure the environment variable NUGET_API_KEY
is defined prior to publishing PowerShell modules.
To update the submodule:
git submodule update --remote build/PSModulePublisher
To use a specific tag of the submodule:
# Checkout ref to use
git --git-dir build/PSModulePublisher/.git checkout vx.x.x
# Bump PSModulePublisher to the same ref in CI file
vi azure-pipelines.yml
# Commit the submodule and CI file
git commit -am 'Bump PSModulePublisher to vx.x.x'
- Use only tag refs of
PSModulePublisher
in your main project. - Ensure your main project's CI file(s) is configured to use the CI templates of
PSModulePublisher
and that the ref used matches that of thePSModulePublisher
submodule used in your main project.