ST. LOUIS — In seven innings against the Cardinals on Friday night, Jacob deGrom allowed a career-high four home runs. Twice, the Cardinals went yard back-to-back.

Yet the Mets held off the Cardinals, 6-5, and deGrom emerged with his ninth victory. With that, the righthander capped what has been an unusual first half of extremes.

Volatility has cursed the Mets’ starting rotation, and deGrom had been the poster boy.

During a two-start span a month ago, the former All-Star looked out of sorts while allowing 15 runs in eight innings. The performances raised red flags about whether he still was feeling the effects of surgery.

Then deGrom reeled off four straight outings in which he allowed one run or fewer while pitching at least seven innings. In that span, he went 4-0 with an 0.84 ERA.

On Friday night, he survived what became an impromptu home run derby, allowing four solo shots. Cardinals ace Carlos Martinez and deGrom combined to allow six homers on a hot and humid night at Busch Stadium.

“It’s not fun,” deGrom (9-3) said after winning his fifth straight decision. “But at least they were solo shots.”

advertisement | advertise on newsday

Jay Bruce had three hits, including a home run in the fifth that gave the Mets a 5-4 lead; T.J. Rivera knocked in a pair of runs, one with a double in the seventh that made it 6-4, and Addison Reed closed it out for his 15th save.

The Mets (39-45) snapped a three-game losing streak. They remained 9 1⁄2 games behind in the race for the second wild card, thanks to deGrom hanging in without his best stuff.

“He just didn’t have his command,” manager Terry Collins said. “But you looked up and he was still in the game in the seventh inning.”

The Mets took a 2-0 lead when Jose Reyes lined a solo homer in the second. DeGrom retired the first six batters he faced. But in the third, things turned.

In a season that has been defined by a spike in homers, deGrom has been particularly victimized by the long ball. He began the day having allowed 1.2 homers per nine innings, a career high — and things would get worse.

Mets videos

In the third, Randal Grichuk and Paul DeJong hit back-to-back home runs. In the fourth, after the Mets took a 4-2 lead, deGrom gave it right back as Dexter Fowler and Jedd Gyor ko hit back-to-back homers to tie it at 4.

That made it 18 homers against deGrom this season, two more than his previous career high and three more than his total for all of last season.

“A couple of those balls, I don’t know if they should have gone out,” he said. “But whatever.”

Just when the night appeared lost, deGrom settled in, using a pair of double plays to keep the Cardinals off the board. Bruce homered off Martinez in the fifth, allowing the Mets to reclaim the lead. And deGrom would not surrender it.

“When [pitchers] dominate, it’s great, and Jake has that capability every time he goes out there,” Bruce said. “But I feel like a pitcher’s job is to keep you in the ballgame and give you a chance to win. Jake did that tonight.”

advertisement | advertise on newsday

Notes & quotes: Outfielder Michael Conforto is expected to be activated from the disabled list Saturday, though Collins did not commit to penciling him into the starting lineup. He has been sidelined by a bruised left hand . . . Just before Friday’s deadline, the Mets signed first-round pick David Peterson (Oregon) for $2,994,500, the full slot value of the 20th pick in the draft . . . According to a source, the Mets have no plans to manage Zack Wheeler’s innings in the second half. Wheeler, who will start Saturday against the Cardinals, is on pace to pitch 140 innings in his first season back after missing two years because of a bumpy recovery from Tommy John surgery.