parameterized-trigger-plugin icon indicating copy to clipboard operation
parameterized-trigger-plugin copied to clipboard

Handle NOT_BUILT and ABORTED as other results

Open hashar opened this issue 1 year ago • 0 comments

Use case

My use case is a post build script which does not need to fully complete, that does get cancelled while waiting and causes the triggering build to fail as a result I need the build step result to be passed through the BlockingBehavior so that I can set it to never block and thus be considered a SUCCESS even when NOT_BUILT or ABORTED.

See: https://phabricator.wikimedia.org/T352319

Solution

When a triggered job is aborted (InterruptedException), the build step was throwing an AbortException which marks the parent job has having been aborted. This change catches it as an ABORTED result and passes it through the BlockingBehavior to determine the build step outcome.

Jenkins Result defines ordinal ranking the results as: SUCCESS, UNSTABLE, FAILURE, NOT_BUILT, ABORTED. The NOT_BUILT and ABORTED results are thus worse than a FAILURE and would be matched as such in BlockingBehavior mapBuildStepResult() and mapBuildResult() which uses isWorseOrEqualTo() for comparison.

BREAKING CHANGE: since ABORTED has a worse ordinal than FAILURE, the aborted build step causes the build to now be marked FAILURE. This is reflected in testCancelsDownstreamBuildWhenInterrupted() test which now becomes a SUCCESS (since the test blocking behavior is to never block).

When a job is cancelled from the build queue (CancellationException), catch it, set the result to NOT_BUILD and pass it through the BlockingBehavior. This lets one to configure the build step to never fail or mark the build unstable when previously the exception would bubble up and call the build to fail.

Add a test testCancelledFromBuildQueue() to cover the CancellationException() is caught and it results in a SUCCESS (since the test blocking behavior is to never block).

ResultConditionTest covers the BlockingBehavior is able to map NOT_BUILD and ABORTED since it has two tests explicitly cancelling and interrupting jobs.

Examples

When a build is aborted, by aborting it:

  Waiting for the completion of downstream-project
  downstream-project #7 started.
  downstream-project #7 completed. Result was ABORTED
  Build step 'Trigger/call builds on other projects' marked build as failure
  Finished: FAILURE

When it is waiting in the build queue and cancelled:

  Waiting for the completion of downstream-project
  Not built: downstream-project has been cancelled while waiting in the queue.
  Build step 'Trigger/call builds on other projects' marked build as failure
  Finished: FAILURE

Testing done

I have created two jobs in a Jenkins spinned up with mvn hpi:run. I have covered the behavior for cancelled jobs with a new test TriggerBuilderTest.testCancelledFromBuildQueue().

The result mapping for ABORTED and NOT_BUILD is already covered by ResultConditionTest.

Submitter checklist

  • [x] Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch! I have used a branch named handle-not_built-canceled
  • [x] Ensure that the pull request title represents the desired changelog entry
  • [x] Please describe what you did
  • [ ] Link to relevant issues in GitHub or Jira
  • [ ] Link to relevant pull requests, esp. upstream and downstream changes
  • [x] Ensure you have provided tests - that demonstrates feature works or fixes the issue

hashar avatar Oct 18 '24 12:10 hashar