Home > Software engineering >  Extract Logs from remoteJobTrigger
Extract Logs from remoteJobTrigger

Time:05-03

I'm using the below code to trigger a remote job in a a jenkins pipeline. After the job is trigger, I'd need to parse the logs and retrieve few information. The below code used handle.lastLog() function, but its returning null. Is there a way to get the logs from triggerRemoteJob ?

            def handle = triggerRemoteJob (
                auth: TokenAuth(apiToken:....'),...,
                
                job: build_job, 
                parameters: parameters1,
                useCrumbCache: true, 
                useJobInfoCache: true,
                overrideTrustAllCertificates: false,
                trustAllCertificates: true
            )       
            def status = handle.getBuildStatus()
            echo "--------------------------------------------------------------------------------"
            echo "Log: "   handle.lastLog()
            echo "--------------------------------------------------------------------------------"

CodePudding user response:

You have many ways to parse the remote job console log by saving the output to a file as follows:

One way is to use the handle object to get the remote build URL:

def remoteBuildOutput = handle.getBuildUrl().toString() "consoleText"
sh "curl -o remote_build_output.txt ${remoteBuildOutput} && cat remote_build_output.txt"

Another way is to output the last build log:

sh "curl -o remote_last_build_output.txt ${env.JENKINS_URL}/job/build_job/lastBuild/consoleText && cat remote_last_build_output.txt"

You can use env.JENKINS_URL if it's set. If not, you can replace it by your Jenkins URL.

build_job is the name of your remote job.

Note that if you're using the /lastBuild/ api call, you can't garantee that the last build is the same build that was triggered by your job (if the build_job is triggered by another system/person at the same time)

One last way is to enable the Parameterized Remote Trigger Plugin logging via the enhancedLogging option.

If set to true, the console output of the remote job is also logged.

def handle = triggerRemoteJob (
            job: build_job, 
            enhancedLogging: true
            ... 
            )

You can then save the current job output in a file and parse what ever you want:

sh "curl -o current_build_output.txt ${env.JENKINS_URL}job/${env.JOB_NAME}/${currentBuild.number}/consoleText && cat current_build_output.txt"

${currentBuild.number} can also be replaced by the ${env.BUILD_NUMBER} variable

  • Related