0

We have jenkins pipeline declarative projects that get their pipeline scripts from SCM (git). Notifications are handled in the post block of the script.

Occasionally, the git command (say git checkout) will fail due to some random error (that isn't the problem I'm trying to solve) while getting the script from SCM.

As a result, the pipeline is never checked out and run, and so the notifications do not happen.

I want to have a groovy script (outside of SCM) that will check if the git checkout has failed, and if it has, I send the appropriate notifications.

To that end, I found the Groovy Postbuild plugin. I see a Groovy Script option in the Post-build action select list for a Freestyle Project, however there is no similar Post-build action available in a pipeline project (except in the post block of the script, but that's in the SCM).

Is there a way to run a post-build script that would handle this scenario?

1 Answer 1

0

there is not Post-build action available in a pipeline project.

Is there a way to run a post-build script that would handle this scenario

Generally yes - there is the post section specifically for that kind of actions.

But there is one major detail.

With Jenkins Pipelines, you define the code either as a script directly in the job configuration, or within your codebase in a Jenkinsfile (which is more preferable from the maintenance perspective) - and based on what you state in your question it looks like you use the latter.

Either way, before running the build, Jenkins doesn't know what's inside the Jenkinsfile. So if your issue is that Jenkins can't check the code out it to load the Jenkinsfile, it won't be able to read the post section either.

Occasionally, the git command (say git checkout) will fail due to some random error (that isn't the problem I'm trying to solve) while getting the script from SCM.

As you noticed, Groovy Post-build action plugin doesn't apply to Pipeline job - so as far as I know, there's no way to handle this situation with a Groovy script.

However, there are a couple things you can do.

First, if you use HTTPS to checkout the code you can set up the build commit status to be reported into your SCM, so you will be able to notice the failed build in its UI, block the PRs on that condition and so on.

Alternatively, Jenkins provides some metrics so you can set up alerting with, say, Grafana and Alertmanager for failing builds.

With that being said, I believe that you have no real way to solve it without dealing with the actual Git failures.

1
  • Thanks for your response. You have correctly re-iterated the problem. We do use HTTPS to checkout the code, it's the connection is set up in the configuration of the pipeline (that is, not in SCM.) If I'm understanding correctly, I would say that the issue is not related to commits or pull requests, but rather checkouts. Regarding solving the git problem, it's very rare, maybe 1 out of 5000 builds. It's due to some kind of transient connection problem.
    – Marek P
    Commented Jun 20 at 14:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.