简体   繁体   中英

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

Check the permissions of your zip file and make sure it is readable by the user jenkins runs under.

You must clean the workspace for each time. If it is not a code repository you should use a jenkins workspace cleaner plugin.

Put your scripts in a batch file. and run it from jenkins "Execute Windows Batch Command" like this.

call "C:\Scripts\mycustombatch.bat" myparameter1 "myparameter2"

And your batch script file's look like this. %1 is the first parameter. You should modify and add more parameters..

xcopy %1 %2 /y

OR

You should use an free extracting program like 7zip tool. After you have installed it, you should use the following command directly.

"C:\Program Files\7-Zip\7z.exe" e "C:\myzipfile.7z" -o"C:\ExtractedFolder" *.* -r -y

or parameterize with batch file and call bat file from jenkins.

call "C:\Scripts\mycustombatch.bat" "%WORKSPACE%\myzipfile.7z" "C:\ExtractedFolder"

mycustombatch.bat

cd "C:\Program Files\7-Zip"
7z e %1 -o%2 *.* -r -y

7z.exe usage examples:

http://www.dotnetperls.com/7-zip-examples

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM