It was getting close. But CC Sabathia and the Yankees were able to get his new contract done last night to avoid the ace opting out of his previous deal.

Sabathia tweeted the announcement of his new contract at 7:23 p.m., four hours and 36 minutes before he could have opted out and become a free agent. The Yankees gave him an extension for $30 million guaranteed, with the increase potentially cresting to $50 million if an option is exercised.

Sabathia had four years and $92 million left on his deal through 2015 ($23 million annually). Now, in 2016, he will receive $25 million. And in 2017, he has a vesting club option for $25 million with a $5 million buyout. Thus, Sabathia is assured five years and $122 million, with the possibility of $142 million over six years.

“I was confident that we would get to an agreement,” Sabathia said. “I wanted to stay here.”

Sabathia’s new contract represents the highest average annual value for a pitcher on a multiyear deal ($24.4 million). Cliff Lee had held the record with his five-year, $120 million pact with the Phillies (an average of $24 million).

The Yankees initially offered Sabathia a deal for five years and $120.5 million, allowing him to just top Lee’s mark. But the Yankees upped their offer to ensure he would not opt out and hit the free-agent market.

Sabathia’s 2017 option will vest automatically unless he finishes 2016 on the disabled list, spends more than 45 days on the DL in 2016 due to a shoulder injury, or is forced to pitch out of the bullpen six times in 2016 because of shoulder problems. Sabathia also will continue to have a no-trade clause.

“It was a big motivation,” Sabathia said of avoiding the free agency drama. “I didn’t want to be a part of being a free agent and opting out and doing all that stuff.”

General manager Brian Cashman released a statement, praising the 31-year-old as “the ace of our pitching staff, a leader in our clubhouse and a driving force for the Yankees in our community. He is exactly the type of player and person that Yankees fans and this organization can be proud of.”

Sabathia lost 25 pounds last winter but put some of it back on this past season. He said he will prioritize the weight loss again, though he admitted he “got a little lax during the season” with his conditioning.

“I think it’s something I do because it’s for me and to be proactive,” he said of losing weight. “I just need to go out and be healthy and try to do what I can to be out there for every start. For me, that means losing weight. That’s what I’ll do.”

With Sabathia’s status resolved, the Yankees can move on to free agent starter Freddy Garcia, whose agent, Peter Greenberg, said yesterday the Yankees’ priority was resolving Sabathia’s contract, but the team also wanted Garcia to return.

Garcia, 35, was 12-8 this season with a 3.62 ERA.

“There’s definitely interest on their part and there’s interest on Freddy’s part in coming back,” Greenberg said.

The Yankees likely would offer Garcia arbitration if they don’t sign him prior to that point. Garcia is a Type-B free agent, meaning the team would get draft pick compensation.

*

Yankees hitting coach Kevin Long will have surgery today to remove bone spurs from his right big toe. … Brett Gardner, a Gold Glove candidate, was named the left field winner of the 2011 Fielding Bible Awards. The awards are not league specific — one player at each spot in the majors is honored.