1

I have a method in a shared library in my Jenkins pipeline. The idea is to use this library and upload files to a remote host. The library is imported in a singleton library.

import com.package.jobutil.UploadFile

def uploadFunc() {
 def uploader = new UploadFile(this)
 withCredentials ([usernamePassword(credentialsId: 'user', userNameVariable: 'username', passwordVariable:'password)]) {
  uploader.uploadArtifact("${username}", "${password}", file.txt, location)
 }
}

def call() {
 uploadFunc()
}

The class that is instantiated looks like this:

class UploadFile {
   def steps

   UploadFile (steps) {
     this.steps = steps
   }

   pulic uploadArtifct (String user, String password, String file, String location) {
   Process proc
   def cred = "${user}:${pass}"
   def cmd = ["curl", "-v", "-u", cred, "--upload-file", file, location]
   steps.println "CURL: ${cmd}"

   proc = cmd.execute()
  }
}

Even though I see the println line in the logs. I do not see the curl command being executed. Is there something I am missing that does not invoke the cmd.execute to work?

EDIT

When I use the curl directly in the library, it works.

pulic uploadArtifct (String user, String password, String file, String 
  location) {
  def cred = "${user}:${password}"
  def cmd = "curl -v -u ${cred} --upload-file ${file} ${nexusLocation}/${file}"
  try {

  steps.sh cmd
  } catch (Exception e) {
    throw new RuntimeExceptipon("Cannot execute curl, exception: [${e.getClass().getName()} - '${e.getMessage()}']")
   }
  }

However, when trying to use the Process it does not work.

pulic uploadArtifct (String user, String password, String file, String 
  location) {
  def cred = "${user}:${password}"
  def cmd = ["curl", "-v", "-u", cred, "--upload-file", ${file}, ${location}]
  try {
   def sout = new StringBuffer(), serr = new StringBuffer()
   def proc = cmd.execute()
   proc.consumeProcessOutput(sout, serr)
   proc.waitForOrKill(1000)
   println sout
  } catch (Exception e) {
    throw new RuntimeExceptipon("Cannot execute curl, exception: [${e.getClass().getName()} - '${e.getMessage()}']")
   }
  }

The exception I get is:

java.lang.RuntimeException: Cannot execute curl, exception: [groovy.lang.MissingMethodException - 'No signature of method: java.lang.String.div() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [file.txt]
SSF
  • 915
  • 5
  • 23
  • 47

1 Answers1

1

As explained here, you need to capture stdout/stderr to see anything.

At the very least:

def outputStream = new StringBuffer();
proc.waitForProcessOutput(outputStream, System.err)
//proc.waitForProcessOutput(System.out, System.err)

Or, as in this gist:

def sout = new StringBuffer(), serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println sout

An example of blocking call would be:

println new ProcessBuilder( 'sh', '-c', 'du -h --max-depth=1 /var/foo/bar/folder\\ with\\ spaces | sort -hr').redirectErrorStream(true).start().text

def cmd = ["curl", "-v", "-u", cred, "--upload-file", ${file}, ${location}/${file}]
No signature of method: java.lang.String.div() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [file.txt]

The '/' in '${location}/${file}' is interpreted as an '/ (div) operation instead of a string.

Try instead for that curl command argument:

${location}+"/"+${file}

As noted in your subsequent question, the all path needs to be between double-quotes.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. That seems to at least show me the error. I placed the above in a `try` statement and got this. `java.lang.RuntimeException: Cannot execute curl, exception: [groovy.lang.MissingMethodException - 'No signature of method: java.lang.String.div() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [file.txt]` – SSF Jun 13 '19 at 07:48
  • Seems like the file is not uploading the file. Is there a specific thing I need to do `proc` to enable upload? – SSF Jun 13 '19 at 07:50
  • @SSF The error message looks like a type conversion error: https://stackoverflow.com/q/49711193/6309 – VonC Jun 13 '19 at 07:51
  • @SSF Do a `curl -v` (verbose mode: https://ec.haxx.se/usingcurl-verbose.html): you will have more clues. – VonC Jun 13 '19 at 07:51
  • I am doing a `-v`. Also when I do `steps.sh cmd` where `cmd= "curl -v -u ..."` it works. But when I use `proc = cmd.execute()` does not work. – SSF Jun 13 '19 at 08:00
  • @SSF Just to be sure, could you edit your question with the code you are now testing? – VonC Jun 13 '19 at 08:06
  • 1
    @SSF OK, I have edited my answer to address your error message. – VonC Jun 14 '19 at 04:51
  • Ok, I don't think I needed the `/` in the `curl` command. However, I still cannot upload. The error is: `cannot open file.txt`. Do I need to stream in the content of the file or something? – SSF Jun 18 '19 at 00:39
  • I posted another question with the latest changes. Appreciate it if you could look into it too. https://stackoverflow.com/questions/56640699/upload-local-file-by-using-curl-command-in-shared-library-jenkins-pipeline – SSF Jun 18 '19 at 01:35
  • @SSF Sure, sorry that was in the middle of the night for me. I have posted an answer. – VonC Jun 18 '19 at 05:00