bmustiata / jenny

Command line Jenkinsfile runner written in groovy. Does not need a Jenkins installation to run the Jenkinsfile.
BSD 3-Clause "New" or "Revised" License
91 stars 8 forks source link

JOB_* variables are not exported (variables and startsWith) #57

Closed petersenna closed 5 years ago

petersenna commented 5 years ago

While this works just fine on Jenkins, Jenny does not like it.

$ cat Jenkinsfile
#!/usr/bin/env groovy

osname = 'linux'

def created = env.JOB_NAME.startsWith("${osname}-")

It fails with:

Cannot invoke method startsWith() on null object

And the issue is the variable as if I replace ${osname} by linux- then it works just fine.

bmustiata commented 5 years ago

Hm, can you paste a small Jenkinsfile? The error seems to suggest that the JOB_NAME is null. Which is probably true. A W/A would be to use the env setting in jenny, and define JOB_NAME.

I'm using variable interpolation a lot, and I have no problems.

Either: jenny -e JOB_NAME=abc or having in the .jenny/config an entry for the environment. I'm going to check if Jenkins does have by default JOB_NAME, and ensure jenny also exports it.

petersenna commented 5 years ago

the thing is that the Jenkins file in question has a lot of variable definitions inside it, and then later refers to the variable using ${} notation. So on that example ${osname} is not expanding to 'linux'.

bmustiata commented 5 years ago

@petersenna Hm, this is strange.

In the error I see, startsWith() is called on a null object. The null object is actually in the example you sent me: env.JOB_NAME. That's true, because atm jenny is not exporting any JOB_* variables.

The other thing you mention is the string variable interpolation. I use that feature extensively in my own Jenkinsfiles so I would be very surprised for it not to be working.

bmustiata commented 5 years ago

Variables that should be exported are: JOB_BASE_NAME, JOB_DISPLAY_URL, JOB_NAME, and JOB_URL. DISPLAY_URL should be the URL with '/display/redirect' appended.

bmustiata commented 5 years ago

I'm exporting now the variables. I've added the string interpolation you provided also in the test, to ensure that all should work well for you:

https://github.com/bmustiata/jenny/blob/60f64731e0c0e2b07a6d386df4ba4007eb185fd2/features/build-environment-variables/Jenkinsfile#L2-L6

The output is correct:

https://github.com/bmustiata/jenny/blob/60f64731e0c0e2b07a6d386df4ba4007eb185fd2/features/build-environment-variables/jenny-expected.txt

petersenna commented 5 years ago

It solved the issue, thank you!

bmustiata commented 5 years ago

Great to hear!