在下面的代码中,
def url = "${BUILD_URL}console".toURL()
def consoleOutput = url.getText('utf-8')
Groovy脚本不允许使用语法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
另一种方法:
print "${BUILD_URL}console"
def data = readFile("${BUILD_URL}console")
这给出了错误:
java.nio.file.NoSuchFileException: /app/jenkins/workspace/../folder/https:/xxxx.yy.zz.cccc.cloud/job/a/b/job/67/console
输出后:
https:/xxxx.yy.zz.cccc.cloud/job/a/b/job/67/console
如何获取Jenkins构建输出的内容?
案例1:由于错误状态,有一些groovy方法未经管理员事先批准无法访问。它必须经过一次批准,然后才能使用。 (如果您要在GUI中提供脚本而不是Jenkins文件,则可以禁用此Groovy Sandbox并使用所有方法)
案例2:
错误是您期望发生的。 BUILD_URL
保持 - 顾名思义 - 当前构建的URL。这可以在echo
中看到。在第二个命令中,您在readFile
中使用它。 ReadFile
期待一条道路作为一个论点。由于BUILD_URL
不是以/
开头,因此它被视为相对路径,因此当前工作目录是前置的。这导致groovy尝试使用路径<current_workspace><build_url>
访问文件,这显然不起作用。
无论哪种方式,我都不建议获取GUI的控制台输出,因为
.../console
不一定包含整个输出(它只包含最后n个字节;要访问完整输出,至少应该查询.../consoleFull
)我建议使用this answer中描述的REST API