paul paul - 14 days ago 9
Groovy Question

Use file features on Job DSL

I´m using Job DSL and I would like to download a file, read it, and set some env variables.

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)

}


This is the exception that I´m receiving

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


Seems like the features of file cannot being used. But being in groovy file I was expecting can use the Job DSL templates and also, all the groovy features.

Answer

File is null so is throwing NPE when you call a method on it

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