att
att

Reputation: 51

EnvInject Error using Jenkins evaluated Groovy script

We have a evaluated Groovy script in Jenkins below:- ;

But the build is failing with error [EnvInject] - [ERROR] - [EnvInject] - [ERROR] - Problems occurs on injecting env vars as a build wrap: null 17:04:06 Finished: FAILURE. Also how can I call the variable from Jenkins shell script to get last successful build date. -Thanks

def env = System.getenv()
def item = Jenkins.instance.getItem("")
 def  f=item.getLastFailedBuild()

 println f.getTime()

 def  ff=env['item.getLastSuccessfulBuild()]
  println ff.getTime().format("YYYY-MMM-dd HH:MM:SS")
  println ff.getTime().format("dd-MM-yyyy")

  def pa = new ParametersAction([new          StringParameterValue('PARAMETER_NAME', ff)]);

Thread.currentThread().executable.addAction(pa) println 'Script finished! \nenv variable

Upvotes: 2

Views: 3676

Answers (2)

att
att

Reputation: 51

Thanks Nick!!

I added the below "execute system groovyscript" as part of Jenkins job and it worked: import jenkins.model.Jenkins

`. def item = Jenkins.instance.getItem("Job")

def  ff=item.getLastSuccessfulBuild()
println ff.getTime().format("yyyy-MM-dd")
def temp = ff.getTime().format("yyyy-MM-dd")
import hudson.model.*
def build = Thread.currentThread().executable
def pa = new ParametersAction([
new StringParameterValue("LAST_BUILD_DATE",temp)
])
 build.addAction(pa)`

Upvotes: 1

Nick Holt
Nick Holt

Reputation: 168

The easy answer is that on line 7 you have no closing quote here:

def ff=env['item.getLastSuccessfulBuild()]

However, that is not the last of your issues:

  • I don't think you want to use ff = env['item.getLastSuccessfulBuild()]'] but rather just a simple ff = item.getLastSuccessfulBuild()

  • You need to include the following import lines to be able to use the associated classes: import jenkins.model.Jenkins import hudson.model.ParametersAction import hudson.model.StringParameterValue

  • The line item = Jenkins.instance.getItem("Fastlane_Test") doesn't work in my environment, even replacing "Fastlane_Test" with a job that exists.
    I've replaced it with item = Jenkins.instance.getItemByFullName("Fastlane_Test").

  • Also, for safety, you should test to ensure item isn't null

  • Finally, have you missed the Thread.currentThread().executable.addAction(pa) line out for a reason? You need to use it to add the new parameter to the running environment.

The following code should hopefully be a reasonable starting point, however please note that I've removed the line def env = System.getenv() since env isn't used anywhere else in the code later:

import jenkins.model.Jenkins
import hudson.model.ParametersAction
import hudson.model.StringParameterValue

def item = Jenkins.instance.getItemByFullName("Fastlane_Test")

if (item) {
    def  f=item.getLastFailedBuild()

    println f.getTime()

    def  ff=item.getLastSuccessfulBuild()
    println ff.getTime().format("YYYY-MMM-dd HH:MM:SS")
    println ff.getTime().format("dd-MM-yyyy")

    def pa = new ParametersAction([new StringParameterValue("LAST_GOOD", ff.getTime().toString())])
    Thread.currentThread().executable.addAction(pa)
}

Hope you find this of assistance, although I see it's been a while since you posted the question.

Kind Regards

Upvotes: 1

Related Questions