How do fail a Packer pipeline upon powershell provisioner pester test failure?

HOW TO -️ October 18, 2021

I'm building a packer pipeline and having an issue in getting the failure of a powershell provisioner step to fail the packer job itself. This is important as without failing the Packer job, the AMI would be published, despite the test failure.

How I'm proceeding:

  1. Run provisioning setup steps
  2. Copy Pester Script to Remote machine
  3. Invoke inline powershell statement to Invoke-Pester
  4. Download the test results from the Pester remote run
  5. Next provisioning step parses the file to count errors and throws error upon issue count > 0

However, even with issue count being > 0 it is not causing the pipeline to fail. Additionally some errors thrown in other provisioner steps also fail to cause the Packer job to stop. I've set $ErrorActionPreference = 'Stop' on one of them and it didn't seem to make any difference.

Here's part of the provisioning workflow for packer.

ERROR Rendering Code Block

Once this part is done it's followed by this step which is some logic from this blog post:

ERROR Rendering Code Block

The Get-TestResults.ps1 has the following function thanks to this article: Testing Packer Images with Pester written originally by Sam Cogan and modified slightly by me.

ERROR Rendering Code Block

Answer

I found one syntax issue when reworking through this, as the default Packer behavior per PowerShell's default behavior is $ErrorActionPreference = 'continue' . I set manually now at the top of all my scripts and also fixed a syntax issue on Get-TestResults and will see if it works correctly this time. Fingers crossed

Initializing...