1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-05 15:15:44 +02:00

Merge branch 'master' into healthExecuteCheck

This commit is contained in:
Alejandra Ferreiro Vidal 2019-04-02 21:01:29 +02:00 committed by GitHub
commit 0397f152dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 102 additions and 208 deletions

View File

@ -1,55 +1,17 @@
# handlePipelineStepErrors
# ${docGenStepName}
## Description
Used by other steps to make error analysis easier. Lists parameters and other data available to the step in which the error occurs.
## ${docGenDescription}
## Prerequisites
none
## Parameters
| parameter | mandatory | default | possible values |
| -----------------|-----------|---------|-----------------|
| `stepParameters` | yes | | |
| `stepName` | yes | | |
| `echoDetails` | yes | true | true, false |
* `stepParameters` - The parameters from the step to be executed. The list of parameters is then shown in the console output.
* `stepName` - The name of the step executed to be shown in the console output.
* `echoDetails` - If set to true the following will be output to the console:
1. Step beginning: `--- Begin library step: ${stepName}.groovy ---`
2. Step end: `--- End library step: ${stepName}.groovy ---`
3. Step errors:
```log
----------------------------------------------------------
--- An error occurred in the library step: ${stepName}
----------------------------------------------------------
The following parameters were available to the step:
***
${stepParameters}
***
The error was:
***
${err}
***
Further information:
* Documentation of step ${stepName}: .../${stepName}/
* Pipeline documentation: https://...
* GitHub repository for pipeline steps: https://...
----------------------------------------------------------
```
## ${docGenParameters}
## Step configuration
none
## Side effects
none
## Exceptions
none

View File

@ -284,7 +284,6 @@ steps:
sendMail: true
timeoutInSeconds: 900
pipelineStashFilesAfterBuild:
runOpaTests: false
stashIncludes:
checkmarx: '**/*.js, **/*.scala, **/*.py, **/*.go, **/*.xml, **/*.html'
classFiles: '**/target/classes/**/*.class, **/target/test-classes/**/*.class'
@ -293,12 +292,12 @@ steps:
checkmarx: '**/*.mockserver.js, node_modules/**/*.js'
classFiles: ''
sonar: ''
noDefaultExludes: []
pipelineStashFilesBeforeBuild:
runCheckmarx: false
stashIncludes:
buildDescriptor: '**/pom.xml, **/.mvn/**, **/assembly.xml, **/.swagger-codegen-ignore, **/package.json, **/requirements.txt, **/setup.py, **/whitesource_config.py, **/mta*.y*ml, **/.npmrc, **/whitesource.*.json, **/whitesource-fs-agent.config, Dockerfile, **/VERSION, **/version.txt, **/build.sbt, **/sbtDescriptor.json, **/project/*'
deployDescriptor: '**/manifest*.y*ml, **/*.mtaext.y*ml, **/*.mtaext, **/xs-app.json, helm/**, *.y*ml'
git: '**/gitmetadata/**'
git: '.git/**'
opa5: '**/*.*'
opensourceConfiguration: '**/srcclr.yml, **/vulas-custom.properties, **/.nsprc, **/.retireignore, **/.retireignore.json, **/.snyk'
pipelineConfigAndTests: '.pipeline/**'
@ -313,6 +312,8 @@ steps:
pipelineConfigAndTests: ''
securityDescriptor: ''
tests: ''
noDefaultExludes:
- 'git'
seleniumExecuteTests:
buildTool: 'npm'
containerPortMappings:

View File

@ -21,9 +21,19 @@ def getMandatoryParameter(Map map, paramName, defaultValue = null) {
}
def stash(name, include = '**/*.*', exclude = '') {
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude})"
steps.stash name: name, includes: include, excludes: exclude
def stash(name, include = '**/*.*', exclude = '', useDefaultExcludes = true) {
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude}, useDefaultExcludes: ${useDefaultExcludes})"
Map stashParams = [
name: name,
includes: include,
excludes: exclude
]
//only set the optional parameter if default excludes should not be applied
if (!useDefaultExcludes) {
stashParams.useDefaultExcludes = useDefaultExcludes
}
steps.stash stashParams
}
def stashList(script, List stashes) {
@ -46,9 +56,9 @@ def stashList(script, List stashes) {
}
}
def stashWithMessage(name, msg, include = '**/*.*', exclude = '') {
def stashWithMessage(name, msg, include = '**/*.*', exclude = '', useDefaultExcludes = true) {
try {
stash(name, include, exclude)
stash(name, include, exclude, useDefaultExcludes)
} catch (e) {
echo msg + name + " (${e.getMessage()})"
}

View File

@ -78,7 +78,7 @@ class ArtifactSetVersionTest extends BasePiperTest {
return closure()
})
shellRule.setReturnValue("date --universal +'%Y%m%d%H%M%S'", '20180101010203')
shellRule.setReturnValue("date --utc +'%Y%m%d%H%M%S'", '20180101010203')
shellRule.setReturnValue('git diff --quiet HEAD', 0)
helper.registerAllowedMethod('fileExists', [String.class], {true})

View File

@ -240,7 +240,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
],
containerName: 'mavenexecute',
containerPortMappings: [
'selenium/standalone-chrome': [[containerPort: 4444, hostPort: 4444]]
'selenium/standalone-chrome': [[containerPort: 4444]]
],
containerWorkspaces: [
'selenium/standalone-chrome': ''
@ -263,8 +263,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
hasItem('maven:3.5-jdk-8-alpine'),
hasItem('selenium/standalone-chrome'),
))
// assertThat(portList, is(null))
assertThat(portList, hasItem([[name: 'selenium0', containerPort: 4444, hostPort: 4444]]))
assertThat(portList, hasItem([[name: 'selenium0', containerPort: 4444]]))
assertThat(containerCommands.size(), is(1))
assertThat(envList, hasItem(hasItem(allOf(hasEntry('name', 'customEnvKey'), hasEntry ('value','customEnvValue')))))
}

View File

