1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-28 05:47:08 +02:00

add step materializeLogFile to jenkins library (#1046)

This commit is contained in:
RainerHimmeroeder 2019-12-16 22:09:25 +01:00 committed by Oliver Nocon
parent 23d7058fdd
commit 188686f5a7
5 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,18 @@
# ${docGenStepName}
## ${docGenDescription}
## Prerequsites
None
## Example
```groovy
jenkinsMaterializeLog script:this, { name -> println "log file: " + name }
```
## ${docGenParameters}
## ${docGenConfiguration}
## ${docJenkinsPluginDependencies}

View File

@ -53,6 +53,7 @@ nav:
- handlePipelineStepErrors: steps/handlePipelineStepErrors.md
- healthExecuteCheck: steps/healthExecuteCheck.md
- influxWriteData: steps/influxWriteData.md
- jenkinsMaterializeLog: steps/jenkinsMaterializeLog.md
- kanikoExecute: steps/kanikoExecute.md
- karmaExecuteTests: steps/karmaExecuteTests.md
- mailSendNotification: steps/mailSendNotification.md

View File

@ -127,3 +127,7 @@ def getLibrariesInfo() {
return libraries
}
def getInstance() {
Jenkins.get()
}

View File

@ -0,0 +1,54 @@
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import com.sap.piper.GitUtils
import hudson.AbortException
import util.BasePiperTest
import util.JenkinsDockerExecuteRule
import util.JenkinsEnvironmentRule
import util.JenkinsLoggingRule
import util.JenkinsReadMavenPomRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.JenkinsWriteFileRule
import util.Rules
import com.sap.piper.JenkinsUtils
import jenkins.model.Jenkins
class JenkinsMaterializeLogTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
class JenkinsUtilsMock extends JenkinsUtils {
def getInstance() {
def map = [getComputer:{return null}];
return map
}
}
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
.around(loggingRule)
.around(writeFileRule)
.around(stepRule)
@Test
void testMaterializeLog() {
def map = [script: nullScript, jenkinsUtilsStub: new JenkinsUtilsMock()]
def body = { name -> def msg = "hello " + name }
binding.setVariable('currentBuild', [result: 'UNSTABLE', rawBuild: [getLogInputStream: {return new StringBufferInputStream("this is the input")}]])
binding.setVariable('env', [NODE_NAME: 'anynode', WORKSPACE: '.'])
stepRule.step.jenkinsMaterializeLog(map, body)
}
}

View File

@ -0,0 +1,85 @@
import com.sap.piper.ConfigurationHelper
import com.sap.piper.GenerateDocumentation
import com.sap.piper.k8s.ContainerMap
import hudson.FilePath
import com.sap.piper.Utils
import groovy.transform.Field
import java.util.UUID
import java.io.File
import com.cloudbees.groovy.cps.NonCPS
import com.sap.piper.JenkinsUtils
import static com.sap.piper.Prerequisites.checkScript
@Field String STEP_NAME = getClass().getName()
@Field Set GENERAL_CONFIG_KEYS = []
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
/**
* This step allows you to materialize the Jenkins log file of the running build.
*
* It acts as a wrapper executing the passed function body.
*
* Note: the file that has been created during step execution will be removed automatically.
*/
@GenerateDocumentation
void call(Map parameters = [:], body) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
checkScript(this, parameters)
withMaterializedLogFile(body, jenkinsUtils)
}
}
@NonCPS
def writeLogToFile(fp) {
def logInputStream = currentBuild.rawBuild.getLogInputStream()
fp.copyFrom(logInputStream)
logInputStream.close()
}
@NonCPS
def deleteLogFile(fp) {
if(fp.exists()) {
fp.delete()
}
}
def getFilePath(logFileName, jenkinsUtils) {
def nodeName = env['NODE_NAME']
if (nodeName == null || nodeName.size() == 0) {
throw new IllegalArgumentException("Environment variable NODE_NAME is undefined")
}
def file = new File(logFileName)
def instance = jenkinsUtils.getInstance()
if (instance == null) {
// fall back
return new FilePath(file);
} else {
def computer = instance.getComputer(nodeName)
if (computer == null) {
// fall back
println "Warning: Jenkins returned computer instance null on node " + nodeName
return new FilePath(file);
}
def channel = computer.getChannel()
return new FilePath(channel, file)
}
}
// The method cannot be NonCPS because we call CPS
def withMaterializedLogFile(body, jenkinsUtils) {
def tempLogFileName = "${env.WORKSPACE}/log-${UUID.randomUUID().toString()}.txt"
def fp = getFilePath(tempLogFileName, jenkinsUtils)
writeLogToFile(fp)
try {
body(tempLogFileName)
} finally {
deleteLogFile(fp)
}
}