0

I want to trigger the jenkins build in post-commit hook.if file is committed on directory "SOURCE/databasescript" and file name is "update.sql".

post-commit

set "REPOS=%~1"
set "TXN=%~3"
set "REV=%~2"
echo "repos %REPOS%." >> C:\test.log
echo "REV %REV%." >> C:\test.log
echo "txn %TXN%." >> C:\test.log
"%VISUALSVN_SERVER%\bin\svnlook.exe" changed -r %2 %1 >>C:\test.log

Output

"repos D:\Repositories\TestNAPFServer." 
"REV 82." 
"txn 81-2e." 
U   SOURCE/databasescript/basedb.sql
U   SOURCE/databasescript/update.sql 

I do not know how to write condition to find the filename in directory list in batch file.Please suggest.

Sameek Mishra
  • 9,174
  • 31
  • 92
  • 118

1 Answers1

1

Below solution work for me:

Post-commit.cmd

setlocal enabledelayedexpansion
@echo off 
REM Get the current path
set mypath=%cd%

set "REPOS=%~1"
set "TXN=%~3"
set "REV=%~2"
REM create log folder
IF NOT EXIST %REPOS%\log (
    md %REPOS%\log\
)
REM Delete the file log file
del /F /Q %REPOS%\log\napfposthook.log
echo "Current path-%mypath%" >> %REPOS%\log\napfposthook.log
echo "repos %REPOS%." >> %REPOS%\log\napfposthook.log
echo "REV %REV%." >> %REPOS%\log\napfposthook.log
echo "txn %TXN%." >> %REPOS%\log\napfposthook.log
set SVNLOOK="%VISUALSVN_SERVER%\bin\svnlook.exe"
%SVNLOOK% changed -r %2 %1 >> %REPOS%\log\napfposthook.log

Set SEPARATOR=,
SET COMMENT=

REM Concatenate all the lines in the commit message
fOR /F "tokens=2 delims= " %%g IN ('%SVNLOOK% changed -r %REV% %REPOS%') do ( 
    set currentline=%%g
    set COMMENT=!COMMENT!%SEPARATOR%!currentline!
)

REM Delete the file before insert the directory list
del /F /Q  %REPOS%\log\napfsearch.txt

REM Write the all directory list into file
echo %COMMENT% >> %REPOS%\log\napfsearch.txt

REM Find the string into file
find /I "update.sql" "%REPOS%\log\napfsearch.txt">nul
set findStatus=%errorlevel%
echo %findStatus% >> %REPOS%\log\napfposthook.log


SET CSCRIPT=%windir%\system32\cscript.exe
SET VBSCRIPT=D:\Repositories\post-commit-hook-jenkins-sam.vbs
"%CSCRIPT%" "%VBSCRIPT%" "NAPF_PRODB" "f23338c8b8f7216fad54dd34da7a2a5d" "%findStatus%"

post-commit-hook-jenkins-sam.vbs

Set args = WScript.Arguments
JobName = args.Item(0)
Token = args.Item(1)
updateDbScript=args.Item(2)
JenkinsServerUrl="http://10.254.6.206:8080/jenkins/"

If updateDbScript = 0 Then
    Wscript.Echo "Triggered the update job..."
    updateUrl = JenkinsServerUrl+"/buildByToken/buildWithParameters?job=" + JobName + "&token=" + Token+ "&Type=1"
    updateRequest = ""
    'Sending rest request to jenkins 
    HTTPPost updateUrl, updateRequest
else
     Wscript.Echo "Triggered the jenkins main job..."
     sUrl = JenkinsServerUrl+"/buildByToken/buildWithParameters?job=" + JobName + "&token=" + Token+ "&Type=0"
    'POST Request to send....
    sRequest = ""
    'Sending rest request to jenkins 
    HTTPPost sUrl, sRequest
End If


Function HTTPPost(sUrl, sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl,false
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
 End Function 
Sameek Mishra
  • 9,174
  • 31
  • 92
  • 118