Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-29817] Inject environment variables for a Job
- Loading branch information
Showing
12 changed files
with
124 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/org/jenkinsci/plugins/envinject/EnvInjectEnvVarsContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...sources/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapper/help-propertiesContent.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
12 changes: 6 additions & 6 deletions
12
...ources/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapper/help-propertiesFilePath.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
8 changes: 4 additions & 4 deletions
8
...sources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-keepBuildVariables.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
8 changes: 4 additions & 4 deletions
8
...org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-keepJenkinsSystemVariables.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
19 changes: 14 additions & 5 deletions
19
...esources/org/jenkinsci/plugins/envinject/EnvInjectJobProperty/help-propertiesContent.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
48 changes: 48 additions & 0 deletions
48
src/test/java/org/jenkinsci/plugins/envinject/EnvInjectEnvVarsContributorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |