SAN FRANCISCO — Near the end of July, Robbie Ray pitched in a game for the Giants at Petco Park.

He allowed one run on four hits and a walk over six innings.

Four days later, he was traded to San Diego. He was acquired to help bolster the Padres’ starting rotation and their chances of making the postseason, as he had at the time posted a 1.09 ERA over his previous eight starts.

It has not gone anywhere near that smoothly for Ray since then.

Back at Oracle Park on Friday night facing the struggling team for which he pitched from 2024 until last month’s trade deadline and in a ballpark where he has pitched well, Ray got his 100th career victory. But it was much more uncertain than it seemed it would be at the start.

He allowed five runs in five innings before the Padres held on late to beat the Giants 7-5.

It took four relievers holding it together over the final four innings after Ray surrendered a grand slam in the fifth inning that buoyed the Giants after the Padres had gone up 7-1 in the third inning.

“The bullpen is unbelievable,” Ray said. “They picked me up today, which was huge. And then the bats came out early, which is great to see too. Overall, this is just a really solid team, and I think we’re getting hot right at the perfect time.”

The Padres’ fifth straight victory maintained their half-game lead over the Diamondbacks in the race for the final National League playoff spot.

Ray was actually not nearly as displeased with Friday’s start as the numbers suggested he might be. It was case of the process outweighing the performance, which is not unusual for a veteran pitcher looking for his feel.

“Outside of (the grand slam), I felt really good,” Ray said. “I felt like that’s the most consistent my shapes have been. I felt like the four-seam was getting a lot of ride. The two-seam was getting a lot of horizontal movement on it. Outside of that one pitch, I felt like everything was more consistent. It’s just a tough situation with a really good hitter.”

As shaky as Ray had been with the Padres, twice allowing one run in six innings but posting a 7.48 ERA in his other five starts, Friday was the first time he allowed a run in the first inning.

Then the Padres commenced giving him a big cushion, scoring two runs in the second inning and batting around and scoring five times in the second.

They had six doubles and a triple in those two innings against Giants starter Anthony Molina, who was replaced before the start of the fourth.

One of the things the Padres coaches and front office will be assessing over the next two weeks is which starting pitchers could be part of a postseason rotation.

Ray (12-8, 3.57) is certainly not a lock.

His start on July 30 in San Diego was his sixth time going at least six innings and allowing no more than one earned run in a span of eight outings. That stretch lowered his ERA to 3.03 for the season.

He had a 4.71 ERA in his six starts for the Padres heading into Friday, and the Padres were 2-4 in those games.

He and the Padres took advantage of what they had to deal with Friday.

The Giants entered the series 23 games below .500 and have retooled their roster with Triple-A call-ups and waiver wire pickups.

Rafael Devers was the only player in the Giants lineup to have ever faced Ray, who entered the game with a 2.22 ERA in 11 starts at Oracle Park this season.

They scored against him right away, as rookie Jonah Cox led off with a single, stole second and went home from there on a single by rookie Bryce Eldridge.

Ray had a 2-1 lead when he took the mound a second time.

The Padres answered the Giants with a leadoff double by Jackson Merrill and a single by Dustin Harris and double by Freddy Fermin with two outs.

It was 7-1 the next time Ray took the mound, as four of the first five Padres batters got hits and they sent 10 batters to the plate.

In the third inning, Xander Bogaerts drove in two runners with a double, Harris hit an RBI triple high off the wall in right field and Fermin hit his second run-scoring double.

They had a chance to put the game practically out of reach in the half-inning before the Giants struck back, but a single by Samad Taylor and walk by Harris were followed by three straight outs.

Ray had retired seven straight batters when the bottom of the fifth inning began.

But Andrew Knizner doubled to lead off the bottom of the fifth before Ray walked the next two batters and Devers reached well below the zone and sent a curveball just over the tall wall in right field to make it 7-5.

“With the walks, I felt like I threw some really good pitches, but they were just off,” Ray said. “… Just missing a little bit with the two-seam away. In that situation, you just gotta bear down and not walk those guys, especially with Raffy Devers coming up.”

Ray finished the inning before Yuki Matsui took over in the sixth.

Matsui and Adrian Morejón set down six straight batters between them.

The Padres again failed to capitalize in the eighth when three straight outs followed Fernando Tatis Jr.’s leadoff triple.

But Bradgley Rodriguez worked around a leadoff double in the eighth, and Mason Miller struck out Eldridge to end the game after a pair of walks and a bunt single with two outs loaded the bases in the ninth.

“A little bit more tense in the end than what we wanted,” Padres manager Craig Stammen said. “But that’s why it’s nice to have Mason Miller on the mound at the end.”