moniek007
moniek007

Reputation: 11

How to make a Java console output a environment variable in Jenkins?

I am new to Jenkins and even though I found a few similar questions, none of the solutions seemed to work for me the way I need it to. It might look like a basic problem to some but for me it's a very big deal that I'm struggling with.

Basically, I built a project that executes Java Selenium code, which displays session ID in Jenkins' Console Output and that's what I need to add to environment variables to be used in the projects triggered after completion of this one.

I tried some Groovy scripts but I don't think I understand enough how to work with it and so whatever I was given, wasn't what I hoped to get.

Has anyone done something similar to provide some tips on how to achieve that?

Many thanks

Upvotes: 1

Views: 2303

Answers (1)

Gerold Broser
Gerold Broser

Reputation: 14762

There are two options (in theory, one of them doesn't work, see 2. below) depending on whether the printing is under your control or not.

  1. Printing is under your control:

    • Write the session ID to a properties file, e.g. from-build-log.properties:

      sessionId=...
      
    • Add post-build actionTrigger parameterized build on other projects

      This plugin triggers builds on other projects, with parameters that are predefined, or supplied by the finished build.

      Every parameter will be passed to the target project(s), even if the target is not parameterized, or if no property of that name is defined.

      • Add parametersParameters from properties file
        • Use properties from file: from-build-log.properties
  2. Printing is not under your control:

    • Add post-build actionPost build task:

      This feature allows you to associate shell or a batch scripts that perform some tasks on Hudson depending on the build log output. If the log text matches somewhere in the build log file, the script will execute. [...]

      Java Regex are allowed, and groups can be used as script parameters. If the text is "Last Build : #(\d+)" and the script is "script.sh", then if the log contains a line "Last Build : #4", the script "script.sh 4" will be called.

      • TasksScript:

        [...] References %1, .. %n are allowed, and will be replaced by the groups matched by the Regex. %0 is the whole match.


      Unfortunately this doesn't to work since there is an issue known since 2013: [JENKINS-17268] Post build task plugin: Passing arguments does not work as documented.

      • BuildExecute Windows batch commandCommand:

        @echo( & echo   CMD: sessionId=123456789
        
      • Post build taskTasks:

        • Log text: sessionId=(\d+)
        • Script:

          @echo( & echo   sessionId='%1'(!) of '%0'
          

      Console Output:

      ...
      [Freestyle-project] $ cmd /c call C:\Windows\TEMP\hudson4684581005071706054.bat
      
        CMD: sessionId=123456789
      
      C:\Program Files (x86)\Jenkins\workspace\Freestyle-project>exit 0 
      Performing Post build task...
      Match found for :sessionId=(\d+) : True
      Logical operation result is TRUE
      Running script  : @echo( & echo   sessionId='%1'(!) of '%0'
      
      [Freestyle-project] $ cmd /c call C:\Windows\TEMP\hudson1525182929053902824.bat
      
        sessionId=''(!) of 'C:\Windows\TEMP\hudson1525182929053902824.bat'
      
      C:\Program Files (x86)\Jenkins\workspace\Freestyle-project>exit 0 
      POST BUILD TASK : SUCCESS
      END OF POST BUILD TASK : 0
      Finished: SUCCESS
      

      %0 is not the "the whole match" but the script's name, as usual with Windows command line. %1 is empty.


      A workaround is:

    • Add build stepExecute shellCommand:

      sed -En 's/.*(sessionId=[0-9]+)/\1/p' \
        ../../jobs/${JOB_NAME}/builds/${BUILD_NUMBER}/log > from-build-log.properties
      
    • Add post-build actionTrigger parameterized build on other projects

      • Add parametersParameters from properties file
        • Use properties from file: from-build-log.properties

Upvotes: 2

Related Questions