0

I've been having some problems with a groovy script;

sh '''
    echo \'Starting script\';
    sleep 5;
    echo \'pwd\';
    sleep 5; 
    pwd;
    sleep 5;
    find / -name jenkins.war -type f;
    sleep 5;
'''

Commands that take longer to execute always cause the pipeline to fail; all commands except find are run successfully. In the logs (browser), a number of files are shown before the pipeline fails with message:

Jenkins build has status: FAILURE. Notifying  Bitbucket with state: FAILED.

The issue is that there is no direct access to the jenkins server (no easy way to check logs).

I'm not sure if I'm going about this the right way but are there any settings that could cause such behavior?

Sebi
  • 4,262
  • 13
  • 60
  • 116
  • What do you do with the `find ...`result? Just print it in the build's Console Output or is there more surrounding pipeline code you don't show us? Finding from the root dir can take a while. Chances are that you run into a server's timeout or that the user which runs the script doesn't have permission to access certain dirs/files. I'd try to return `stderr` acoording to the answer to [How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step](https://stackoverflow.com/a/68967643/1744774). – Gerold Broser Oct 29 '21 at 15:17
  • @GeroldBroser yes, the result is used to display version (java -jar `find / -name jenkins.war -type f` --version). Yes there are files for which find gives `permission denied` , But it's strange that a number of such files with no permissions are still printed to the logs. – Sebi Oct 29 '21 at 18:53
  • Why is this strange? That's `find`'s output to `stderr`. I'd definitely like to see the error output of _any_ command (or _any_ program, for that matter) in the log(s) in case something goes wrong. – Gerold Broser Oct 29 '21 at 20:36
  • That's not a groovy script. It's a series of Unix commands within a pipeline e execute she'll step. @gerold-broser provides an actual groovy script, but you don't explain what you are really trying to achieve, and I think you are missing something more to the script since that should not produce a failure. – Ian W Oct 30 '21 at 05:40
  • 1
    @IanW If you refer to the _[jenkins-]groovy_ tags: Pipeline steps (here `sh`) technically are Groovy methods (though that's not the cause of the issue here). – Gerold Broser Oct 30 '21 at 05:52
  • `sh` in this context is an [Execute Shell](https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script) step of the [pipeline workflow syntax](https://www.jenkins.io/doc/book/pipeline/syntax/). The syntax is a groovy-based DSL, not groovy. The OP's question is about the commands within the step `step ``` ... ``` `, which are simple UNIX shell commands. Within the step, only the last command's result (ExitCode) is relevant and `sleep 5;` would never return non-zero. That suggests there's more to this. OP should post the entire stage/steps. – Ian W Oct 30 '21 at 06:24
  • @IanW A DSL in a certain programming language neither changes the syntax of that language nor introduces a new one. It's just a certain concept on top of that language, i.e. how to use the features of that language in a certain way. See, for instance, [Groovy positional and named parameters for methods](http://www.groovy-lang.org/objectorientation.html#_positional_parameters). That's exactly what you define in a Jenkins pipeline step. – Gerold Broser Oct 30 '21 at 18:29
  • @IanW The problem is not that the script is not executed but that some commands cause the pipeline to fail: `which perl python java` always causes a failure; same goes with `find` when pointed to a root directory; my guess is that jenkins terminates on stderr but am not sure. – Sebi Nov 01 '21 at 11:00
  • 1
    @Sebi Jenkins does _not_ terminate on stderr. See [How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step](https://stackoverflow.com/q/68967642/1744774). – Gerold Broser Nov 01 '21 at 13:01

1 Answers1

1

There's a more elegant, more accurate and much more performant way to get Jenkins' version:

    stages {
        stage('Jenkins version') {
            steps {
                print "${Jenkins.getInstance().getVersion()}"
            }
        }
    }

Console Output

...
[Pipeline] stage
[Pipeline] { (Jenkins version)
[Pipeline] echo
2.303.2
...

Note: getInstance() and getVersion() have to be approved by a Jenkins admin.

You could also curl https://JENKINS_URL/about/ and extract the element/line containing <td>org.jenkins-ci.main:jenkins-war:2.303.2</td>.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107