We aren't by any means PowerShell experts and when we published the module we found that we should have run the PSSScriptAnalyzer , so we run it and fixed the issues but that will not scale as a manual task also I would like to automate the publishing to the gallery so is always in sync with the code in the repo.

Provide static analysis using PSSScriptAnalyzer and show the results to the users submitting the pull request

After the PR is merged into main publish automatically to the PowerShell gallery

So I started by onboarding the GitHub repo to AppVeyor and created a New Project based on our repo, the procedure is pretty straightforward and AppVeyor has plenty of documentation so I'm not including any detailed instructions.

Onboarding the ScriptAnalyzer

First thing is to add the appveyor.yml file in our GitHub repo, my initials attempts failed , basically I wanted to install the PSScriptAnalyzer module but it failed with

Exception calling "ShouldContinue" with "2" argument(s): "The method or operation is not implemented."

Command executed with exception: NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201' or newer version of NuGet provider is installed.

The solution is to install first the nuget package provider, the install section end up being

install:

- ps:Install-PackageProvider -Name NuGet -Force

- ps:Install-Module -Name PSScriptAnalyzer -Force

The next step is to produce a XML results file, I spend some time figuring out what would be the simplest schema and selected junit and then I used some magic string manipulation to get the results of the script analyzer into the testresults file

Publishing to the PowerShell Gallery

I want to control the publishing process, I don't want every single commit to master to publish a new version of the module to the PowerShell Gallery, so I decided to use git tags in order to request a publish, basically when a new tag is added the continuous integration process will run the tests and if they pass it will use the tag label as the version to publish in the gallery, the general workflow is

Update the module manifest (.psd1) with the tag version

Publish to the PowerShell Gallery

If the publish is successful commit back to the repo the new module manifest (.psd1)

One issue that I hit was that my git commands from powershell were treating the git console messages as errors, searching around found this workaround and implemented in the AppVeyor script, the deploy section looks like this.

# Deploy to Powershell Gallery only where there is a tag which will have the version number

For some unexplainable reason I wasn’t able to use the Update-ModuleManifest cmdlet so I did the manual replacing of the module version.

If you are interested in the entire deployment file it is available here in our repo appveyor.yml

Something I realized after I finished is that GitHub handle the tags as releases, which is perfect for my intentions, because now when we need to publish a new version we just create a release in GitHub and it will show up automatically as a release in the PowerShell gallery

Release in GitHub and in the PowerShell gallery

Finally many thanks to a fellow Microsoft engineer Sergei Vorobev who provided various tips around the entire process.