0

In my company I have a pipeline that runs several jobs. I wanted to get the result of each job and write each of these results in a file or variable, later email it to me. Is there such a possibility? Remembering that: I don't want the result of the pipeline, but the result of each of the jobs that are inside it.

I even tried to make requests via api, but for each pipeline it would have to have a code and that is not feasible at all, the maintenance issue.

1 Answers1

0

When you trigger a job inside a pipeline, you use the build job step. This step has a property called propagate that:

If enabled (default state), then the result of this step is that of the downstream build (e.g., success, unstable, failure, not built, or aborted). If disabled, then this step succeeds even if the downstream build is unstable, failed, etc.; use the result property of the return value as needed.

You can write a wrapper for calling jobs, that stores the result of each job (and maybe other data useful for debugging, like build url), so you can use it later to construct the contents of an email. E.g.

def jobResults = [:]

def buildJobAndStoreResult(jobName, jobParams) {
  def run = build job: jobName, parameters: jobParams, propagate: false
  jobResults[jobName] = [
    result: run.result
  ]
}

Then you can constuct the body of an email by iterating through the map e.g.

emailBody = "SUMMARY\n\n"

jobResults.each() { it ->
​  str += "${it.key}: ${it.value.result}\n"
}

And use the mail step to send out a report.

It's worth thinking if you want your pipeline to fail after sending the email if any of the called jobs failed, and adding links from your email report to the failed jobs and caller pipeline.

Vasiliki Siakka
  • 1,185
  • 8
  • 15