如何正确地将Bitbucket post-commit Hook shell脚本转换为Powershell脚本?

时间:2017-10-23 04:33:31

标签: git shell powershell

如果我的回购中有任何更改,我正在尝试为Bitbucket设置Post-commit钩子以触发Teamcity。

以下是它的shell脚本:

SERVER=https://buildserver-url
USER=buildserver-user
PASS="<password>"

LOCATOR=$1

# The following is one-line:
(sleep 10;  curl --user $USER:$PASS -X POST "$SERVER/app/rest/vcs-root-instances/commitHookNotification?locator=$LOCATOR" -o /dev/null) >/dev/null 2>&1 <&1 &

exit 0

但是,我正在开发一个Windows环境并且在Powershell中需要这个脚本,我试图将它转换为Powershell但它似乎不起作用。

$SERVER=https://buildserver-url
$USER=buildserver-user
$PASS="<password>"

$LOCATOR=%1%

# The following is one-line:
(sleep 10;  curl --user $USER:$PASS -X POST "$SERVER/app/rest/vcs-root-instances/commitHookNotification?locator=$LOCATOR" -o /dev/null) >/dev/null 2>&1 <&1 &

exit 0

我对Powershell脚本编写并不熟悉。我在哪里错了?

1 个答案:

答案 0 :(得分:3)

在PowerShell中做同样的事情可能有点复杂。 我们需要一份工作来获得background task,并且必须重定向到Out-Null

param (
    [string]$locator  = "xyz"
)
$SERVER="https://buildserver-url"
$USER="buildserver-user"
$PASS="<password>"

$scriptBlock = {
    $output = & curl --user "$($USER):$($PASS)" -X POST "$($SERVER)/app/rest/vcs-root-instances/commitHookNotification?locator=$($args[1])" -o Out-Null 2>&1 | Out-Null;
    return $output
}

#Sleep 10
$job = Start-Job -scriptblock $scriptBlock -ArgumentList $locator
Wait-Job $job
Receive-Job $job

exit 0

我无法对这种端对端进行测试,但这应该会给你一个开端。