简体   繁体   English

通过 jenkins 执行批处理命令不提取文件,尽管它在作为 bat 文件运行时提取

[英]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.我在 Jenkins 中编写了以下批处理脚本。 When I run it as a bat file in the Jenkins server workspace from the same folder, it runs without any issues.当我在同一文件夹的 Jenkins 服务器工作区中将它作为 bat 文件运行时,它运行时没有任何问题。 But when I run it through jenkins using "Execute Windows Batch Command" Its not extracting.但是当我使用“执行 Windows 批处理命令”通过 jenkins 运行它时,它没有提取。 It prints the line "about to copy from" with relevant paths and it just keeps executing from there.它打印带有相关路径的“about to copy from”行,并且它只是从那里继续执行。 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.这在作为 bat 文件运行时工作正常。 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.检查您的 zip 文件的权限,并确保它可以被运行 jenkins 的用户读取。

You must clean the workspace for each time.每次都必须清洁工作区。 If it is not a code repository you should use a jenkins workspace cleaner plugin.如果它不是代码存储库,您应该使用 jenkins workspace cleaner 插件。

Put your scripts in a batch file.将您的脚本放入批处理文件中。 and run it from jenkins "Execute Windows Batch Command" like this.并像这样从詹金斯“执行Windows批处理命令”运行它。

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

And your batch script file's look like this.你的批处理脚本文件看起来像这样。 %1 is the first parameter. %1 是第一个参数。 You should modify and add more parameters..您应该修改并添加更多参数..

xcopy %1 %2 /y

OR要么

You should use an free extracting program like 7zip tool.您应该使用免费的解压程序,例如 7zip 工具。 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.或使用批处理文件进行参数化并从 jenkins 调用 bat 文件。

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

mycustombatch.bat mycustombatch.bat

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

7z.exe usage examples: 7z.exe使用示例:

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

I had to specify GOTO:eof at the end of the sub-routine.我必须在子例程结束时指定 GOTO:eof。 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.否则,子例程不会返回到调用它的主要部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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