Skip to content

Allowing empty valued global variables in config file#2657

Merged
timja merged 5 commits intojenkinsci:masterfrom
PereBueno:empty-vars-exporting
Apr 3, 2025
Merged

Allowing empty valued global variables in config file#2657
timja merged 5 commits intojenkinsci:masterfrom
PereBueno:empty-vars-exporting

Conversation

@PereBueno
Copy link
Copy Markdown
Contributor

@PereBueno PereBueno commented Apr 1, 2025

Fixes: #2658

Exporting empty variables as an empty string seems not possible. There's a explicit check to avoid it and removing it would result in lots of empty configurations being exported, which is not desired.

Also, export works recursively, so any line being exported is not aware of the existence of any sibling, meaning in

- key: "FOO"
  value: ""

key and value lines are not aware of each other.

Having these, this PR will allow reading a config file that contains variables without a value (as they are exported)

jenkins:
  globalNodeProperties:
  - envVars:
      env:
      - key: VARIABLE1
        value: foo
      - key: EMPTY_VAR

will be accepted, Jenkins will start normally and variable will be visible
image

Modified integration tests accordingly.

Your checklist for this pull request

🚨 Please review the guidelines for contributing to this repository.

  • Make sure you are requesting to pull a topic/feature/bugfix branch (right side) and not your master branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or in Jenkins JIRA
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Did you provide a test-case? That demonstrates feature works or fixes the issue.

@PereBueno PereBueno changed the title Using configurator for global props Allowing empty valued global variables in config file Apr 2, 2025
@PereBueno PereBueno marked this pull request as ready for review April 2, 2025 14:12
@PereBueno PereBueno requested a review from a team as a code owner April 2, 2025 14:12
@Override
public Set<Attribute<EnvironmentVariablesNodeProperty, ?>> describe() {
Set<Attribute<EnvironmentVariablesNodeProperty, ?>> attrs = super.describe();
attrs.add(new MultivaluedAttribute<EnvironmentVariablesNodeProperty, EnvironmentVariablesNodeProperty.Entry>(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, if not using it env is not exported

- \"envVars\"
- toolLocation:    
  - locations:\n    
    - home: "/home/user/bin/git"
    [....]

If I'm not wrong it's because we're extending BaseConfigurator which will take EnvVars as an scalar, while before this configurator it was read as a HeteroDescribableConfigurator.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would extending HeteroDescribableConfigurator work instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't test that, seems more complex than this approach.
I can do a test and check if we would be simplifying things

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

worth a quick go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my fault, not Hetero, but DataBoundConfigurator.
Just changed it to use it, slightly simpler.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks, much simpler and more like what I expected, thanks for sorting.

Copy link
Copy Markdown
Member

@timja timja left a comment

Choose a reason for hiding this comment

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

Thanks!

@timja timja merged commit 506f96a into jenkinsci:master Apr 3, 2025
17 checks passed
timja added a commit that referenced this pull request Apr 7, 2025
@timja
Copy link
Copy Markdown
Member

timja commented Apr 7, 2025

Reverted in #2663

See #2662

timja added a commit that referenced this pull request Apr 7, 2025
import java.util.List;

@Extension
public class GlobalNodePropertiesConfigurator extends DataBoundConfigurator<EnvironmentVariablesNodeProperty> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO this is the wrong fix and it is

if (value == null || value.length() == 0) {
return null;
}
which should be corrected. If a field is set to the empty string (rather than null), we should export

thatField: ''

regardless of where it is. If some config class neglects to call Util.fixEmpty (or fixEmptyAndTrim etc.) when processing a parameter to a @DataBoundSetter which cannot logically be the empty string, then fix that setter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

that might have an huge impact on the export. Any single property with an empty String as value will be exported when nowadays is not

If some config class neglects to call Util.fixEmpty (or fixEmptyAndTrim etc.) when processing a parameter to a @DataBoundSetter which cannot logically be the empty string, then fix that setter.

which might mean to fix how many plugins? Although I agree with that being the 100% correct solution, that might be little realistic as users using the export nowadays will see unexpected changes in the export without a (from the user perspective) good reason

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

which might mean to fix how many plugins?

Not sure, but they need to be fixed anyway even without JCasC.

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.

Empty environment variables are exported without value

4 participants