@ -22,22 +22,6 @@ class PipelineStashFilesAfterBuildTest extends BasePiperTest {
@Test
void testStashAfterBuild() {
helper.registerAllowedMethod("fileExists", [String.class], {
searchTerm ->
return false
})
stepRule.step.pipelineStashFilesAfterBuild(
script: nullScript,
juStabUtils: utils
)
// asserts
assertFalse(loggingRule.log.contains('Stash content: checkmarx'))
assertThat(loggingRule.log, containsString('Stash content: classFiles'))
assertThat(loggingRule.log, containsString('Stash content: sonar'))
}
@Test
void testStashAfterBuildWithCheckmarx() {
helper.registerAllowedMethod("fileExists", [String.class], {
searchTerm ->
return true
@ -52,21 +36,4 @@ class PipelineStashFilesAfterBuildTest extends BasePiperTest {
assertThat(loggingRule.log, containsString('Stash content: classFiles'))
assertThat(loggingRule.log, containsString('Stash content: sonar'))
}
@Test
void testStashAfterBuildWithCheckmarxConfig() {
helper.registerAllowedMethod("fileExists", [String.class], {
searchTerm ->
return true
})
stepRule.step.pipelineStashFilesAfterBuild(
script: [commonPipelineEnvironment: [configuration: [steps: [executeCheckmarxScan: [checkmarxProject: 'TestProject']]]]],
juStabUtils: utils,
)
// asserts
assertThat(loggingRule.log, containsString('Stash content: checkmarx'))
assertThat(loggingRule.log, containsString('Stash content: classFiles'))
assertThat(loggingRule.log, containsString('Stash content: sonar'))
}
}

View File

@ -22,27 +22,7 @@ class PipelineStashFilesBeforeBuildTest extends BasePiperTest {
.around(stepRule)
@Test
void testStashBeforeBuildNoOpa() {
stepRule.step.pipelineStashFilesBeforeBuild(script: nullScript, juStabUtils: utils)
// asserts
assertEquals('mkdir -p gitmetadata', shellRule.shell[0])
assertEquals('cp -rf .git/* gitmetadata', shellRule.shell[1])
assertEquals('chmod -R u+w gitmetadata', shellRule.shell[2])
assertThat(loggingRule.log, containsString('Stash content: buildDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: deployDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: git'))
assertFalse(loggingRule.log.contains('Stash content: opa5'))
assertThat(loggingRule.log, containsString('Stash content: opensourceConfiguration'))
assertThat(loggingRule.log, containsString('Stash content: pipelineConfigAndTests'))
assertThat(loggingRule.log, containsString('Stash content: securityDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: tests'))
}
@Test
void testStashBeforeBuildOpa() {
void testStashBeforeBuild() {
stepRule.step.pipelineStashFilesBeforeBuild(script: nullScript, juStabUtils: utils, runOpaTests: true)
@ -56,4 +36,21 @@ class PipelineStashFilesBeforeBuildTest extends BasePiperTest {
assertThat(loggingRule.log, containsString('Stash content: securityDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: tests'))
}
@Test
void testStashBeforeBuildCustomConfig() {
stepRule.step.pipelineStashFilesBeforeBuild(script: nullScript, juStabUtils: utils, runOpaTests: true, stashIncludes: ['myStash': '**.myTest'])
// asserts
assertThat(loggingRule.log, containsString('Stash content: buildDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: deployDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: git'))
assertThat(loggingRule.log, containsString('Stash content: opa5'))
assertThat(loggingRule.log, containsString('Stash content: opensourceConfiguration'))
assertThat(loggingRule.log, containsString('Stash content: pipelineConfigAndTests'))
assertThat(loggingRule.log, containsString('Stash content: securityDescriptor'))
assertThat(loggingRule.log, containsString('Stash content: tests'))
assertThat(loggingRule.log, containsString('Stash content: myStash'))
}
}

View File

@ -135,5 +135,5 @@ def isAppContainer(config){
}
def getTimestamp(pattern){
return sh(returnStdout: true, script: "date --universal +'${pattern}'").trim()
return sh(returnStdout: true, script: "date --utc +'${pattern}'").trim()
}

View File

@ -72,6 +72,12 @@ import hudson.AbortException
* Specifies a dedicated user home directory for the container which will be passed as value for environment variable `HOME`.
*/
'dockerWorkspace',
/**
* Kubernetes Security Context used for the pod.
* Can be used to specify uid and fsGroup.
* See: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
*/
'securityContext',
/**
* Specific stashes that should be considered for the step execution.
*/
@ -83,13 +89,7 @@ import hudson.AbortException
/**
*
*/
'stashIncludes',
/**
* Kubernetes Security Context used for the pod.
* Can be used to specify uid and fsGroup.
* See: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
*/
'securityContext'
'stashIncludes'
])
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.minus([
'stashIncludes',
@ -277,19 +277,15 @@ private List getContainerList(config) {
}
if (config.containerPortMappings?.get(imageName)) {
def portMapping = { m ->
[
name: m.name,
containerPort: m.containerPort,
hostPort: m.hostPort
]
}
def ports = []
def portCounter = 0
config.containerPortMappings.get(imageName).each {mapping ->
mapping.name = "${containerName}${portCounter}".toString()
ports.add(portMapping(mapping))
def name = "${containerName}${portCounter}".toString()
if(mapping.containerPort != mapping.hostPort) {
echo ("[WARNING][${STEP_NAME}]: containerPort and hostPort are different for container '${containerName}'. "
+ "The hostPort will be ignored.")
}
ports.add([name: name, containerPort: mapping.containerPort])
portCounter ++
}
containerSpec.ports = ports

View File

@ -1,5 +1,6 @@
import com.cloudbees.groovy.cps.NonCPS
import com.sap.piper.GenerateDocumentation
import com.sap.piper.ConfigurationHelper
import groovy.text.SimpleTemplateEngine
@ -10,14 +11,49 @@ import groovy.transform.Field
@Field Set GENERAL_CONFIG_KEYS = []
@Field Set STEP_CONFIG_KEYS = []
@Field Set PARAMETER_KEYS = [
/**
* If set to true the following will be output to the console:
* 1. Step beginning: `--- Begin library step: ${stepName}.groovy ---`
* 2. Step end: `--- End library step: ${stepName}.groovy ---`
* 3. Step errors:
*
* ```log
* ----------------------------------------------------------
* --- An error occurred in the library step: ${stepName}
* ----------------------------------------------------------
* The following parameters were available to the step:
* ***
* ${stepParameters}
* ***
* The error was:
* ***
* ${err}
* ***
* Further information:
* * Documentation of step ${stepName}: .../${stepName}/
* * Pipeline documentation: https://...
* * GitHub repository for pipeline steps: https://...
* ----------------------------------------------------------
* ```
* @possibleValues `true`, `false`
*/
'echoDetails',
/** Defines the url of the library's documentation that will be used to generate the corresponding links to the step documentation.*/
'libraryDocumentationUrl',
/** Defines the url of the library's repository that will be used to generate the corresponding links to the step implementation.*/
'libraryRepositoryUrl',
/** Defines the name of the step executed that will be shown in the console output.*/
'stepName',
/** */
'stepNameDoc',
/** Defines the parameters from the step to be executed. The list of parameters is then shown in the console output.*/
'stepParameters'
]
/**
* Used by other steps to make error analysis easier. Lists parameters and other data available to the step in which the error occurs.
*/
@GenerateDocumentation
void call(Map parameters = [:], body) {
// load default & individual configuration
Map config = ConfigurationHelper.newInstance(this)

View File

@ -5,7 +5,7 @@ import com.sap.piper.ConfigurationHelper
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field Set STEP_CONFIG_KEYS = ['runCheckmarx', 'stashIncludes', 'stashExcludes']
@Field Set STEP_CONFIG_KEYS = ['noDefaultExludes', 'stashIncludes', 'stashExcludes']
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
void call(Map parameters = [:]) {
@ -28,9 +28,6 @@ void call(Map parameters = [:]) {
.mixinGeneralConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
.mixin([
runCheckmarx: (script.commonPipelineEnvironment.configuration?.steps?.executeCheckmarxScan?.checkmarxProject != null && script.commonPipelineEnvironment.configuration.steps.executeCheckmarxScan.checkmarxProject.length()>0)
])
.mixin(parameters, PARAMETER_KEYS)
.use()
@ -40,27 +37,9 @@ void call(Map parameters = [:]) {
stepParam1: parameters?.script == null
], config)
// store files to be checked with checkmarx
if (config.runCheckmarx) {
utils.stash(
'checkmarx',
config.stashIncludes.checkmarx,
config.stashExcludes.checkmarx
)
config.stashIncludes.each {stashKey, stashIncludes ->
def useDefaultExcludes = !config.noDefaultExludes.contains(stashKey)
utils.stashWithMessage(stashKey, "[${STEP_NAME}] no files detected for stash '${stashKey}': ", stashIncludes, config.stashExcludes[stashKey]?:'', useDefaultExcludes)
}
utils.stashWithMessage(
'classFiles',
"[${STEP_NAME}] Failed to stash class files.",
config.stashIncludes.classFiles,
config.stashExcludes.classFiles
)
utils.stashWithMessage(
'sonar',
"[${STEP_NAME}] Failed to stash sonar files.",
config.stashIncludes.sonar,
config.stashExcludes.sonar
)
}
}

View File

@ -5,14 +5,14 @@ import com.sap.piper.ConfigurationHelper
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field Set STEP_CONFIG_KEYS = ['runOpaTests', 'stashIncludes', 'stashExcludes']
@Field Set STEP_CONFIG_KEYS = ['noDefaultExludes', 'stashIncludes', 'stashExcludes']
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
void call(Map parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters, stepNameDoc: 'stashFiles') {
def utils = parameters.juStabUtils
Utils utils = parameters.juStabUtils
if (utils == null) {
utils = new Utils()
}
@ -21,9 +21,6 @@ void call(Map parameters = [:]) {
if (script == null)
script = this
//additional includes via passing e.g. stashIncludes: [opa5: '**/*.include']
//additional excludes via passing e.g. stashExcludes: [opa5: '**/*.exclude']
Map config = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
@ -38,59 +35,9 @@ void call(Map parameters = [:]) {
stepParam1: parameters?.script == null
], config)
if (config.runOpaTests){
utils.stash('opa5', config.stashIncludes?.get('opa5')?config.stashIncludes.opa5:'**/*.*', config.stashExcludes?.get('opa5')?config.stashExcludes.opa5:'')
config.stashIncludes.each {stashKey, stashIncludes ->
def useDefaultExcludes = !config.noDefaultExludes.contains(stashKey)
utils.stashWithMessage(stashKey, "[${STEP_NAME}] no files detected for stash '${stashKey}': ", stashIncludes, config.stashExcludes[stashKey]?:'', useDefaultExcludes)
}
//store build descriptor files depending on technology, e.g. pom.xml, package.json
utils.stash(
'buildDescriptor',
config.stashIncludes.buildDescriptor,
config.stashExcludes.buildDescriptor
)
//store deployment descriptor files depending on technology, e.g. *.mtaext.yml
utils.stashWithMessage(
'deployDescriptor',
"[${STEP_NAME}] no deployment descriptor files provided: ",
config.stashIncludes.deployDescriptor,
config.stashExcludes.deployDescriptor
)
//store git metadata for SourceClear agent
sh "mkdir -p gitmetadata"
sh "cp -rf .git/* gitmetadata"
sh "chmod -R u+w gitmetadata"
utils.stashWithMessage(
'git',
"[${STEP_NAME}] no git repo files detected: ",
config.stashIncludes.git,
config.stashExcludes.git
)
//store nsp & retire exclusion file for future use
utils.stashWithMessage(
'opensourceConfiguration',
"[${STEP_NAME}] no opensourceConfiguration files provided: ",
config.stashIncludes.get('opensourceConfiguration'),
config.stashExcludes.get('opensourceConfiguration')
)
//store pipeline configuration including additional groovy test scripts for future use
utils.stashWithMessage(
'pipelineConfigAndTests',
"[${STEP_NAME}] no pipeline configuration and test files found: ",
config.stashIncludes.pipelineConfigAndTests,
config.stashExcludes.pipelineConfigAndTests
)
utils.stashWithMessage(
'securityDescriptor',
"[${STEP_NAME}] no security descriptor found: ",
config.stashIncludes.securityDescriptor,
config.stashExcludes.securityDescriptor
)
//store files required for tests, e.g. Gauge, SUT, ...
utils.stashWithMessage(
'tests',
"[${STEP_NAME}] no files for tests provided: ",
config.stashIncludes.tests,
config.stashExcludes.tests
)
}
}