ㄍㄨㄥㄅㄞˋㄔㄨㄟˊㄔㄥˊgōngbài‑chuíchéng

功败垂成
  1. 事情在即成功失敗」。 諸葛亮一生結果功敗垂成」。

to fail within sight of success (idiom)​; last-minute failure, to fall at the last hurdle, snatching defeat from the jaws of victory