Consider a Jenkins Pipeline with two stages, Stage A then Stage B.
In Stage B, is it possible to parse the logs of Stage A for some particular text?
To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)
So to mark a stage as skipped you need to call static method markStageSkippedForConditional passing the name of the stage you are skipping. If you're using a version of Jenkins older than mid 2019, you must uncheck Use Groovy Sandbox checkbox, since the Utils method was not yet whitelisted for external use.
Log files should be at /var/log/jenkins/jenkins. log , unless customized in org. jenkins-ci.
Use idto get into a particular stage, for this example the logs of Test stage which has an idof 15. http://localhost:8080/blue/rest/organizations/jenkins/pipelines/test/runs/1/nodes/15/log/
No need to access rawBuild, instead you can do the following without any special permissions: This will allow you to see the result of the previous build without needing to have any special privileges set by the Jenkins administrator. It should be able to be run anywhere.
This is good for cases where you just want to email the logs of a particular failed stage. Here's how hope it helps. On the sample pipeline below a job named testis executed once (build #1)
When you add username and password pair in Jenkins -> Manage Jenkins -> Credentials, you can given an name to this pair, the name called credentials Id. If you not given a name, Jenkins will generate a random string as the value for crendentails Id. Therefor, the real username and password is stored on Jenkins, won't be on github.
Use tee
to split the output to both stdout and file. Next parse the file for your text.
STAGE_A_LOG_FILE = 'stage_a.log'
pipeline {
agent any
stages {
stage('Stage A') {
steps {
script {
// tee log into file
tee(STAGE_A_LOG_FILE) {
echo 'print some Stage_A log content ...'
}
}
}
}
stage('Stage B') {
steps {
script {
// search log file for 'Stage_A'
regex = java.util.regex.Pattern.compile('some (Stage_A) log')
matcher = regex.matcher(readFile(STAGE_A_LOG_FILE))
if (matcher.find()) {
echo "found: ${matcher.group(1)}"
}
}
}
}
}
}
Pipeline output:
print some Stage_A log content ...
found: Stage_A
Finished: SUCCESS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With