Skip to content
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

Merged
merged 1 commit into from
Aug 14, 2015
Merged

[JENKINS-29817] Inject environment variables for a Job #56

merged 1 commit into from
Aug 14, 2015

Conversation

recena
Copy link
Contributor

@recena recena commented Aug 5, 2015

https://issues.jenkins-ci.org/browse/JENKINS-29817

  • Implement EnvInjectEnvVarsContributor
  • Add tests
  • Update the documentation (help information) for the Properties Content field

@reviewbybees

@recena recena changed the title [JENKINS-29817] Inject environment variables for a Job [WiP] [JENKINS-29817] Inject environment variables for a Job Aug 5, 2015
EnvVars environment = project.getEnvironment(jenkins.getInstance(), listener);

assertNotNull(environment.get("REPO"));
jenkins.assertBuildStatusSuccess(project.scheduleBuild2(0).get());
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jglick Done

@jenkinsadmin
Copy link
Member

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()) {
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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);
    }
}

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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)?

Copy link
Member

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 ;)

@oleg-nenashev
Copy link
Member

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).

@recena
Copy link
Contributor Author

recena commented Aug 6, 2015

@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 Job.getEnvironment().

@oleg-nenashev
Copy link
Member

@recena
I'll try to respond on this evening.

@oleg-nenashev
Copy link
Member

@recena
After digging into the code...
EnvInjectInfo: getPropertiesContentMap() works with property contents only (text area field). See https://github.com/jenkinsci/envinject-plugin/blob/master/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectInfo.java#L42-L65. The current implementation ignores environment variables from property files, Groovy scripts and from Master node.

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. buildEnvironmentFor for Jobs is being called before the resolution for runs. According to the brief code analysis, seems that the use-case with build parameter overrides will be broken by the change. The behavior is configurable via the ''Override build parameters" option. According to the manual testing, AbstractBuild:getEnvironment() returns wrong values after the change. The test has been added in #58

@recena
Copy link
Contributor Author

recena commented Aug 10, 2015

@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?

@recena
Copy link
Contributor Author

recena commented Aug 10, 2015

@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?

@oleg-nenashev
Copy link
Member

I don't understand why you say that this new extension breaks anything

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.

I'm going to implement the different way to provide variables (property files, groovy scripts, etc..).

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.

@oleg-nenashev
Copy link
Member

The rest of tests are working fine. Maybe we should review these test, Isn't that so?

Unfortunately the plugin does not contain tests for EnvInjectJobProperty at all :(

@recena recena changed the title [WiP] [JENKINS-29817] Inject environment variables for a Job [JENKINS-29817] Inject environment variables for a Job Aug 12, 2015
<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>
Copy link
Contributor Author

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?

Copy link
Member

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.

@ghost
Copy link

ghost commented Aug 12, 2015

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>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleg-nenashev Better?

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Member

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

@recena
Copy link
Contributor Author

recena commented Aug 12, 2015

@oleg-nenashev What do you thing about last commit?

@@ -35,6 +21,20 @@
value="${instance.info.groovyScriptContent}"/>
</f:entry>

<f:entry field="propertiesFilePath"
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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:

  1. It is completely unrelated to the PR
  2. The change impacts the usability and UX IMO, because it changes the original layout

@oleg-nenashev
Copy link
Member

🐛 due to the unrelated change in src/main/resources/org/jenkinsci/plugins/envinject/EnvInjectBuildWrapper/config.jelly, which is not related to this PR. It should be decoupled to a separate pull-request

@amuniz
Copy link
Member

amuniz commented Aug 13, 2015

@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.

@oleg-nenashev
Copy link
Member

Please also address a comment regarding the Japanese help

@oleg-nenashev
Copy link
Member

🐝
Could you please squash commits? After that we will be able to merge it according to the policy. I'm planning to release 0.92-beta1 on the weekend (or today if everything goes right)

@recena
Copy link
Contributor Author

recena commented Aug 13, 2015

@oleg-nenashev done

@recena
Copy link
Contributor Author

recena commented Aug 13, 2015

@oleg-nenashev By the way, Is there an official maintainer? /cc @gboissinot

recena added a commit that referenced this pull request Aug 14, 2015
[JENKINS-29817] Inject environment variables for a Job
@recena recena merged commit 95c1f0e into jenkinsci:master Aug 14, 2015

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

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

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants