I'm trying to run a batchscript present inside the workspace of jenkins. I have written a groovy script as below to do this
stage('batchscript') {
steps{
bat 'start cmd.exe /c C:\\Program Files (x86)\\Jenkins\\workspace\\jenkins Project\\batchfile.bat'\
}
}
when I build the job it should open a new command window and run my batch file in a new command prompt executing all the bat commands. The build is succesful but no command window opens up. Any suggestion will be helpfull
The number of reasons why this doesn’t work is large. Very large. You need to supply more context to get a better answer. However, one thing you could check is that your Jenkins instance is running on Windows. If it isn’t, you can’t expect a .bat file to work. If you are running on a *nix system, make sure your script has the execution flag set.
If your pipeline will run on MS Windows you'll need to use the bat command. Naturally the commands you pass to these will also need to make sense on the specific operating system. In this example first we use the internal echo command of Jenkins. Then we call sh and run the echo of our Unix shell.
I have created a powershell script which will transfer (using winscp.dll) the files from Jenkins windows server to Linux server. In Jenkins batch command, I have executed that powershell script and it works fine. But when i tried the same in Jenkins pipeline job, it calls the powershell script and comes to the next step.
In this example first we use the internal echo command of Jenkins. Then we call sh and run the echo of our Unix shell. Then we execute the hostname command and finally the uptime command.
Jenkins is aimed to execute shell commands in background mode, not for interactive(UI) mode. When you run start cmd.exe /c c://some/app.exe
a new cmd UI is opened and this will never happen in jenkins.
If you need to execute a simple batch commands with jenkins :
stage('build') {
cmd_exec('echo "Buils starting..."')
cmd_exec('echo "dir /a /b"')
}
def cmd_exec(command) {
return bat(returnStdout: true, script: "${command}").trim()
}
Here a advanced example :
steps {
echo 'Deploy to staging environment'
// Launch tomcat
bat """
cd c:\\qa\\bin
dir /a /b
startup
"""
bat """
cd c:\\qa\\bin
startup
"""
// Code to move WAR to Tomcat
bat "xcopy /y c:\\webapp\\target\\webapp.war ..."
bat "xcopy /y c:\\webapp\\target\\webapp.war ..."
}
Example:
If you need to execute a batch file with jenkins :
stage('build') {
dir("build_folder"){
bat "run_build_windows.bat"
}
}
or
stage('build') {
bat "c://some/folder/run_build_windows.bat"
}
Windows paths some time are bizarre :s . Anyway, linux is the best choice to host jenkins.
Reference: https://thenucleargeeks.com/2020/11/24/how-to-run-batch-scripts-commands-in-jenkinsfile/
node {
stage('Preparation') {
//Preparations and checkout the code
}
stage('Build') {
//Build command
}
stage('Post build action'){
bat ''' ECHO Hello World '''
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With