Inject env vars to Jenkins programmatically

38 views
Skip to first unread message

ni...@liveperson.com

unread,
Jun 13, 2018, 1:05:50 PM6/13/18
to Jenkins Developers
Hi,

I am trying to inject environment variables to Jenkins build from my internal Maven plugin in Java. 

What did I do? 

I added the Jenkins code Maven dependency to my plugin an implemented the relevant interface: 


public class Test implements EnvironmentContributingAction{

       @Override

    public void buildEnvironment(Run<?, ?> run, EnvVars env) {

                EnvironmentContributingAction.super.buildEnvironment(run, env);

    }  
 
}


Now - I'm trying to call this class from a different class: 

EnvVars envVars = new EnvVars();

envVars.put("myvar", "myvalue");

Test test = new Test();

test.buildEnvironment(???? , envVars);



What is the "Run" class? What should I add into this method so it will run from the build object in Jenkins? 

Any Github example or snippet will be much appreciated.

Thanks,
Nir

This message may contain confidential and/or privileged information. 
If you are not the addressee or authorized to receive this on behalf of the addressee you must not use, copy, disclose or take action based on this message or any information herein. 
If you have received this message in error, please advise the sender immediately by reply email and delete this message. Thank you.

Dan Bomsta

unread,
Dec 10, 2018, 5:33:46 PM12/10/18
to Jenkins Developers
Hi,
I was struggling through the same type of issue and just got mine to work.  Hope this helps

My plugin 'extends' Builder.  For this to work I had to create another class 'InjectVariable'

// This is called from the Builder class I wrote
build
.addAction( new InjectVariable( "VERSION_TAG", "V1.2.3.4" ) );

public class InjectVariable implements EnvironmentContributingAction {
 
private String key;
 
private String value;

 
public InjectVariable( String key, String value )
 
{
 
this.key = key;
 
this.value = value;
 
}

 
@Override
 
public String getIconFileName() {
 
return null;
 
}

 
@Override
 
public String getDisplayName() {
 
return null;
 
}

 
@Override
 
public String getUrlName() {
 
return null;
 
}

 
@Override
 
public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
 
if( env != null && key != null && value != null )Enter code here.

 
{
 env
.put(this.key, this.value);
 
}
 
}
 
}

Enter code here...
Reply all
Reply to author
Forward
0 new messages