Jenkins not able to run Poetry in batch file. Works when run manually - batch-file

I'm attempting to get Jenkins to run a .bat file which contains a call to Poetry (for package management).
However, when running this .bat file via Jenkins, I'm getting a "'poetry' is not recognized as an internal or external command, operable program or batch file." error.
I believe this is due to the .bat file failing when it attempts to run a "call poetry check" command.
The .bat file runs successfully when run manually.
Is there some obvious feature of Jenkins that I'm missing here?
I'm aware that there is an alternative method of embedding poetry into Jenkins with direct commands. However this project is about to be passed on to someone else, and they would not be able to maintain that.
I've reproduced the key code below (the Jenkins code calling the .bat file, the code in the .bat file and the error output from Jenkins).
Jenkins code (standard pipeline with default settings):
pipeline {
agent any
stages {
stage('Main') {
steps {
bat 'E:\\project-folder\\project_run.bat'
}
}
}
}
Bat file code:
#echo off
cd /d "E:\project-folder\"
call poetry check && (
echo poetry checked
) || (
echo Problem with poetry
EXIT /B
)
echo Installing prod packages
call poetry install --no-dev && (
echo Production packages successfully installed
) || (
echo Problem installing packages to environment
EXIT /B
)
call python local-program.py
EXIT /B
)
Jenkins Error:
C:\Windows\system32\config\systemprofile\AppData\Local\Jenkins.jenkins\workspace[Pipeline_Name]>E:\project-folder\project_run.bat
Moving to project directory
'poetry' is not recognized as an internal or external command,
operable program or batch file.
Problem with poetry

Windows is looking up executables on the path you specify. If you run a command without a path, Windows looks for the command/executable in all directories specified in the environment variable PATH.
Very likely your Jenkins is running with different path settings than your interactive shell.
What you can do:
specify the full path to the command you want to raise in Jenkins
set the PATH variable in the Jenkins job prior to running the command
set the PATH variable before invoking Jenkins
Apart from the directory, Windows also has to check extensions. If you run poetry, Windows may have to find poetry.com, poetry.exe, poetry.bat or poetry.cmd. I suspect one of these files reside in E:\project-folder. To understand which files Windows checks for and the preference, have a look at the environment variable PATHEXT.
So to better understand these differences you can run the command set without parameters. Do that on the command line, and add the same command into your Jenkins job.

Related

Batch-file to update files from SVN and then run a program

Currently am executing a program from command prompt, basically a Python command, but I want that to be automated so that the program runs the latest version of the files before the program starts.
So current program is run by saying:
python c:\program\mysoftwareUI.py
mysoftwareUI.py is present in folder that constantly is provided with updates
Now I want to write a batch file with the above command but before that also SVN command that updates the folder before the actual program is run.
How can I do this?
Create a .bat file script and place it into a directory that is in the PATH variable or the current working directory.
=== doit.bat
#ECHO OFF
PUSHD "c:\program"
svn up
python c:\program\mysoftwareUI.py
POPD
EXIT /B

Executing FART.exe (find and replace text) commands on remote server through SSH

I'm executing a batch file on a remote server from our build server (Jenkins) through SSH. The batch file uses fart.exe commands to find and replace text. I have placed the fart.exe in C:\Windows\System32 and I'm invoking it as C:\windows\system32\fart.exe in the batch file.
The command works perfectly fine oin the remote server, but when invoking through SSH I get an error in the Jenkins log as:
'"C:\windows\system32\fart.exe"' is not recognized as internal or external command.
This is the only error I'm getting and the other commands successfully execute in the batch script. Both of the servers are Windows Server 2012 R2.
I tried adding the path to system variable but it didn't work.
This is how the Fart.exe is used in the batch script.
for /R "%BACKUP_SOURCE%" %%G in (%ConfigFile%) do (
"C:\windows\system32\fart.exe" "%%G" %PlaceHolder% %AppPath%
)
Invoking From Jenkins
I don't think the way i invoke the batch script do matter, because it is done trough the Jenkins SSH plugin. The batch script get invoked successfully. The error i get is from when executing the Fart.exe
I tried invoking a different command of an exe located in the same path, and that is successfully invoked. so i guess the issue is isolated to FART.EXE.

Trying to uninstall a program with wmic and getting error 1603

I'm try to create a batch file that will, among other things like installing the newest version of our software, first UNINSTALL the old version. I have used "wmic product get name" to find the actual name of the program, and then I have scripted the following code to uninstall the program:
wmic product where "name like 'Borland CaliberRM 10.1'" call uninstall /nointeractive >> C:\users\pbrandvold\Desktop\log.txt
When it's finished, I get this message:
Executing (\\PHIL-BRANDVOLD\ROOT\CIMV2:Win32_Product.IdentifyingNumber="{ED8B0A1F-8E90-478A-82B6-7C885A628257}",Name="Borland CaliberRM 10.1",Version="10.1.0.84")->Uninstall()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 1603;
};
I can't think of what is happening - why won't this uninstall? I've also tried using the msi, and running:
msiexec.exe /qn /x "Borland CaliberRM 10.1.msi"
Which doesn't work either.
I had this issue when trying to uninstall and reinstall Web Deploy using wmic via a batch file. It did not help running the batch file with elevated privileges.
The only way I could get it to run correctly was to open a Command Prompt window with elevated privileges and run the batch file through that.
The solution I ended up going with was to turn my batch file into an executable using a Bat to Exe converter. http://www.f2ko.de/en/b2e.php
The issue occurs due to the user's privileged of uninstalling the program.
Just open the command prompt as 'Run as administrator and run your command to uninstall the program.
Example :
open cmd with admin:
run below commands
wmic
product get name
product where name="YOUR_PROGRAM_NAME" call uninstall

