0

Hi I have the following groovy script that I want to use to build a project in Jenkins

#!/usr/bin/env groovy

def deploymentPaths = '[]'

pipeline{
  agent {
    label 'jenkins-agent'
  }
  stages{
    stage('Build debug'){
        steps{
            sh './make_debug.sh'
        }
    }
  }
}

How can I make it wait till make_debug finishes and print its output? I tried

#!/usr/bin/env groovy

def deploymentPaths = '[]'

pipeline{
  agent {
    label 'jenkins-agent'
  }
  stages{
    stage('Build debug'){
        steps{
            def sout = new StringBuilder(), serr = new StringBuilder()
            def proc = './make_debug.sh'.execute()
            proc.consumeProcessOutput(sout, serr)
            proc.waitForOrKill(10000)
            println "out> $sout\nerr> $serr"
        }
    }
  }
}

But this doesn't work. Got following errors:

WorkflowScript: 13: Expected a step @ line 13, column 7.
            def sout = new StringBuilder(), serr = new StringBuilder()
         ^
WorkflowScript: 13: Expected a step @ line 13, column 39.
            def sout = new StringBuilder(), serr = new StringBuilder()
                                         ^
WorkflowScript: 14: Expected a step @ line 14, column 7.
            def proc = './make_debug.sh'.execute()
         ^
WorkflowScript: 15: Method calls on objects not allowed outside "script" blocks. @ line 15, column 13.
               proc.consumeProcessOutput(sout, serr)
               ^
WorkflowScript: 16: Method calls on objects not allowed outside "script" blocks. @ line 16, column 13.
               proc.waitForOrKill(1000)
               ^
WorkflowScript: 12: Missing required parameter: "delegate" @ line 12, column 5.
          step{
       ^
WorkflowScript: 15: Arguments to "error" must be explicitly named. @ line 15, column 13.
               proc.consumeProcessOutput(sout, serr)
               ^
WorkflowScript: 16: Expecting "class java.lang.String" but got "1000" of type class java.lang.Integer instead @ line 16, column 32.
               proc.waitForOrKill(1000)
ycr
  • 12,828
  • 2
  • 25
  • 45
W Nguyen
  • 45
  • 7
  • Why you are not using standard `sh script: './make_debug.sh'` ? – daggett Sep 27 '22 at 06:53
  • My organization requires groovy script. – W Nguyen Sep 27 '22 at 06:54
  • To run .sh script you need to use shell runner for example `bash ./make_debug.sh` . Also when you are saying "it's not working" - what does it mean? Any error message? – daggett Sep 27 '22 at 06:59
  • you have missing `script` step. https://www.jenkins.io/doc/book/pipeline/syntax/#script – daggett Sep 27 '22 at 08:04
  • Have a look at: https://stackoverflow.com/questions/55508871/how-to-fix-pipeline-script-expected-a-step-error/55510151#55510151 – Michael Kemmerzell Sep 27 '22 at 13:08
  • I added script{}. And replaced def proc = './make_debug.sh'.execute() with def proc = sh(script: './make_debug.sh', returnStdout: true). Hovewer it results in + ./make_debug.sh /tmp/workspace/tibranch_pipeline_jenkins_test01@tmp/durable-92efdbf4/script.sh: line 1: ./make_debug.sh: No such file or directory However make_debug.sh is in same directory as groovy file. – W Nguyen Sep 29 '22 at 09:26

0 Answers0