Skip to content
Permalink
Browse files

[FIXED JENKINS-47202] Switch to HashMaps for env to fix serialization

TreeMap#Entry isn't serializable, which can bite you in the weirdest
places. Like here - if `environment` variable values end up triggering
a program save, tada, you get a `NotSerializableException`. Which is
bad. I can't reproduce this consistently - maybe 3 times out of 4,
it'll fail. But this fix has yet to reproduce the error after 20+
attempts, so I think it's good.
  • Loading branch information...
abayer committed Oct 4, 2017
1 parent a90828b commit 03f4dcf0fc9dc9e72e03dd7ee0be82f8a9ee5357
@@ -72,7 +72,7 @@ public class Environment implements Serializable {
private static final long serialVersionUID = 1L

private CpsScript script
private Map<String,Closure> closureMap = new TreeMap<>()
private Map<String,Closure> closureMap = new HashMap<>()
private EnvironmentResolver fallback

@Whitelisted
@@ -307,7 +307,7 @@ public class ModelInterpreter implements Serializable {
* @return The return of the resulting executed closure
*/
def withCredentialsBlock(@CheckForNull Environment environment, Closure body) {
Map<String,CredentialWrapper> creds = new TreeMap<>()
Map<String,CredentialWrapper> creds = new HashMap<>()

if (environment != null) {
try {
@@ -291,4 +291,13 @@ public void defaultEnvValue() throws Exception {
.logContains("FOO is OTHER", "BAZ is BAR")
.go();
}

@Issue("JENKINS-42702")
@Test
public void readFileInEnv() throws Exception {
expect("readFileInEnv")
.otherResource("readFileInEnv-data.txt", "Version")
.logContains("Version is BANANA")
.go();
}
}
@@ -0,0 +1 @@
BANANA
@@ -0,0 +1,37 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
agent any
environment {
MY_VERSION = readFile 'Version'
}
stages {
stage ('Print'){
steps {
echo "Version is ${MY_VERSION}"
}
}
}
}

0 comments on commit 03f4dcf

Please sign in to comment.
You can’t perform that action at this time.