Skip to content
Permalink
Browse files

Merge pull request #56 from recena/JENKINS-29817

[JENKINS-29817] Inject environment variables for a Job
  • Loading branch information
recena committed Aug 14, 2015
2 parents abca0ed + b34ae59 commit 95c1f0eac808580d21c9e71e9fe9b55ab085cb85
@@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.532</version>
<version>1.532.3</version>
</parent>

<artifactId>envinject</artifactId>
@@ -10,12 +10,10 @@
import java.util.HashMap;
import java.util.Map;

/**
* @author Gregory Boissinot
*/

/**
* Overriding job parameters with environment variables populated by EnvInject plugin
*
* @author Gregory Boissinot
*/
@Extension
public class EnvInjectBuildVariableContributor extends BuildVariableContributor {
@@ -0,0 +1,26 @@
package org.jenkinsci.plugins.envinject;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.*;

import java.io.IOException;
import java.util.Map;

@Extension
public class EnvInjectEnvVarsContributor extends EnvironmentContributor {

@Override
public void buildEnvironmentFor(Job job, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
EnvInjectJobProperty jobProperty = (EnvInjectJobProperty) job.getProperty(EnvInjectJobProperty.class);
if (jobProperty != null) {
EnvInjectJobPropertyInfo jobPropertyInfo = jobProperty.getInfo();

// Processes "Properties Content"
Map<String, String> result = jobPropertyInfo.getPropertiesContentMap(env);
if (result != null) {
env.putAll(result);
}
}
}
}
@@ -8,7 +8,7 @@
</f:entry>

<f:entry title="${%Properties Content}"
help="/descriptor/org.jenkinsci.plugins.envinject.EnvInjectJobProperty/help/propertiesContent">
help="/descriptor/org.jenkinsci.plugins.envinject.EnvInjectBuildWrapper/help/propertiesContent">
<f:textarea
name="envInjectInfoWrapper.propertiesContent"
value="${instance.info.propertiesContent}"/>
@@ -0,0 +1,6 @@
<div>
<p>Give a set of key/value (one variable per line): <code>KEY=VALUE</code>.</p>

<p>All the properties name will be accessible as environment variables by their names. You can use or override the
properties specified in the above properties file.</p>
</div>
@@ -1,8 +1,8 @@
<div>
<p>
Gives a file path of a properties file. The file format must be the standard <a
href="http://en.wikipedia.org/wiki/.properties">Java property file format</a>.
The file path can be absolute or relative to the workspace (the process is executed after a SCM checkout).<br/>
All the properties name will be accessible as environment variables by their names.
</p>
<p>Gives a file path of a properties file. The file format must be the standard <a
href="http://en.wikipedia.org/wiki/.properties">Java property file format</a>.</p>

<p>The file path can be absolute or relative to the workspace (the process is executed <strong>after</strong> a SCM
checkout). All
the properties name will be accessible as environment variables by their names.</p>
</div>
@@ -1,6 +1,6 @@
<div>
<p>
Inject Jenkins build variables such as EXECUTOR_NUMBER, BUILD_ID, BUILD_TAG, JOB_NAME and so on.<br/>
Inject also environment contributors and build variable contributors provided by other plugins.
</p>
<p>Inject Jenkins build variables such as <code>EXECUTOR_NUMBER</code>, <code>BUILD_ID</code>,
<code>BUILD_TAG</code>, <code>JOB_NAME</code> and so on.</p>

<p>Inject also environment contributors and build variable contributors provided by other plugins.</p>
</div>
@@ -1,6 +1,6 @@
<div>
<p>
Inject Jenkins system variables such as JENKINS_HOME, JENKINS_URL, NODE_NAME and so on. <br/>
Inject also environment variables defined as global properties and as node properties.
</p>
<p>Inject Jenkins system variables such as <code>JENKINS_HOME</code>, <code>JENKINS_URL</code>,
<code>NODE_NAME</code> and so on.</p>

<p>Inject also environment variables defined as global properties and as node properties.</p>
</div>
@@ -1,7 +1,16 @@
<div>
<p>
Give a set of key/value (KEY=VALUE).<br/>
All the properties name will be accessible as environment variables by their names.<br/>
You can use or override the properties specified in the above properties file.<br/>
</p>
<p>Give a set of key/value (one variable per line): <code>KEY=VALUE</code>.</p>

<p>All the properties name will be accessible as environment variables by their names. You can use or override the
properties specified in the above properties file.</p>

<p>These environment variables (<strong>only these</strong>) will be available in the Job environment independently
from the build. There are following use-cases: SCM polling, validation of fields depending on environment
variables (e.g. <code>http://server/svn/$REPO</code>), custom workspace management, etc.</p>

<p><strong>Limitations:</strong> The field may contain variables, which are not resolvable outside the Build context
(e.g. <code>BUILD_NUMBER</code>, <code>WORKSPACE</code>, etc.), the plugin simply won't interpret these variables.
This behavior should be taken into account by job editors. For example, with <code>VAR1=${WORKSPACE}</code> it
will receive the string <code>${WORKSPACE}</code>.</p>

</div>
@@ -1,16 +1,14 @@
<div>
<p>
If this option is checked, Jenkins makes it possible to set an environment
for the build job (or for each run on a matrix project) by defining environment variables and execute a script
(a setup script).<br/>
All these actions will be executed before a SCM checkout.<br/>
By default, after the set up, only injected variables (with Jenkins variables) will be available in the build
scripts, in the Jenkins post-actions, and so on. <br/>
<p>If this option is checked, Jenkins makes it possible to set an environment for the build job (or for each run on
a matrix project) by defining environment variables and execute a script (a setup script).</p>

Default inherited environment variables (inherited variables by
the Jenkins process which often contains machine-specific information,
like PATH, TIMEZONE, etc.) can be unset if you enable
the 'envInject' property and check 'Unset system variables'
in the node configuration page.
</p>
<p>All these actions will be executed before a SCM checkout.</p>

<p>By default, after the set up, only injected variables (with Jenkins variables) will be available in the build
scripts, in the Jenkins post-actions, and so on.</p>

<p>Default inherited environment variables (inherited variables by the Jenkins process which often contains
machine-specific information, like <code>PATH</code>, <code>TIMEZONE</code>, etc.) can be unset if you enable
the <strong>envInject</strong> property and
check <strong>Unset system variables</strong> in the node configuration page.</p>
</div>
@@ -0,0 +1,48 @@
package org.jenkinsci.plugins.envinject;

import hudson.EnvVars;
import hudson.model.FreeStyleProject;
import hudson.model.TaskListener;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class EnvInjectEnvVarsContributorTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Test
public void envVarsJob() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();

EnvInjectJobPropertyInfo jobPropertyInfo = new EnvInjectJobPropertyInfo(null, "REPO=trivial-maven", null, null, null, false);
EnvInjectJobProperty envInjectJobProperty = new EnvInjectJobProperty();
envInjectJobProperty.setOn(true);
envInjectJobProperty.setInfo(jobPropertyInfo);
project.addProperty(envInjectJobProperty);

TaskListener listener = jenkins.createTaskListener();
EnvVars environment = project.getEnvironment(jenkins.getInstance(), listener);
assertNotNull(environment.get("REPO"));
}

@Test
public void notAvailableEnvVarsJob() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject("notAvailableEnvVarsJob");

EnvInjectJobPropertyInfo jobPropertyInfo = new EnvInjectJobPropertyInfo(null, "VAR1=${WORKSPACE}\nVAR2=${JOB_NAME}", null, null, null, false);
EnvInjectJobProperty envInjectJobProperty = new EnvInjectJobProperty();
envInjectJobProperty.setOn(true);
envInjectJobProperty.setInfo(jobPropertyInfo);
project.addProperty(envInjectJobProperty);

TaskListener listener = jenkins.createTaskListener();
EnvVars environment = project.getEnvironment(jenkins.getInstance(), listener);
assertEquals("${WORKSPACE}", environment.get("VAR1"));
assertEquals("notAvailableEnvVarsJob", environment.get("VAR2"));
}
}

0 comments on commit 95c1f0e

Please sign in to comment.
You can’t perform that action at this time.