My shell command works when run in the terminal, but not in a Jenkins pipeline; when the pipeline runs test.sh, the shell is run, but the bug service is not. Why is that, and how can I fix this?
run.sh:
nohup /usr/local/java/1.8/bin/java -Dspring.cloud.config.profile=test -Dspring.cloud.config.uri=http://localhost:8888/ -cp /data/jcpt/service/service-account/jar/.:/data/jcpt/service/service-account/jar/lib/* com.caifubao.jcpt.account.app.AccountApplication >./logs/service-account.log &
Jenkins pipeline:
stage('UPLOAD') {
agent{node { label "TEST" }}
steps {
sh "./test.sh"
}
}
Related
I'm running a declarative Jenkins pipeline on Ubuntu 18.04 slave. My issue is that whenever I'm trying to provide arguments from a sh step to a bash script, the arguments are not there. When running the exact same commands from either a terminal directly or another script file (similarly how Jenkins does it via temp file) the arguments work fine.
The Jenkinsfile looks something like
pipeline {
agent { label "ubuntu" }
options { timeout(time: 1, unit: 'HOURS') }
stages {
stage('Build') {
steps {
sh """
#!/bin/bash
...
. ./Scripts/install_tools.sh "force"
"""
}
}
}
}
The pipeline itself runs smooth and does what I need it to do. The problem is that when calling install_tools.sh no arguments are found. The script looks something like
#!/bin/bash
echo "Running $0"
echo "Args: $#"
...
The line echo "Args: $#" I have tried also with $* and $1 - Each time the arguments are returned as empty, but only when running from the pipeline. It seems to me that this is related to some Groovy stuff, but I have no clue what.
How do I call a bash script during the pipeline and get the arguments passed properly?
put #!/bin/bash into the first line
sh """#!/bin/bash
. ./Scripts/install_tools.sh "force"
"""
otherwise . (dot) command could have different meaning
I have a task in a build.gradle file, in which I'd like to run this command:
(export ENV_VAR=/my/path ; /path/to/build.sh)
I tried running this in gradle:
task myTask {
doLast {
exec {
commandLine ['bash', '-c', '"(export ENV_VAR=/my/path ; /path/to/build.sh)"']
}
}
}
Unfortunately, I have an error that says
Successfully started process 'command 'bash''
bash: (export ENV_VAR=/my/path ; /path/to/build.sh): No such file or directory
Now I'm sure the file exists and the specified paths are correct. Running this command manually in the terminal works.
Is there something in gradle that makes a bash -c like this not work? I can't think of another way to make an export like this otherwise.
Try without the extra quotes:
commandLine ['bash', '-c', '(export ENV_VAR=/my/path ; /path/to/build.sh)']
When you run that in the command line, your shell needs the quotes to pass to the command (which happens to be bash) as a single argument, but gradle is already doing that with that syntax, so bash is receiving literally one argument "(export ENV_VAR=/my/path ; /path/to/build.sh)" and since it does not recognize this as internal syntax, tries to run a command with this name.
I've a big Jenkins pipeline and when build is run, lot of console output is generated which causes space issue on Jenkins master.
I've following code in Jenkins pipeline with Shell Script, which logs every file being removed. I've lots of log files that cause lot of console output -
stage('Logs Cleanup') {
steps {
script {
sh '''rm -rf /home/oracle/test/logs1/* /home/oracle/test/logs2/*'''
}
}
}
Is there any way I can suppress output of that command?
NOTE: If same command it run from Terminal, it logs nothing in output.
For your specific delete using Jenkins only:
stage('Logs Cleanup') {
steps {
dir ('/home/oracle/test/logs1/') {
deleteDir()
}
dir ('/home/oracle/test/logs2/') {
deleteDir()
}
}
}
Looks like there are some comments that solve the problem for you, but no one mentioned that you can control the output of sh commands through the command itself.
The sh command has some optional parameters that can be used; one of them is returnStdout. In your case, you can suppress stdout like this:
stage('Logs Cleanup') {
steps {
script {
sh script: 'rm -rf /home/oracle/test/logs1/* /home/oracle/test/logs2/*', returnStdout: false
}
}
}
There are some other useful parameters, for example returnStatus will return the status code of the command for use in the pipeline.
This question already has answers here:
Hiding password in Jenkins pipeline script
(2 answers)
Closed 3 years ago.
I have setup a pipeline script, that will build a JAR using maven and the parameters are coming from Jenkins for the JAR. One of which is a password.
stages {
stage('Maven Build') {
steps {
script {
echo "Maven Build"
withMaven(jdk: javaVersion, maven: mavenVersion) {
sh 'mvn clean install'
}
}
}
}
stage('Branch Creation') {
steps {
script {
sh """
set +x
"java -jar target/github-branch-creator-1.0-SNAPSHOT-jar-with-dependencies.jar ${params.Username} ${params.Password} ${params.value1} ${params.value2} ${params.value3}"
"""
}
}
}
}
I did mention """ triple double quotes to accept the parameters from the jenkins, and did set +x, but it would still print the command in the console.
+java -jar target/github-branch-creator-1.0-SNAPSHOT-jar-with-dependencies.jar abc sensitive_password v1 v2 v2
I tried #!/bin/bash +x on top of my groovy script. I still found it to be printing.
If someone can help?
I think it would be better if you don't pass the password in plain text.
Ways you can try:
Either encrypt the password then pass the arguments to command line, you can write to decrypt the same while using it in your java code.
Write the command in file then execute the file.
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.