How to Trigger Jenkins from a Perforce Server running Windows
|Ever since version 1.3.9 of the Jenkins P4 Plugin, it’s possible to trigger Jenkins jobs from a Perforce commit. There’s a Cloudbees article that discusses how the Jenkins plugin works in detail.
In the article, it discusses how to create a Perforce trigger so that when change-commit events occur, a script would submit a POST to the REST API to trigger Perforce jobs. The script, while simple, was written for bash and targets servers running Linux.
Below is the Windows equivalent script if you don’t want to figure out how to run bash on Windows. This script is adapted from the Swarm trigger script as it operates similarly and also requires curl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Perforce Jenkins Trigger Script | |
' | |
' This script is meant to be called from a Perforce trigger. | |
' It should be placed on the Perforce Server machine. | |
' See usage information below for more details. | |
' | |
' Adapted from swarm-trigger.vbs | |
' | |
' | |
' Prerequisites: | |
' 1. curl. curl can be downloaded from | |
' http://curl.haxx.se/download.html. | |
' | |
JENKINS_HOST = "https://My-Jenkins-Server:8080/" | |
CURL_EXE = "c:\windows\system32\curl.exe" | |
' DO NOT EDIT PAST THIS LINE ————————————— | |
' Determine name and full path of the script | |
Myname = Wscript.ScriptName | |
Fullname = Wscript.ScriptFullName | |
' Parse arguments | |
Tvalue = WScript.Arguments.Named.Item("value") | |
' Check arguments | |
If WScript.Arguments.Count <> 1 Then | |
Wscript.Echo "Unexpected arguments" | |
Usage Myname,Fullname | |
End If | |
If Tvalue = "" Then | |
Wscript.Echo "No value supplied" | |
Usage Myname,Fullname | |
End If | |
If JENKINS_HOST = "" Then | |
Wscript.Echo "JENKINS_HOST empty or default; please update in this script" | |
Usage Myname,Fullname | |
End If | |
Set WshShell = WScript.CreateObject("WScript.Shell") | |
DoubleQuote = chr(34) | |
WshShell.Run DoubleQuote & CURL_EXE & DoubleQuote & " –header ""Content-Type: application/json"" –request POST –data ""payload={change:" & Tvalue & ", p4port:\""My-Perforce-Server:1666\""}"" https://My-Jenkins-Server:8080/p4/change", 0 | |
Wscript.Quit 0 | |
Sub Usage(aname,afullname) | |
Wscript.Echo "Usage: cscript " & aname & " /value:<value>" | |
Wscript.Echo " /value: specify the changelist " | |
Wscript.Echo ". " | |
Wscript.Echo " This script is meant to be called from a Perforce trigger. " | |
Wscript.Echo " It should be placed on the Perforce Server machine and the " | |
Wscript.Echo " following entries should be added using 'p4 triggers': " | |
Wscript.Echo ". " | |
Wscript.Echo " perforce.commit change-commit //… ""C:\windows\system32\cscript.EXE /nologo %quote%" & afullname & "%quote% /value:%change%"" " | |
Wscript.Echo ". " | |
Wscript.Echo " Please note that the use of '%quote%' is not supported on 2010.2 servers (they are harmless" | |
Wscript.Echo " though); if you're using this version, ensure you don't have any spaces in the pathname to" | |
Wscript.Echo " this script." | |
Wscript.Quit 99 | |
End Sub |