Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

WASHINGTON — Sean Spicer, who has been one of the best-known faces of the Trump administration in its first half-year, resigned as press secretary on Friday.

"It's been an honor & a privilege to serve @POTUS @realDonaldTrump & this amazing country. I will continue my service through August," Spicer tweeted about two hours after the news first broke.

His sudden departure came shortly before President Donald Trump named Anthony Scaramucci, a transition official in the Trump campaign and longtime Wall Street financier, as named White House communications director.

Sarah Huckabee Sanders, who was promoted to press secretary on Friday, read a statement from the president at the press briefing in which he wished Spicer "continued success" and noted his "great TV ratings."

Trump said in the statement that his administration has "accomplished so much and we are being given credit for so little." The "people get it even if the media doesn't," Trump said.

Scaramucci took the podium in the White House briefing room on Friday afternoon in a breezy debut that seemed intended to smooth over the brusque relationship that Spicer and Sanders have had with the White House press corps. He thanked Spicer for leaving so that Scaramucci could start the communications director job fresh and said he hopes that the former press secretary goes on to "make a lot of money."

One source with direct knowledge of the situation told NBC News that Spicer had informed people on Thursday night that he would quit if Scaramucci were hired.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

Spicer had doubts about Scaramucci’s ability to do the job, the source said. Another source said Spicer didn't present an ultimatum, but indicated that he was not happy about the president's decision.

During Trump's bid for office, Scaramucci helped with fundraising and often appeared on cable TV to defend the candidate. Scaramucci had first supported Scott Walker and then Jeb Bush for president.

In 2015, Scaramucci, a Fox News Channel contributor, railed against Trump on the network, calling him "another hack politician" and criticized his "crazy rhetoric spinning everybody's head around."

Scaramucci, on Friday, said he's apologized repeatedly to the president for those comments and he did so again on camera from the podium.

Spicer's sometimes contentious relationship with the press was often spoofed on "Saturday Night Live" with comedian Melissa McCarthy portraying a volatile "Spicey." He often found himself answering tough questions from reporters on Trump campaign officials' conversations with Moscow and the president's tweets on a wide range of topics.

Related: Toughest Job in D.C.: Explaining President Trump

Some administration insiders viewed the appointment of Scaramucci as a blow to the White House chief of staff, Reince Priebus. Two sources said that Priebus took his objections about Scaramucci to Vice President Mike Pence but found no support.

Publicly, however, Priebus backed the new hire. He told the Associated Press that he supports Scaramucci "100 percent."

“We go back a long, long way and are very good friends,” Priebus said of Scaramucci, in comments to the AP. “All good here.”

Scaramucci also made a point of denying any friction with the chief of staff. During the press briefing, he noted that he had even offered Priebus a top job at his firm, SkyBridge Capital, after the 2012 election.

Scaramucci described their relationship as "a little bit like brothers, where we rough each other up once in a while" while still maintaining a close friendship.

One official, speaking on behalf of Priebus, told NBC News that Priebus "was not trying to block (Scaramucci’s) hiring, he was only trying to slow down the process."

New White House communications director Anthony Scaramucci speaks to members of the media in the Brady Press Briefing room of the White House in Washington on July 21, 2017. Pablo Martinez Monsivais / AP

Spicer told NBC News that he resigned because "I thought it best to give (Scaramucci) and (Huckabee Sanders) a clean slate."

He added that he is looking forward to spending time with his family and hadn't decided what he's going to do next.

Spicer's tenure in the White House press shop has been a rocky one, beginning with his inaccurate description of Trump's inaugural crowd as the biggest in history.

Shortly after the firing of FBI Director James Comey, several sources with knowledge of White House conversations told NBC News that the president began asking those closest to him for their thoughts about how Spicer handled press briefings in comparison to Sanders, who sometimes filled in for Spicer while he was on Navy reserve duty.