Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline - Reading previous stage log

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?

like image 453
Chadi Avatar asked Jul 06 '16 08:07

Chadi


People also ask

How do you skip failed stage in Jenkins Pipeline?

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)

How do you skip stage in Jenkins scripted Pipeline?

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.

Where are Jenkins logs stored?

Log files should be at /var/log/jenkins/jenkins. log , unless customized in org. jenkins-ci.

How to get into a particular stage in Jenkins?

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/

Is it possible to run a Jenkins build without having rawbuild?

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.

Is it possible to email the logs of a failed stage?

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)

How to get the real username and password of Jenkins users?

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.


1 Answers

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
like image 59
Gerasim Avatar answered Nov 15 '22 07:11

Gerasim