-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-29817] Inject environment variables for a Job #56
Conversation
EnvVars environment = project.getEnvironment(jenkins.getInstance(), listener); | ||
|
||
assertNotNull(environment.get("REPO")); | ||
jenkins.assertBuildStatusSuccess(project.scheduleBuild2(0).get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running the build does not really accomplish anything. You could delete this line, or make the test demonstrate that the variable is also accessible to build steps. Which reminds me that it is likely that other code in this plugin is interpreting EnvInjectJobProperty
on a Run
(or AbstractBuild
) that could probably now be deleted, since Run
environments inherit from Job
environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jglick Indeed. I'm going to review the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jglick Done
Thank you for a pull request! Please check this document for how the Jenkins project handles pull requests |
|
||
@Override | ||
public void buildEnvironmentFor(Job job, EnvVars env, TaskListener listener) throws IOException, InterruptedException { | ||
for (Object property : job.getProperties().values()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to get properties by class without such cycles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev I'll take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev I've done some changes related to your comment. Could you review it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev I've reverted the changes because the tests were failing. I was trying this:
@Override
public void buildEnvironmentFor(Job job, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
EnvInjectJobProperty jobProperty = (EnvInjectJobProperty) job.getProperty(EnvInjectJobProperty.class)
EnvInjectJobPropertyInfo jobPropertyInfo = jobProperty.getInfo();
// Process "Properties Content"
Map<String, String> result = jobPropertyInfo.getPropertiesContentMap(new HashMap<String, String>());
if (result != null) {
env.putAll(result);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null check is missing. If a job has no EnvInjectJobProperty
, your test will fail with NPE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev AFAIK, if you are using the plugin (checkbox is clicked), there is always a EnvInjectJobProperty
. And I've verified that job.getProperties()
can never be null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
job.getProperty() may return null if you run the method agains the loaded job, which has been saved before the EnvInject installation.
job.getProperties()
is never null, but IMO you're using a wrong pattern to lookup for properties. It may become performance-innefective in future Jenkins versions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev I don't know how job.getProperty() can be null
if this method is invoked only if job.getProperties().values()
returns values.
Do you prefer to use job.getProperty(EnvInjectJobProperty.class)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. And it can return the null value ;)
I general I agree that such contributor mat be useful. On the other hand I cannot elaborate an impact of the change, because it may break the variable resolution order on existing installations. It may also cause a total breakage of existing jobs if they use Groovy scripts to inject variables and use build variables. More tests and investigations are required before this PR becomes mergeable. I'm putting a 👎 on it now (as OSS contributor). |
@oleg-nenashev What tests do you propose to verify that improvement (very needed) does not breaks anything? This PR has been tested with Subversion Plugin (master) in order to verify this kind of use cases JENKINS-29340 and works perfectly. Basically, we need the environment variables defined by EnvInject availables in |
@recena |
@recena Such implementation may address a narrow case from JENKINS-29817, but it is incomplete. I would not vote for merging the fix in the current state, because it may confuse the plugin users => serious 🪲 . You should implement a new method, which would generate variables without a build context. I'm also aware about the variables resolution order. |
@oleg-nenashev I'm going to implement the different way to provide variables (property files, groovy scripts, etc..). I don't understand why you say that this new extension breaks anything. The rest of tests are working fine. Maybe we should review these test, Isn't that so? |
@oleg-nenashev There are a lot of users using Subversion Plugin and EnvInject Plugin. We need the environment variables (injected by this plugin) availables, p.e, in polling process. What would it your proposal? |
I just see a wrong output in the API method. This result needs more investigation. buildEnvironmentForJob() is being called before the Environment contributing actions, so probably the issue in overrides (or I'm doing something wrong in the debugger). I'll write a unit test and submit it.
Please be aware about Groovy ones. I suspect that many Users rely on build runtime variables there. The plugin's code correctly handles null build references, but we should ensure that the newly introduced method is not failing horribly in the case of the misusage. |
Unfortunately the plugin does not contain tests for |
<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 context Job.</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a context Job
? I don't know such term. In any case it makes sense to add several sentences, which would allow users to understand what's going on here.
This pull request originates from a CloudBees employee. At CloudBees, we require that all pull requests be reviewed by other CloudBees employees before we seek to have the change accepted. If you want to learn more about our process please see this explanation. |
<ul> | ||
<li>Validate Subversion Repository with a URL like this: <code>http://server/svn/$REPO</code></li> | ||
<li>Subversion SCM Polling</li> | ||
</ul> | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev Better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposals:
... 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. http://server/svn/$REPO), custom workspace management, etc.
Limitations: The field may contain variables, which are not resolvable outside the Build context (e.g. BUILD_NUMBER, WORKSPACE, etc.), the plugin [injects empty value/ignores the field/or something else]). This behavior should be taken into account by job editors.
Please also add a proper test-case for the limitation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev Thanks so much for your help. I'm going to investigate the limitations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev I don't understand what you mean with limitations. Could you show an example the variable defined in this field not resolvable? Do you mean something like this VAR1=$WORKSPACE
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@recena
Yes, WORKSPACE var is a valid example for this limitation
@oleg-nenashev What do you thing about last commit? |
@@ -35,6 +21,20 @@ | |||
value="${instance.info.groovyScriptContent}"/> | |||
</f:entry> | |||
|
|||
<f:entry field="propertiesFilePath" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you move this section? How is it related to this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev Indeed this change is not directly related to PR but I considered it would be helpful to show the form fields in the same order in both sections. Don't make sense to use a different order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: "Properties content" is the most widely used section, so I would use another direction. BTW, the interface should be reworked to show only the required section (extension point?). Such rework is not a part of the PR, so I'd prefer to submit it as another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleg-nenashev We can discuss about it out of the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a summary of the discussion, I still think it should be moved to another PR:
- It is completely unrelated to the PR
- The change impacts the usability and UX IMO, because it changes the original layout
🐛 due to the unrelated change in |
@oleg-nenashev Is that really a bug? Is preventing to use the plugin somehow? Since it's not really a functional change and does not affect in any way to the behavior being modified in this PR, it's ok for me. Moreover it's a minor change that you can quick see that is only a code block moved some lines below. |
Please also address a comment regarding the Japanese help |
🐝 |
@oleg-nenashev done |
@oleg-nenashev By the way, Is there an official maintainer? /cc @gboissinot |
[JENKINS-29817] Inject environment variables for a Job
|
||
TaskListener listener = jenkins.createTaskListener(); | ||
EnvVars environment = project.getEnvironment(jenkins.getInstance(), listener); | ||
assertEquals("${WORKSPACE}", environment.get("VAR1")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is failing on my local laptop. The result is null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed by clean build, please ignore
https://issues.jenkins-ci.org/browse/JENKINS-29817
EnvInjectEnvVarsContributor
@reviewbybees