Skip to content

Commit

Permalink
Merge pull request buildkite#1122 from pdemirdjian/powershell_hooks
Browse files Browse the repository at this point in the history
add powershell support for hooks
  • Loading branch information
keithpitt authored Dec 12, 2019
2 parents 4ebcbae + 38ed632 commit 5c47385
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (b *Bootstrap) applyEnvironmentChanges(environ *env.Environment, dir string
func (b *Bootstrap) findHookFile(hookDir string, name string) (string, error) {
if runtime.GOOS == "windows" {
// check for windows types first
if p, err := shell.LookPath(name, hookDir, ".BAT;.CMD"); err == nil {
if p, err := shell.LookPath(name, hookDir, ".BAT;.CMD;.PS1"); err == nil {
return p, nil
}
}
Expand Down
19 changes: 16 additions & 3 deletions bootstrap/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ func newHookScriptWrapper(hookPath string) (*hookScriptWrapper, error) {
var err error
var scriptFileName string = `buildkite-agent-bootstrap-hook-runner`
var isBashHook bool
var isPwshHook bool
var isWindows = runtime.GOOS == "windows"

// we use bash hooks for scripts with no extension, otherwise on windows
// we probably need a .bat extension
if filepath.Ext(hookPath) == "" {
if filepath.Ext(hookPath) == ".ps1" {
isPwshHook = true
scriptFileName += ".ps1"
} else if filepath.Ext(hookPath) == "" {
isBashHook = true
} else if runtime.GOOS == "windows" {
} else if isWindows {
scriptFileName += ".bat"
}

Expand Down Expand Up @@ -96,7 +101,7 @@ func newHookScriptWrapper(hookPath string) (*hookScriptWrapper, error) {

// Create the hook runner code
var script string
if runtime.GOOS == "windows" && !isBashHook {
if isWindows && !isBashHook && !isPwshHook {
script = "@echo off\n" +
"SETLOCAL ENABLEDELAYEDEXPANSION\n" +
"SET > \"" + h.beforeEnvFile.Name() + "\"\n" +
Expand All @@ -105,6 +110,14 @@ func newHookScriptWrapper(hookPath string) (*hookScriptWrapper, error) {
"SET " + hookWorkingDirEnv + "=%CD%\n" +
"SET > \"" + h.afterEnvFile.Name() + "\"\n" +
"EXIT %" + hookExitStatusEnv + "%"
} else if isWindows && isPwshHook {
script = `$ErrorActionPreference = "STOP"\n` +
`Get-ChildItem Env: | Foreach-Object {$($_.Name)=$($_.Value)"} | Set-Content "` + h.beforeEnvFile.Name() + `\n` +
absolutePathToHook + `\n` +
`if ($LASTEXITCODE -eq $null) {$Env:` + hookExitStatusEnv + ` = 0} else {$Env:` + hookExitStatusEnv + ` = $LASTEXITCODE}\n` +
`$Env:` + hookWorkingDirEnv + ` = $PWD | Select-Object -ExpandProperty Path\n` +
`Get-ChildItem Env: | Foreach-Object {"$($_.Name)=$($_.Value)"} | Set-Content "` + h.afterEnvFile.Name() + `"\n` +
`exit $Env:` + hookExitStatusEnv
} else {
script = "export -p > \"" + filepath.ToSlash(h.beforeEnvFile.Name()) + "\"\n" +
". \"" + filepath.ToSlash(absolutePathToHook) + "\"\n" +
Expand Down
9 changes: 9 additions & 0 deletions bootstrap/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func (s *Shell) RunScript(path string, extra *env.Environment) error {

var isBash = filepath.Ext(path) == "" || filepath.Ext(path) == ".sh"
var isWindows = runtime.GOOS == "windows"
var isPwsh = filepath.Ext(path) == ".ps1"

switch {
case isWindows && isBash:
Expand All @@ -267,6 +268,14 @@ func (s *Shell) RunScript(path string, extra *env.Environment) error {
command = bashPath
args = []string{"-c", filepath.ToSlash(path)}

case isWindows && isPwsh:
if s.Debug {
s.Commentf("Attempting to run %s with Powershell", path)
}
command = "powershell.exe"
args = []string{"-file", path}


case !isWindows && isBash:
command = "/bin/bash"
args = []string{"-c", path}
Expand Down

0 comments on commit 5c47385

Please sign in to comment.