-1

Case 1

In the below code,

def url = "${BUILD_URL}console".toURL()
def consoleOutput = url.getText('utf-8')

Groovy script does not allow to use the syntax toURL():

Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods toURL java.lang.String. Administrators can decide whether to approve or reject this signature.
Error cause: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods toURL java.lang.String

Case 2

Another approach:

print "${BUILD_URL}console"
def data = readFile("${BUILD_URL}console")

which gives error:

java.nio.file.NoSuchFileException: /app/jenkins/workspace/../folder/https:/xxxx.yy.zz.cccc.cloud/job/a/b/job/67/console

after the output:

https:/xxxx.yy.zz.cccc.cloud/job/a/b/job/67/console

How to get the contents of Jenkins build output?

overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

2

Case 1

As the error states there are several groovy methods that can not be accessed without prior approval of an admin. It has to be approved once and can then always be used. (If you were to supply the script in the GUI instead of a Jenkinsfile, you could disable this Groovy Sandbox and use all method)

Case 2

The error is what you would expect to happen. The BUILD_URL holds - as the name implies - the URL of the current build. This can be seen in the echo. In the second command you use it inside of readFile. ReadFile expects a path as an argument. Since the BUILD_URL does not start with a / it is treated as a relative path, thus the current working directory is prepended. This leads to groovy trying to access a file with the path <current_workspace><build_url> which obviously does not work.

Either way I would not recommend fetching the console output of the GUI because

  • you would also receive the GUI not only the console output
  • .../console does not necessarily contain the whole output ( it only contains the last n Bytes; to access the full output you at least should query .../consoleFull)

I would instead recommend using the REST API as described in this answer

jBuchholz
  • 1,742
  • 2
  • 17
  • 25