Search code examples
batch-filejenkinscmdcontinuous-integrationbuild-process

batch command execution through jenkins not extracting files though it extracts when run as bat file


I have written the below batch script in Jenkins. When I run it as a bat file in the Jenkins server workspace from the same folder, it runs without any issues. But when I run it through jenkins using "Execute Windows Batch Command" Its not extracting. It prints the line "about to copy from" with relevant paths and it just keeps executing from there. Nothing is printed in the console output and nothing is extracted. Below is the script.

echo %CD%
FOR /D %%p IN ("%CD%\Setups\*") DO rmdir "%%p" /s /q

 call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.2.23:8081/nexus/content/repositories/releases/ -Dartifact=test:update-service:1.0.3 -Ddest=Setups/Services/update-service.jar
call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.2.23:8081/nexus/content/repositories/releases/ -Dartifact=test:installer-prerequisites:1.0.0 -Ddest=Setups/PreRequisites/installer-prerequisites.zip -Dpackaging=zip

echo came after the downloads

for /r %%i in (*.zip) do (
  echo about to copy from %%~dpi to %%~fi
Call :UnZipFile "%%~dpi" "%%~fi"
echo called unzip on %%i
del /S /Q "%%~fi"
)

exit /b

:UnZipFile <ExtractTo> <newzipfile>
    setlocal
    set vbs="%temp%\_.vbs"
    if exist "%vbs%" del /f /q "%vbs%"
     >"%vbs%" echo Set fso = CreateObject("Scripting.FileSystemObject")
    >>"%vbs%" echo If NOT fso.FolderExists("%~1") Then
    >>"%vbs%" echo fso.CreateFolder("%~1")
    >>"%vbs%" echo End If
    >>"%vbs%" echo set objShell = CreateObject("Shell.Application")
    >>"%vbs%" echo set FilesInZip=objShell.NameSpace("%~2").items
    >>"%vbs%" echo objShell.NameSpace("%~1").CopyHere(FilesInZip)
    >>"%vbs%" echo Set fso = Nothing
    >>"%vbs%" echo Set objShell = Nothing
    cscript //nologo "%vbs%"
    if exist "%vbs%" del /f /q "%vbs%"
    endlocal

This works fine when run as a bat file. Please advice.

Below is the jenkins workspace path :

C:\Program Files (x86)\Jenkins\jobs\Installer\workspace\Setups

Solution

  • I had to specify GOTO:eof at the end of the sub-routine. That is,

    :UnZipFile <ExtractTo> <newzipfile>
    setlocal
    set vbs="%temp%\_.vbs"
    if exist "%vbs%" del /f /q "%vbs%"
     >"%vbs%" echo Set fso = CreateObject("Scripting.FileSystemObject")
    >>"%vbs%" echo If NOT fso.FolderExists("%~1") Then
    >>"%vbs%" echo fso.CreateFolder("%~1")
    >>"%vbs%" echo End If
    >>"%vbs%" echo set objShell = CreateObject("Shell.Application")
    >>"%vbs%" echo set FilesInZip=objShell.NameSpace("%~2").items
    >>"%vbs%" echo objShell.NameSpace("%~1").CopyHere(FilesInZip)
    >>"%vbs%" echo Set fso = Nothing
    >>"%vbs%" echo Set objShell = Nothing
    cscript //nologo "%vbs%"
    if exist "%vbs%" del /f /q "%vbs%"
    GOTO:eof
    endlocal
    

    Without that, the sub-routine didn't return back to the main part which called it.