mirror of
https://github.com/firstBitMarksistskaya/jenkins-lib.git
synced 2025-02-19 19:59:48 +02:00
Конфиг по умолчанию, мерж конфигураций, интеграционные тесты
This commit is contained in:
parent
9d1edee928
commit
6f21593e0b
@ -46,6 +46,7 @@ contributor(ctx) {
|
|||||||
method(name: 'readProperties', type: 'Object', namedParams: [parameter(name: 'defaults', type: 'java.util.Map'), parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'interpolate', type: 'java.lang.Boolean'), parameter(name: 'text', type: 'java.lang.String'),], doc: 'Read properties from files in the workspace or text.')
|
method(name: 'readProperties', type: 'Object', namedParams: [parameter(name: 'defaults', type: 'java.util.Map'), parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'interpolate', type: 'java.lang.Boolean'), parameter(name: 'text', type: 'java.lang.String'),], doc: 'Read properties from files in the workspace or text.')
|
||||||
method(name: 'readFile', type: 'java.lang.String', params: [file: 'java.lang.String'], doc: 'Read file from workspace')
|
method(name: 'readFile', type: 'java.lang.String', params: [file: 'java.lang.String'], doc: 'Read file from workspace')
|
||||||
method(name: 'readFile', type: 'java.lang.String', namedParams: [parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'encoding', type: 'java.lang.String'),], doc: 'Read file from workspace')
|
method(name: 'readFile', type: 'java.lang.String', namedParams: [parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'encoding', type: 'java.lang.String'),], doc: 'Read file from workspace')
|
||||||
|
method(name: 'fileExists', type: 'Object', params: [file: 'java.lang.String'], doc: 'Verify if file exists in workspace')
|
||||||
method(name: 'readTrusted', type: 'Object', params: [path: 'java.lang.String'], doc: 'Read trusted file from SCM')
|
method(name: 'readTrusted', type: 'Object', params: [path: 'java.lang.String'], doc: 'Read trusted file from SCM')
|
||||||
method(name: 'readYaml', type: 'Object', params: [:], doc: 'Read yaml from files in the workspace or text.')
|
method(name: 'readYaml', type: 'Object', params: [:], doc: 'Read yaml from files in the workspace or text.')
|
||||||
method(name: 'readYaml', type: 'Object', namedParams: [parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'text', type: 'java.lang.String'),], doc: 'Read yaml from files in the workspace or text.')
|
method(name: 'readYaml', type: 'Object', namedParams: [parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'text', type: 'java.lang.String'),], doc: 'Read yaml from files in the workspace or text.')
|
||||||
|
@ -1,28 +1,46 @@
|
|||||||
package ru.pulsar.jenkins.library.configuration
|
package ru.pulsar.jenkins.library.configuration
|
||||||
|
|
||||||
|
import com.cloudbees.groovy.cps.NonCPS
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import org.apache.commons.beanutils.BeanUtils
|
import org.apache.commons.beanutils.BeanUtils
|
||||||
import ru.pulsar.jenkins.library.IStepExecutor
|
import ru.pulsar.jenkins.library.IStepExecutor
|
||||||
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
||||||
|
|
||||||
class ConfigurationReader implements Serializable {
|
class ConfigurationReader implements Serializable {
|
||||||
static JobConfiguration create(String config) {
|
|
||||||
|
private static ObjectMapper mapper = new ObjectMapper()
|
||||||
|
private static final String DEFAULT_CONFIGURATION_RESOURCE = 'globalConfiguration.json'
|
||||||
|
|
||||||
|
static JobConfiguration create() {
|
||||||
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
|
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
|
||||||
|
|
||||||
def mapper = new ObjectMapper()
|
def globalConfig = steps.libraryResource DEFAULT_CONFIGURATION_RESOURCE
|
||||||
def globalConfig = steps.libraryResource 'globalConfiguration.json'
|
|
||||||
|
|
||||||
def globalConfiguration = mapper.readValue(globalConfig, JobConfiguration.class)
|
def globalConfiguration = mapper.readValue(globalConfig, JobConfiguration.class)
|
||||||
def jobConfiguration = mapper.readValue(config, JobConfiguration.class)
|
|
||||||
|
|
||||||
BeanUtils.describe(jobConfiguration).entrySet().stream()
|
|
||||||
.filter({ e -> e.getValue() != null })
|
|
||||||
.filter({ e -> e.getKey() != "class" })
|
|
||||||
.filter({ e -> e.getKey() != "metaClass" })
|
|
||||||
.forEach { e ->
|
|
||||||
BeanUtils.setProperty(globalConfiguration, e.getKey(), e.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
return globalConfiguration
|
return globalConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static JobConfiguration create(String config) {
|
||||||
|
def globalConfiguration = create()
|
||||||
|
def jobConfiguration = mapper.readValue(config, JobConfiguration.class)
|
||||||
|
|
||||||
|
return mergeConfigurations(globalConfiguration, jobConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonCPS
|
||||||
|
private static JobConfiguration mergeConfigurations(
|
||||||
|
JobConfiguration baseConfiguration,
|
||||||
|
JobConfiguration configurationToMerge
|
||||||
|
) {
|
||||||
|
BeanUtils.describe(configurationToMerge).entrySet().stream()
|
||||||
|
.filter({ e -> e.getValue() != null })
|
||||||
|
.filter({ e -> e.getKey() != "class" })
|
||||||
|
.filter({ e -> e.getKey() != "metaClass" })
|
||||||
|
.forEach {e ->
|
||||||
|
BeanUtils.setProperty(baseConfiguration, e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return baseConfiguration;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package ru.pulsar.jenkins.library.configuration
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
|
||||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription
|
import com.fasterxml.jackson.annotation.JsonPropertyDescription
|
||||||
|
import org.apache.commons.lang3.builder.ReflectionToStringBuilder
|
||||||
|
import org.apache.commons.lang3.builder.ToStringStyle
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
class JobConfiguration implements Serializable {
|
class JobConfiguration implements Serializable {
|
||||||
@ -10,4 +12,9 @@ class JobConfiguration implements Serializable {
|
|||||||
|
|
||||||
@JsonPropertyDescription("Имя настроенной утилиты sonar-scanner.")
|
@JsonPropertyDescription("Имя настроенной утилиты sonar-scanner.")
|
||||||
String sonarScannerToolName
|
String sonarScannerToolName
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String toString() {
|
||||||
|
return ReflectionToStringBuilder.toString(this, ToStringStyle.NO_CLASS_NAME_STYLE)
|
||||||
|
}
|
||||||
}
|
}
|
77
test/integration/groovy/jobConfigurationTest.groovy
Normal file
77
test/integration/groovy/jobConfigurationTest.groovy
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import org.apache.commons.io.IOUtils
|
||||||
|
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
|
||||||
|
import org.jenkinsci.plugins.workflow.job.WorkflowJob
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.jvnet.hudson.test.JenkinsRule
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
|
class jobConfigurationTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public JenkinsRule rule = new JenkinsRule()
|
||||||
|
|
||||||
|
@Before
|
||||||
|
void configureGlobalGitLibraries() {
|
||||||
|
RuleBootstrapper.setup(rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void "jobConfiguration should not fail without file"() {
|
||||||
|
|
||||||
|
def pipeline = """
|
||||||
|
pipeline {
|
||||||
|
agent any
|
||||||
|
stages {
|
||||||
|
stage('test') {
|
||||||
|
steps {
|
||||||
|
echo jobConfiguration().toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""".stripIndent()
|
||||||
|
final CpsFlowDefinition flow = new CpsFlowDefinition(pipeline, true)
|
||||||
|
final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project')
|
||||||
|
workflowJob.definition = flow
|
||||||
|
|
||||||
|
rule.buildAndAssertSuccess(workflowJob)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void "jobConfiguration should merge configurations"() {
|
||||||
|
|
||||||
|
def file = IOUtils.resourceToString(
|
||||||
|
'jobConfiguration.json',
|
||||||
|
StandardCharsets.UTF_8,
|
||||||
|
this.getClass().getClassLoader()
|
||||||
|
);
|
||||||
|
|
||||||
|
def writeFile = """
|
||||||
|
writeFile text: \"\"\"$file\"\"\", file: 'jobConfiguration.json'
|
||||||
|
"""
|
||||||
|
|
||||||
|
def pipeline = """
|
||||||
|
pipeline {
|
||||||
|
agent any
|
||||||
|
stages {
|
||||||
|
stage('test') {
|
||||||
|
steps {
|
||||||
|
$writeFile
|
||||||
|
echo jobConfiguration().toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""".stripIndent()
|
||||||
|
final CpsFlowDefinition flow = new CpsFlowDefinition(pipeline, true)
|
||||||
|
final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project')
|
||||||
|
workflowJob.definition = flow
|
||||||
|
|
||||||
|
def run = rule.buildAndAssertSuccess(workflowJob)
|
||||||
|
rule.assertLogContains('v8version=8.3.12.1500', run)
|
||||||
|
rule.assertLogContains('sonarScannerToolName=sonar-scanner', run)
|
||||||
|
}
|
||||||
|
}
|
3
test/integration/resources/jobConfiguration.json
Normal file
3
test/integration/resources/jobConfiguration.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"v8version": "8.3.12.1500"
|
||||||
|
}
|
@ -2,9 +2,14 @@ import ru.pulsar.jenkins.library.configuration.ConfigurationReader
|
|||||||
import ru.pulsar.jenkins.library.configuration.JobConfiguration
|
import ru.pulsar.jenkins.library.configuration.JobConfiguration
|
||||||
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
import ru.pulsar.jenkins.library.ioc.ContextRegistry
|
||||||
|
|
||||||
JobConfiguration call(String path = "") {
|
JobConfiguration call(String path = "jobConfiguration.json") {
|
||||||
ContextRegistry.registerDefaultContext(this)
|
ContextRegistry.registerDefaultContext(this)
|
||||||
|
|
||||||
def config = readFile(path)
|
if (fileExists(path)) {
|
||||||
return ConfigurationReader.create(config)
|
def config = readFile(path)
|
||||||
|
return ConfigurationReader.create(config)
|
||||||
|
} else {
|
||||||
|
return ConfigurationReader.create()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user