Search code examples
batch-filegroovyjenkinshudson

Change an environment variable in between Jenkins build steps


I have a Jenkins job with a string parameter called MyPath, a groovy script build step and then a batch command build step.

If I kicked off a job so that the value of MyPath is "C:\Temp\", I want the groovy script to change the value of MyPath to be "C:\Temp\3.4\setup.exe" and then I want to use the batch file to execute MyPath.

I know I can launch a process from groovy and that I can perform my groovy logic in batch, it would just be nice if I could pass variables between build steps.

To use a parameter in groovy you have to use the library to resolve it (below). Is there a method that can set it?

def path = build.buildVariableResolver.resolve("MyPath");

Something like this would be perfect if it existed:

buildVariableChanger.change("MyPath") = "C:\3.4\setup.exe"

I have also looked into the EnvInject plugin but from what I understand it does not support what I want to do.

Thanks.


Solution

  • I don't think it is possible since field value for StringParameterValue.class is final: http://javadoc.jenkins-ci.org/hudson/model/StringParameterValue.html

    The solution may be creating other parameter in groovy script

    import hudson.model.StringParameterValue
    import hudson.model.ParametersAction
    
    def newPath = build.buildVariableResolver.resolve("MyPath") + "3.4\\setup.exe"
    build.addAction(new ParametersAction (new StringParameterValue ('path2', newPath)))
    

    and then use parameter path2 in batch command