Permalink
Please
sign in to comment.
Browse files
Merge pull request #56 from recena/JENKINS-29817
[JENKINS-29817] Inject environment variables for a Job
- Loading branch information
Showing
with
123 additions
and 38 deletions.
- +1 −1 pom.xml
- +2 −4 src/main/java/org/jenkinsci/plugins/envinject/EnvInjectBuildVariableContributor.java
- +26 −0 src/main/java/org/jenkinsci/plugins/envinject/EnvInjectEnvVarsContributor.java
- +1 −1 src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapper/config.jelly
- +6 −0 src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapper/help-propertiesContent.html
- 0 ...i/plugins/envinject/{EnvInjectJobProperty → EnvInjectBuildWrapper}/help-propertiesContent_ja.html
- +6 −6 ...main/resources/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapper/help-propertiesFilePath.html
- +4 −4 src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-keepBuildVariables.html
- +4 −4 ...sources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-keepJenkinsSystemVariables.html
- +14 −5 src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-propertiesContent.html
- +11 −13 src/main/webapp/help.html
- +48 −0 src/test/java/org/jenkinsci/plugins/envinject/EnvInjectEnvVarsContributorTest.java
@@ -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); | ||
} | ||
} | ||
} | ||
} |
@@ -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> |
File renamed without changes.
@@ -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