【问题标题】:How to properly convert Bitbucket post-commit Hook shell script to Powershell Script?如何正确将 Bitbucket 提交后 Hook shell 脚本转换为 Powershell 脚本?
【发布时间】:2017-10-23 04:33:31
【问题描述】:

我正在尝试为 Bitbucket 设置提交后挂钩以在我的存储库中有任何更改时触发 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 脚本。我哪里错了?

【问题讨论】:

  • 为什么? bash 脚本可以在 Git for Windows bash 环境中完美运行。
  • 我们实际上是在为多个存储库编写一个通用的 Powershell 脚本。
  • 你不能用 bash 写那个通用的脚本吗?
  • Bitbucket 安装在 Windows 服务器中,这里的脚本适用于基于 Linux 的服务器:confluence.jetbrains.com/display/TCD10/… 我不确定是否可以使用 bash 脚本。
  • 是的,你可以。它将由 git bash 解释。在 Windows 上。

标签: git shell powershell


【解决方案1】:

在 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

我无法进行端到端的测试,但这应该会给您一个良好的开端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多