2

I am trying to setup Post-commit hook for Bitbucket to trigger Teamcity if there is any change in my repo.

Following is the shell script for it:

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

But, I am working on a Windows environment and need this script in Powershell, I tried to convert it to Powershell but it doesn't seem to work.

$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

I'm not so much familiar with Powershell scripting. Where am I going wrong with this?

ANIL
  • 2,542
  • 4
  • 25
  • 44
  • Why? A bash script would run perfectly in a Git for Windows bash environment. – VonC Oct 23 '17 at 04:46
  • We are actually writing a generalized Powershell script for multiple repositories. – ANIL Oct 23 '17 at 04:48
  • And you cannot write that generalized script in bash? – VonC Oct 23 '17 at 04:52
  • Bitbucket is installed in a Windows server and this script here is for Linux based servers: https://confluence.jetbrains.com/display/TCD10/Configuring+VCS+Post-Commit+Hooks+for+TeamCity I'm not sure if I can use a bash script. – ANIL Oct 23 '17 at 04:54
  • 1
    Yes, you can. It will be interpreted by the git bash. On Windows. – VonC Oct 23 '17 at 04:56
  • I will try, thank you. – ANIL Oct 23 '17 at 05:57

1 Answers1

3

To do the same in PowerShell is arguably a bit more complicated. We need a job to get a background task and have to redirect to 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

I cannot test this end-to-end, but this should give you a headstart.

wp78de
  • 18,207
  • 7
  • 43
  • 71