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

President Donald Trump on Friday removed his chief of staff, Reince Priebus, and tapped Homeland Security Secretary John Kelly for the job.

The stunning shakeup is the latest chapter in the West Wing drama that has unfolded publicly in recent days. White House Communications Director Anthony Scaramucci attacked Priebus in profane terms in an interview on Thursday, predicted he would soon be out of a job, and pledged to crack down on leaks that Scaramucci suspected had come from Priebus.

But what may have ultimately doomed Priebus is the failure of congressional Republicans to repeal Obamacare.

Priebus, former chairman of the Republican National Committee during last year's presidential campaign, led the White House effort on Capitol Hill to get rid of Obamacare, and his fate was largely tied to that, a source close to the White House told NBC News.

"He failed," the source said.

The Republicans' inability to undo the Affordable Care Act — one of Trump's central campaign promises — was the "final straw for Priebus," another person close to the administration told NBC.

He resigned privately on Thursday, a source close to Priebus said.

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

"The president wanted to go in a different direction. I support him in that, and, like I said a couple weeks ago, I said the president has a right to change directions," Priebus said in an interview on CNN.

He added that there is no "ill will" between him and the president and that he even discussed possible replacements with Trump.

"I will continue to serve as a strong supporter of the president's agenda and policies," Priebus said in a statement shortly after the news broke Friday evening.

White House Press Secretary Sarah Huckabee Sanders told reporters Friday that the change had been under discussion for about two weeks.

After announcing Kelly, Trump tweeted thanks to his outgoing adviser "for his service and dedication to his country."

Presidents typically cycle through multiple chiefs of staff during their time in office. But Priebus' tenure of 190 days is one of the shortest in history. His resignation comes one week after White House Press Secretary Sean Spicer announced he will leave his post in August.

Spicer, like Priebus, worked at the R.N.C. during the 2016 presidential race. Scaramucci's appointment ultimately pushed Spicer to resign, a source said.

Sanders told reporters that Scaramucci's role and interview did not factor into Priebus' departure.

Kelly, who starts his new job on Monday, is a retired four-star Marine general whom Trump frequently credits for dramatically reducing the number of undocumented immigrants coming into the U.S. from Mexico. He called Kelly "a true star" in a tweet announcing his appointment as chief of staff.

"I have been fortunate to have served my country for more than 45 years - first as a Marine and then as the secretary of the Department of Homeland Security," Kelly said in a statement. "I am honored to be asked to serve as the Chief of Staff to the President of the United States."

Trump has placed a disciplined and respected military leader in one of the most important jobs in the White House. The chief of staff typically serves as the gatekeeper to the president and plays a large role in staffing and policy decisions in the West Wing.

But Kelly's appointment may also reflect the president's deep concerns over White House leaks. In an interview on NBC's "Meet The Press" in May, Kelly said leaking intelligence information is "darn close to treason."

Trump gushed over Kelly's record at an event in New York on Friday afternoon.

"I want to congratulate John Kelly, who has done an incredible job of Secretary of Homeland Security. Incredible. One of our real stars. Truly, one of our stars. John Kelly is one of our great stars," the president said.

Trump also tweeted at Priebus to thank him for his service.

I am pleased to inform you that I have just named General/Secretary John F Kelly as White House Chief of Staff. He is a Great American.... — Donald J. Trump (@realDonaldTrump) July 28, 2017

...and a Great Leader. John has also done a spectacular job at Homeland Security. He has been a true star of my Administration — Donald J. Trump (@realDonaldTrump) July 28, 2017