not able to set environment variable using Jenkins batch command line - batch-file

I am working on Jenkins android app automation. I have automated Android version name from manifest file for example version name 1.0.1.45 and set the version name as environment variable. I have test.bat file code looks like below.
#For /F %%A In ('Powershell -C "[xml]$fC=Get-Content 'AndroidManifest.xml';"^
"$fC.manifest.versionName"') Do #Set "versionName=%%A"
#Echo %versionName% > name.txt
Now I am calling this test.bat file from Jenkins under "execute windows batch command" section and it ran but it is not able to set as environment variable.
Also I am giving svn command under "execute windows batch command" as
svn commit -m " updated version %versionName% ".
In build output it should show update version 1.0.1.25. but is not taking as a environment variable. Please can you help me

Related

Create a batch file to execute gradle build command

I want to write a batch file to trigger the gradlew shadowJar file from my machine. Currently i have two java versions in my machine, java8 and java17. My environment variable JAVA_HOME is set to Java8. But to execute the gradle i need java 11 (its requirement). By writing a new custom batch file, i will set the JAVA_HOME for Java11.
I tried to write a batch file but its not working,
#echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:/Program Files/Java/jdk-17
set PATH=C:/Program Files/Java/jdk-17/bin;
set APP_HOME=APPDIR
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
When i execute the above batch file, it's failing and its not working as expected. How can i log the execution error?

MSI installer does not install when executed from a batch file

I am currently creating an improvised installer for a cople software packages. To do this I have to install a couple MSI packages first before doing a couple file operations.
To install an MSI package I am using the following command:
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
This command works and installs the package instantly and witout any problems via CMD.
But when I put this command in my batch file and execute it as an administrator, I get the following error:
This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package
What cold be the problem? Using the same command via the console works flawlessly, only the batch file throws the error...
EDIT: I have also tried the /a parameter in order to install it as an administrator and it does not work either. Full command in batch file:
start /wait msiexec /qn /a "Myinstaller V2.1.msi"
EDIT2: I just realized that it only does not work when I start the batch file with Right click > Run as administrator
When I open a console with administrative rights and start my batch file it works for some reason...
Is there a way to make it work with the Right click > Run as administrator method?
SOLUTION: Thanks to RGuggisberg's answer I now know that the directory changes once the file is executed as an administrator. With a small change the installer gets fired up as an admin and works perfectly starting the installer from a relative path in the same directory:
#echo off
pushd %~dp0
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
pause
I've now also implemented a feature to detect wether or not the installation fails or not:
#echo off
pushd %~dp0
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
if %ERRORLEVEL% EQU 0 echo SUCCESSFULL
if NOT %ERRORLEVEL% EQU 0 echo MyProgram installation FAILED
pause
The current directory changes when you run as administrator. If you want to prove that to yourself, see this post
Difference between "%~dp0" and ".\"?
Include the full path to your filename and it will work.

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

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.

Not able to start solr in windows

I have downloaded solr 6.4.0 version and tried starting using solr start from bin directory I end up with following error:
C:\solr-6.4.0\bin>solr start
'findstr' is not recognized as an internal or external command,
operable program or batch file.
C:\solr-6.4.0\bin>
Could you please advise me is there any configuration that i need to do/ how to start?
Thanks.
The program findstr.exe can be found in C:\Windows\system32.
Check if you can find it there.
If it is available, modify your "path" environment variable to include the path "C:\Windows\system32\".
If you have an open command prompt window, exit and re-open it, and check that the %PATH% variable contains the newly added path.
echo %PATH%
Now "findstr.exe" should be available for Solr scripts.
Download solr + copy downloaded folder to c: drive (solr7.2.1)
Download nssm installer + copy downloaded folder to c: drive (solr)
open nssm installer in cmd:
>> cd C:\solr\nssm2.24\win64
>> nssm install solr7
GUI service Installer opens (Enter solr path details)
- C:\solr-7.2.1\bin\solr.cmd
- C:\solr-7.2.1\bin
Install (solr as service gets installed)
Set Environment Variable -> System Variables (JRE Path):
JAVA_HOME: c:\ProgramFiles\java\jre1.8.0_212
Check if solr is running (from services)
Open Solr:
https://localhost:8983/solr/#/
Actually findstr.exe is missing.
So follow these steps:
1.go to C:\Windows\system32\ and copy findstr.exe file.
2. paste this file into the location C:\Program Files\Java\jdk{version}\bin
Then run the this command:
solr.cmd start
Hope this helps.

SQL Server agent for scheduling SFTP using WinSCP under SSIS

I have a batch script which generates a WinSCP upload script to upload a file to an SFTP location. Now when I run the batch file via command prompt - it runs successfully and loads it. I called the same thru SSIS Execute process task - it runs successfully and loads it. Now when I put the same on SQL Agent - I tried the following two options:
Using Operating System (CmdExec) - cmd.exe /c "\.bat"
Added the SSIS package to SSISDB and added it as a job step.
With both the above options the job showed a successful run. However the file is not uploaded! Any ideas on what is happening?
Here's my batch script:
echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\\<staging path>\
SET scriptPath=\\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
#REM ftpFlags: -explicit
echo deleting uploadScript if it already exists
IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1
echo Generating WINSCP Upload Script
>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%#%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit
echo Launching WINSCP upload
start /wait %winscp% /console /script=%scriptPath%
As you start the WinSCP via the start (why?), the exit code is not propagated to the SSIS. So, you never learn, if the script fails. And it most probably fails.
You also should enable logging, so that you can see what's wrong.
You should use this code to propagate the WinSCP exit code to SSIS and to enable logging:
%winscp% /log=\\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%
(Note that the winscp.com does not have the /console parameter)
Anyway, one clear problem is that you do not specify an expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?
When testing the script, add the /ini=nul parameter to isolate the script from your configuration.
For this and other hints, when debugging WinSCP running under SSIS, see My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
And finally, see WinSCP SFTP Task for SSIS.
Your variable seems set incorrectly. To manage with a space in the path and into the variable you have to put in quotes the whole path or the whole variable.
i.e.
set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
or
set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
Another point, you have to check this file: UploadScript.txt because your script adds new lines rather than remake the file.
change this line to >%scriptPath% echo option batch abort instead of >>%...
Ah, I did not pay attention to the IF EXIST.

Resources