forrestaustin
forrestaustin

Reputation: 95

Jenkins Inject Environment variable works with batch command but not powershell

I'm dealing with a very weird anomaly in Jenkins that makes absolutely no sense to me. Essentially Jenkins is behaving differently for a powershell command than for a batch command.

My goal is to pass an environment variable (or parameter) from one Jenkins Job to another. However this variable to be passed is generated during the runtime of the first job.

I made a fake project to test passing variables and I was able to do so by adding a build step to echo out the variable into an env.props file on the node and then used the parameterized trigger plugin to call the next job. I was able to get this to work great in this test scenario but when I tried to implement the same steps in the actual build job (which relies on powershell scripts) it did not work.

After, a lot of trial and error I have found that when I use a windows batch command to echo the variable into a props file and then inject the variable into the job - it works perfect. But when I do the exact same thing with a powershell command it does not inject the variable back in to the job even though I use the exact same line of code. It still writes the variable to the file but Jenkins will not "reinject" this variable back into the job's env variable even though I am using the exact same step to do so.

The command is essentially this:

echo Testvar=Somevalue > C:\Jenkins\env.props

Both sucesffuly write the string to the props file, but when done with a powershell command, Jenkins will not absorb the txt from the run. Almost as if powershell is encoding it in a way that Jenkins cannot read but looks the exact same to me.

Any ideas?

Upvotes: 2

Views: 388

Answers (1)

forrestaustin
forrestaustin

Reputation: 95

Turns out, it was the encoding!

echo "string" > file.txt 

does not produce the same result in batch as powershell.

Switching to

echo "string" | out-file -encoding ASCII file.txt 

did the trick.

Upvotes: 2

Related Questions