I am trying to execute cmd /c echo hello using groovy on a jenkins slave running on windows.
Here is my groovy:
node('WINDOWS-SLAVE-1') {
def cmd_command = "cmd /c echo hello"
cmd_command.execute()
}
And I can see in the job logs that it is indeed running on that windows slave: 'Running on WINDOWS-SLAVE-1'
But I get an error: java.io.IOException: error=2, No such file or directory
And if I try to run linux like ls -l it works fine. Shows me the files of my master.
How can I execute this CMD command on my windows jenkins slave from my groovy script?
If your intention is to execute a command on a given node, you need to use one of the Jenkins Pipeline's steps designed to execute shell scripts (e.g. sh or bat). You need to be aware that any Groovy code in your Jenkinsfile is always executed on the master node:
"1. Except for the steps themselves, all of the Pipeline logic, the Groovy conditionals, loops, etc execute on the master. Whether simple or complex! Even inside a node block!"
Source: https://jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/#fundamentals
node('WINDOWS-SLAVE-1') {
bat "cmd /c echo hello"
}
Related
I need to make an echo in windows for the next sentence:
mvn -Dmaven.wagon.http.ssl.insecure=true -q exec:exec -Dexec.executable="cmd /c echo" -Dexec.args='${project.groupId}:${project.artifactId}:'
The problem is that it detects it (-Dexec.executable="cmd /c echo") as multiple commands and returns the following error:
"ERROR] Failed to execute goal
org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on
project openregt: Command execution failed. Cannot run program "cmd \c
echo" (in directory "C:\Users\hlor\Desktop\Test Senel\main"):
CreateProcess error=2, The system cannot find the specified file ->
[Help 1]".
In linux it doesn't give me problems because echo is an executable and with just echo, it works correctly.
PS. Maven version:Apache Maven 3.2.5
Finally I have solved it with the command:
ECHO ON
More info here.
As I need to run it inside a code in Golang, I set the variable through the command:
os.Setenv("ECHO", "ON")
When I try to execute the shell script test.sh in Jenkins by adding Execute Shell in Jenkins Build configuration I don't see the shell script output in the jenkins console. I just receive that script has been executed seccessfully , but I don't see the output. My script for example is simple just `
#!/bin/bash +x
echo "hello"
and Jenkins console output is :
Started by user Andrej
Building in workspace C:\Program Files (x86)\Jenkins\workspace\test
[test] $ "C:\Program Files\Git\git-bash.exe" -xe C:\Users\ADMINI~1\AppData\Local\Temp\jenkins6727659055076114908.sh
C:\Program Files (x86)\Jenkins\workspace\test>exit 0
Finished: SUCCESS
How can I configure the jenkins that I can see all the script output and if script have receive error I get unseccessfull build message?
Make sure you set up your windows version of Jenkins to run shell scripts.
I noticed the path for your exe isn't for git or cygwin.
Check out this Stack Overflow answer for how to get windows to run shell commands in Jenkins.
I have my bash script in ${JENKINS_HOME}/scripts/convertSubt.sh
My job has build step Execute shell:
However after I run job it fails:
The error message (i.e. the 0: part) suggests, that there is an error while executing the script.
You could run the script with
sh -x convertSubt.sh
For the safe side, you could also do a
ls -l convertSubt.sh
file convertSubt.sh
before you run it.
make sure that the script exist with ls
no need to sh , just ./convertSubs.sh ( make sure you have run permissions)
I'm executing this code:
node('my_windows_slave') {
sh 'ls'
}
In my Windows slave I can properly execute sh command:
But the pipeline script can't run the .sh file:
[Pipeline] sh
[D:\workspace\sandbox_pipeline] Running shell script
sh: D:\workspace\sandbox_pipeline#tmp\durable-2d7dd2f8\script.sh: command not found
What I could notice is that this .sh file is not even created, once I tried with bat and worked fined.
Any clue what could be the problem?
[UPDATE]
Jenkins somehow can't create the SH temporary file. Already checked the log, permissions, everything that came to my mind.
I will leave my workaround as an answer for while before approve it once I'm still not 100% sure about the root cause and might someone else show up with a elegant solution...
def shell(command) {
return bat(returnStdout: true, script: "sh -x -c \"${command}\"").trim()
}
Attention
You still executing SH commands in a CMD, it means some %d for example can break your SH command.
Use the bat step instead of sh.
From Jenkins docs:
Windows-based systems should use the bat step for executing batch commands.
I'm trying to create a job in Jenkins that will execute a simple shell script. However, it seems that Jenkins isn't actually doing anything with my script. No matter what value I put into the Execute Shell Command section, Jenkins always says that it passes. Even if I put in a bogus filename like "RandomBogusFilename.sh" it'll say the job was a success.
Can anyone point out what I'm doing wrong and how I can get Jenkins to actually use my shell script?
The shell script, the job config, and the console output are all shown below. I'm currently trying to do this on a Windows Server 2008 R2 Standard machine.
Thanks.
My .sh file
File Name: surveyToolRequest.sh
File Location: /jobs/Jeff Shell Script Test/workspace
Description:
Hit a web address and retrieve the HTTP Response. Then print out the HTTP Response.
#!/bin/bash
response_code=$(curl -s -o /dev/null -w "%{http_code}" http://SOME-WEBSITE.COM)
echo "The response code is " $response_code
My Jenkins Job Config
Jenkins Console Output
I played with this and found that it worked if I specified the path to the script. If the script is in your job's workspace directory,
./surveyToolRequest.sh
should work as Jenkins looks for files relative to the root of the workspace.
It's more common to just put the contents of the script file directory into the job configuration; that way you can see what the job is doing and you'll avoid problems like this one.
You should use run "Execute windows batch command" and not "Execute Shell"