2

I have done a lot of google for this issue and found nothing that would help to solve my issue.

I am trying to parse the Jenkins job's console output and set environment variable. I would need this variable in my parent job which would be running on different slave.

I could fetch the string value I need from the console output using

def build = Thread.currentThread().executable    
def matcher = manager.getLogMatcher(".*myEnvironemntValue: (.*)")

if(matcher != null && matcher.matches()) {
    log('found my value')
    myEnvironemntValue= matcher.group(1).substring(0)
    def pa = new ParametersAction([
          new StringParameterValue("MY_ENV_VALUE", myEnvironemntValue)
    ])
    build.addAction(pa)
    manager.addInfoBadge(myEnvironemntValue)
}

Update

I have a buildflow job (parent job) from which I will call other child jobs using DSL script something like

def b1 = build("child_job1", NODE: node)
def b2 = build("child_job2", NODE: node)

Child job child_job1 and child_job2 are multiphase jobs, they invoke other jobs in turn in various phases, say phase1_job, phase2_job and phase3_job

Once the job phase3_job is done executing, I would want to get a specific value from its console.


Could someone help in resolving this issue? I am fine to use other plugins if they help me getting the console value and setting it as environment variable

abdulH
  • 375
  • 1
  • 6
  • 19

1 Answers1

1

One thing to note is that Groovy Post build runs on the master, so setting the environment variables there might not be what you are wanting.

That being said, there are probably numerous ways you could pass up that information to the parent. Is the parent job waiting for the child to complete? If so, you can save the environment variable as an artifact and pull it down using the Jenkins model, http, or even the file system (if the parent is running on the master or you access it via the parent Groovy Postbuild).

Would this solve your problem? If not, could you elaborate on how things are setup and perhaps give us an example?

Daniel Omoto
  • 2,431
  • 17
  • 15
  • Thanks for the response. Yes the parent job waits for the child job execution to get over. I have added an update to my questions – abdulH Jul 21 '16 at 06:03
  • Thanks for the clarification! How about using the [Copy Artifact Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin) to copy the last successful build's artifact at the very end of the dsl? For example, something like what [this](https://www.cloudbees.com/blog/copying-artifacts-between-builds-jenkins-workflow) guy is doing? – Daniel Omoto Jul 22 '16 at 02:04
  • I can not copy the artifacts because from groovy script if I create a file, it creates on server not on my slave node. Moreover I will be calling multiple child jobs from my parent jobs and all of them would be having the same groovy script – abdulH Jul 22 '16 at 07:32
  • Sorry, I might be getting confused again... Are you trying to get artifacts from one completed job into the workspace of another job? Then the Copy Artifacts plugin should be able to handle this. It runs as a build step within a job.. It will copy artifacts from the last successful, failed, unstable, etc of one job to the workspace on the slave of another. So, if you add that build step to child_job2 after phase3_job build step, you can get the phase3_job artifacts and pull them into child_job2. – Daniel Omoto Jul 22 '16 at 14:28
  • Regarding getting the artifact saved from phase3_job, you are correct, doing it in postbuild will put it on the server and not the slave. You could save it out in a build step? For example, in a script step like "echo ENV_WORLD > my_file_on_the_slave.txt". The jenkins model also has the ability to save files directly on the slave from the parent using something like build.getWorkspace().createTextTempFile("temp", "", "hello world") – Daniel Omoto Jul 22 '16 at 14:32
  • If the above doesn't answer your question, I might need a more indepth explanation of what you are doing to give you a better answer. Sorry! – Daniel Omoto Jul 22 '16 at 14:35