hashicorp / hashicorp/packer

Add support for using Bash with Shell Provisioner on Windows

Open
#12,413 0 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
15.8k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

#### Community Note

* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request
* Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
* If you are interested in working on this issue or have submitted a pull request, please leave a comment

Please search the existing issues for relevant feature requests, and use the
reaction feature
(https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/)
to add upvotes to pre-existing requests.

#### Description

The Shell Provisioner will work on Windows, but it uses CMD. I would like to be able to specify whether to use bash and then configure all other options like I would on Linux - including having all the paths interpreted as though it's on Linux. I used to use this code:

```
provisioner "shell" {
environment_vars = local.ENV_VARS
env_var_format = "set %s=%s & "
execute_command = "{{ .Vars }} bash {{ .Path }}"
remote_folder = "C:\\Temp\\"
scripts = var.VM_SCRIPTS
}
```
It mysteriously stopped working a couple of months back. I think it started inserting new lines between the set lines, so only the first one executed and not the rest of the execution line (that is what putting echo in front of the `execute_command` suggested)

Now I have switched to the Powershell Provisioner and it is working, but it might stop working in the future as this is not a supported/published method.

```
provisioner "powershell" {
environment_vars = local.ENV_VARS
use_pwsh = true
debug_mode = 1
execute_command = "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'};. {{.Vars}}; bash.exe -l '{{.Path}}'; exit $LastExitCode }\""
scripts = var.VM_SCRIPTS
timeout = "30m"
}
```

I use Git Bash which is added to the path. It's path from a Windows perspective is: `C:\Program Files\Git\bin\bash.exe`. Others might use Cygwin or WSL though, so some flexibility would be good - but just using what is in the path should be fine for most scenarios.

#### Use Case(s)

I have configuration scripts written in bash that need to run on Windows and Linux. Bash is helpful as it will fail the build if a single command fails (whereas Powershell is more difficult to configure for this) i.e. I just put `set -eo pipefail` at the top of the script and it takes care of the rest. For example, we keep the Python and Powershell modules in sync between Windows and Linux and using bash in this way means it's defined in one place (but there are a number of other scenarios I use it for too).

#### Potential configuration

```
provisioner "shell" {
environment_vars = local.ENV_VARS
scripts = var.VM_SCRIPTS
use_bash = true
}
```

#### Potential References

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.