Revert back to previous build image

How to revert back to previous build image if the current build fails. (Using pipeline code)

How would you do it from the command line without Jenkins? Do that same thing from an sh step, a bat step, or a powershell step.

Declarative Pipeline has the post section that can perform specific actions when its associated step fails. Scripted Pipeline can catch exceptions and perform actions after those exceptions.