Skip to content
Permalink
Browse files
[JENKINS-41759] Read properties file into environment
  • Loading branch information
rsandell committed Feb 9, 2017
1 parent 0691d01 commit 49ebf576dd6d156dfa8b540bf7dd724bf64e5ca3
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
@@ -0,0 +1,54 @@
/*
* 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.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.environment.impl;

import hudson.Extension;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.environment.DeclarativeEnvironmentContributor;
import org.jenkinsci.plugins.pipeline.modeldefinition.environment.DeclarativeEnvironmentContributorDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Read environment from a properties file in the scm.
*/
public class TrustedProperties extends DeclarativeEnvironmentContributor<TrustedProperties> {

private final String path;

@DataBoundConstructor
public TrustedProperties(String path) {
this.path = path;
}

public String getPath() {
return path;
}

@Extension @Symbol("fromPropertiesFile")
public static class DescriptorImpl extends DeclarativeEnvironmentContributorDescriptor<TrustedProperties> {

}
}
@@ -0,0 +1,63 @@
/*
* 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.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.environment.impl

import com.cloudbees.groovy.cps.NonCPS
import hudson.Util
import org.jenkinsci.plugins.pipeline.modeldefinition.environment.DeclarativeEnvironmentContributorScript
import org.jenkinsci.plugins.workflow.cps.CpsScript

/**
* Script side of {@link TrustedProperties}.
*/
class TrustedPropertiesScript extends DeclarativeEnvironmentContributorScript<TrustedProperties> {

TrustedPropertiesScript(CpsScript s, TrustedProperties d) {
super(s, d)
}

@Override
List<String> generate(String key) {
String prefix = Util.fixNull(key)
if (!prefix.isEmpty() && !prefix.endsWith("_")) {
prefix = prefix.trim() + "_"
}
String content = script.readTrusted(describable.path)
return generateEnvStrings(prefix, content)
}

@NonCPS
List<String> generateEnvStrings(String prefix, String content) {
Properties p = new Properties()
p.load(new StringReader(content))
List<String> env = []

for (String suffix : p.keySet()) {
env.add("${prefix}${suffix.trim()}=${p.getProperty(suffix, "").trim()}")
}
return env
}
}
@@ -0,0 +1,53 @@
/*
* 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.
*
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.environment.impl;

import org.jenkinsci.plugins.pipeline.modeldefinition.AbstractModelDefTest;
import org.junit.Test;

/**
* Tests {@link TrustedProperties}
*/
public class TrustedPropertiesTest extends AbstractModelDefTest {

@Test
public void globalAndStage() throws Exception {
sampleRepo.write("marker.properties", "NAME=Bobby\nNUM=1\n");
sampleRepo.write("stage/marker.properties", "NAME=Andrew\nNUM=0\n");
sampleRepo.git("init");
sampleRepo.git("add", "marker.properties");
sampleRepo.git("add", "stage/marker.properties");
sampleRepo.git("commit", "--message=Markers");

expect("environmentFromProperties")
.logContains(
"FOO is BAR",
"PROP_NAME is Bobby",
"PROP_NUM is 1",
"P_NAME is Andrew",
"P_NUM is 0").go();
}
}
@@ -0,0 +1,54 @@
/*
* 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 {
environment {
FOO = "BAZ"
PROP = fromPropertiesFile("marker.properties")
}
agent any

stages {
stage("foo") {
environment {
FOO = "BAR"
P_ = fromPropertiesFile("stage/marker.properties")
}

steps {
sh '''
echo "FOO is $FOO"
echo "PROP_NAME is $PROP_NAME"
echo "PROP_NUM is $PROP_NUM"
echo "P_NAME is $P_NAME"
echo "P_NUM is $P_NUM"
'''
}
}
}
}



0 comments on commit 49ebf57

Please sign in to comment.