Execute windows batch command from Jenkins fails but runs fine in cmd.exe

I am trying to run this command in jenkins after a MSbuild
xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Y:\Extraction_Zone\Jenkins\" /E
Y: is a mapped network drive. This runs fine in cmd.exe but when trying to run it in Jenkins, I am getting the error Invalid drive specification.
Here is the output from jenkins:
Time Elapsed 00:00:04.03
[trunk] $ cmd /c call C:\Windows\TEMP\hudson3389873107474371072.bat
C:\Program Files (x86)\Jenkins\workspace\trunk>xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Y:\Extraction_Zone\Jenkins\" /E
Invalid drive specification
0 File(s) copied
C:\Program Files (x86)\Jenkins\workspace\trunk>exit 4
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
Any help would be appreciated.
I too had a similar issue once. Try granting the Jenkins service "Logon as This account" right under services.msc and make sure the account you type there is the same as the one you use for running cmd.exe.
These commands based on Java JAR files worked for me:
cmd
net use x: \\
xcopy "dist\" x:\ /Y
And that's it! I spent lot of time figure out this issue and nothing worked until I wrote CMD and NET USE!
Neither I didn't need to change permission on jenkins service nor use runas command.
But I must mention that everyone had read and write access to the network drive.
I had the same issue with my Windows Task running a batch file (I know it is not exactly same) where I tried to copy file to network location i.e. shared drive. I used the UNC path and mapped drive as well but the error was same. For me it was error number 4 - MS DOS error code.
The solution was to use net use command! Hope that it helps.
Easy fix for most things.
Make a batch command with what your trying to run, filename.bat with the command prompt text inside.
Make a normal windows shortcut for the batch command, edit the shortcuts advanced properties and check the "Run as admin" (tricky tricky).
Now run the filename.lnk shortcut from jenkins command line call, this will get you around all the jazz.
:)
The solution of adarshr (i.e., modifying the log on credentials of the service) has worked for me for a part of the problem: in my case, this allowed me to successfully check out a mercurial repository (using ssh protocol), which I could not do when using 'Local System account'.
However, I still have different behavior between running a command-line script or running the same script from a jenkins 'execute shell' script in the build section. In my case, I compile a Python extension. In Jenkins, I cannot import the extension (I don't see any error, but the execution simply stops, so I suspect it crashes).
If I uninstall the service and run the slave agent as a Java Web Start, I do get the same behavoir. It is a temporary fix for me, but it means that when I reboot the windows build machine, I have to manually re-start the Java Web Start application.
So -at least in my case- it is clear that this is a credential problem.
Credentials usage documentation: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
I've solved my issue with the CIFS plugin.
Faced similar issue and found two ways to solve.
Type 1:
Tell Jenkins about mapped drive.
1.Goto -> Manage Jenkins -> Script Console (Groovy Script).
2.Run below command
def mapdrive = "net use Y: \\\\copy_nework_address"
mapdrive.execute();
println "net use".execute().getText()
Type:2
1.Goto -> cmd -> run "net use" to know network address
xcopy "C:\Program Files (x86)\Jenkins\workspace\trunk\Projects\results\results\obj\Debug\Package\PackageTmp" "Copy_Network_Address\Extraction_Zone\Jenkins\" /E
Conclusion:- I prefer 2nd types as after every restart i should run Groovy Script.

Can't run msi via two batch files

(For a colleague)
I have two batch files: let's call them A.bat and B.bat.
B.bat runs an msi. A.bat invokes B.bat.
If I run B.bat directly, it works fine, but if I run via A.bat, I get the following error message:
msiexec is not recognized as an internal or external command, operable program or batch file.
I've tried the three solutions proposed here, with no success:
Register/repair Windows Installer Service
Reregister/reinstall the Windows Installer
Check/Reregister/reinstall the Windows Installer
Just a minor detail, I'm on Windows XP embedded... on a 'normal' XP it works OK
To avoid the issue of an incorrect DOS path, you can launch msiexec via the builtin start command which will execute the command in a new window with clean environment variables.
For example:
start msiexec ...
Here's a fully fledged example:
C:\>set PATH=an-invalid-path
C:\>notepad
'notepad' is not recognized as an internal or external command,
operable program or batch file.
C:\>start notepad
Belatedly converting #Damien's comment to an answer:
Don't use %PATH as a variable name!

Resources