Packer: Docker builder with powershell provisioner always returns 0

Created on 8 May 2019  ·  7Comments  ·  Source: hashicorp/packer

Packer version 1.4.0
Building Windows containers

When running the powershell provisioner with docker the powershell exit code is not reported back to packer. Is always 0

The issue is quite simple. The execute_command already has the powershell.exe.
When running in the docker provisioner, the execute_command runs already in a powershell console.

image

Workaround:
"execute_command": "\u0026 { if (Test-Path variable:global:ProgressPreference){$ProgressPreference=\"SilentlyContinue\"};. {{.Vars}}; \u0026\"{{.Path}}\"; exit $LastExitCode }"

Basically, removing the "powershell -executionpolicy bypass " from the execute command solves this.

bug buildedocker provisionepowershell

Most helpful comment

I think this is a great solution.

All 7 comments

Changing this so that it doesn't call Powershell inside the provisioner will break things for builders that aren't docker.

What happens if you change your docker builder run_command to ["-d", "-i", "-t", "{{.Image}}"]?

Let me try that. Will let you know..
And change the entrypoint to cmd.exe? This would make the provisioner to run like the other builders (But then will break the other provisioners like the "file" one )

Hey @pintonunes, this could have been fixed by https://github.com/hashicorp/packer/pull/7657, I recently released a nightly build of this, can you please tell us wether this fixed it for you ?

https://github.com/hashicorp/packer/releases/tag/nightly

EDIT: nevermind ! I just tried it and nope.

Hello here, I added the possibility to set the "execution_policy": "none" per powershell provisioner in this PR: https://github.com/hashicorp/packer/pull/7678

This way, the exit code is returned ! 👍

{
  "builders": [
    {
      "type": "docker",
      "image": "microsoft/windowsservercore:1709",
      "windows_container": true,
      "commit": true
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "inline": ["dir c:\\", "exit(32)"],
      "execution_policy": "none"
    }
  ]
}
-> docker: Script exited with non-zero exit status: 32.Allowed exit codes are: [0]

Binaries can be found here: https://circleci.com/gh/hashicorp/packer/2752#artifacts/containers/0

Does it seems like a valid solution to you @SwampDragons & @pintonunes ?

I think this is a great solution.

Yay, awesome ! :D Closing this issue as #7678 got merged.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings