1

I have this Execute Windows Batch Commandin Jenkins that I want to read it's returned value

type foo.txt | find /C "%param%" > temp.txt set /p result=<temp.txt del temp.txt 

And I want to return result value to Jenkins parameter. How can I do that?

EDIT

I have a string Parameter in Jenkins called RESULT_PARAM

after running the code above or any other code, it does not matter, I want to read the result to put it into RESULT_PARAM

2
  • What is your Jenkins parameter? Please explain in more details. You can see that set /P RESULT= < temp.txt will give you echo %RESULT%. Commented Aug 17, 2015 at 8:59
  • please see my edit, acutally it does not matter what the code does, I simply want to save it's output to Jenkins global param Commented Aug 17, 2015 at 9:35

1 Answer 1

1

When you spawn a process, it inherits a copy of environmental variables from the parent. When you chance an environment variable, it only affects that spawned process, it does not propagate back up to the parent (or other children). Even if you explicitly set the variable as global, it will not take effect until the process is restarted.

For the benefit of "clean environment", Jenkins spawns a child process from itself, for every build. This way, when the build is finished, the child process is discarded, and your OS environment is not polluted.

 - ENVIRONMENT (global variables) | ------ Jenkins process (copy of ENVIRONMENT variables) | | | ----- Build 1 (copy of Jenkins process variables) | | | | | ------ %RESULT% variable changed | | | ----- Build 2 (copy of Jenkins process variables) | ------ Other process (copy of ENVIRONMENT variables) 

If your Build 1 changes any environment variable, it does not affect Build 2 or other OS processes that are running.

Even if you use SETX command to force the variable to be propagated back to global ENVIRONMENT, your Jenkins process will not pick it up, unless restarted, and neither will Jenkins child build processes, since they get a copy of Jenkins process.

Workaround

Use EnvInject plugin.

At the end of your job, write the variable you want into a properties file.
echo result=%result% > myvars.props
This file should list, line by line, key-value pairs of variables and their values.

In the next build, you can use EnvInject plugin to read the properties file, and inject these variables into the spawned process. From that point, you can use %result% as any other global environment variable.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.