Jenkins stores shell output in var

I am trying to check the md5sum of a file and export Okay to var. Then check with var to mark it if it doesn't complete the build.

How do I save the md5checksum result to a variable that I can check in Jenkins.

I've found this before, but it doesn't work. I get a message from jerkins anytime I try to run a script.

md5Check = sh( script: 'md5sum -c ${env.SSH_HOME}/MD5SUM.MD5', returnStdout: true ).trim()
sh "sudo ssh -i ${env.SSH_KEY} ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; echo ${md5Check}\""

      

It doesn't like the first line. Is there any other way to do this?

Mistake:

WorkflowScript: 44: Expected a step @ line 44, column 17.
               md5Check = sh "sudo ssh -i ${env.SSH_KEY} 
${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; md5sum -c 
${env.SSH_HOME}/MD5SUM.MD5;\""
               ^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)

      

UPDATE:

I was able to fix this with the two answers below, but now the sh command is run on the Jenkins side and not via ssh.

I also found that I need to wrap the code in a script and node in order for it to run the script.

script{
    node(){
        unstash 'build'
        env.FAIL=sh([script: "md5sum -c ${env.SSH_HOME}/MD5SUM.MD5", returnStdout: true ]).trim()
        sh "sudo ssh -i ${env.SSH_KEY} ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; echo ${env.FAIL}\""
    }
}

      

So with echo $ {env.FAIL} it calls the correct command, but can't find the file because I think its running in Jenkins shell is not ssh.

UPDATE 2

Ok, so if I ssh the file into a specific script and then just echo from the Jenkins shell, it finds the file on the remote server as expected. Here is the last code I used.

script{
    node(){
        unstash 'build'
        env.FAIL=sh([script: "sudo ssh -i ${env.SSH_KEY} ${env.SSH_URL} -tt \"cd ${env.SSH_HOME}/; md5sum -c ${env.SSH_HOME}/MD5SUM.MD5\"", returnStdout: true ]).trim()
        sh "sudo echo ${env.FAIL}"
     }
}

      

+3


source to share


2 answers


It looks like you are missing an internal array to run your script:

sh([ script: 'md5sum -c ${env.SSH_HOME}/MD5SUM.MD5', returnStdout: true ]).trim()

      



Whenever I was setting variables using a script in a Jenkins script script, I did it like this:

env.V5_DIR = WORKSPACE + '/' + sh([script: "basename ${V5_GIT_URL} .git", returnStdout: true]).trim()

      

+4


source


I wonder if your problem is that on the first line you are using a single quote when a binary variable is used instead of a double quote. those. try



md5Check = sh( script: "md5sum -c ${env.SSH_HOME}/MD5SUM.MD5", returnStdout: true ).trim()

      

+1


source







All Articles