在 Job DSL 上使用文件特征

Use file features on Job DSL

我正在使用 Job DSL,我想下载一个文件,阅读它,然后设置一些环境变量。

def static setSecrets(Job delegate, Map overrides = [:]) {
    def liveUsername
    def livePassword
    def file
    new URL('https://path/file').withInputStream { i ->
        file.withOutputStream {
            it << i
        }
    }
    file.withReader { liveUsername = it.readLines().get(0) }
    file.withReader { livePassword = it.readLines().get(1) }

    def options = [
            IDENTITY_USER: liveUsername,
            IDENTITY_PASSWORD: livePassword]

    setEnv(delegate, options, overrides)

}

这是我收到的异常

java.lang.NullPointerException: Cannot invoke method withOutputStream() on null object

似乎无法使用文件的功能。但是在 groovy 文件中我期待可以使用作业 DSL 模板以及所有 groovy 功能。

文件为 null,因此当您在其上调用方法时抛出 NPE

def file
...
file.withOutputStream { // BANG!