You’re welcome, America. Photo: Andrew Harrer/Bloomberg via Getty Images

Congress passed a sweeping, bipartisan budget deal early on Friday morning — but only after a bit of unnecessary drama led to a five-hour shutdown, the second this year.

After keeping the government open through a series of stopgap bills since last summer, with a three-day shutdown last month, on Wednesday Senate leaders Mitch McConnell and Chuck Schumer finally made a breakthrough: a budget deal that included wins for both parties. The spending caps Congress enacted in 2011 would be raised by $296 billion through 2019. Republicans could celebrate making the Pentagon’s already tremendous budget even bigger, with a defense-spending hike of $160 billion. Democrats would get a $128 billion increase for domestic programs, including funding for disaster relief, health programs, infrastructure, and college affordability. Even better, the deal would suspend the debt limit until March 2019, putting off another hairy deadline.

At midday on Thursday, the bill’s passage in the Senate seemed almost certain, though getting it out of the House before the midnight shutdown deadline was more iffy. But overall, the odds of the government staying open looked good.

Then Senator Rand Paul decided C-SPAN could use a ratings boost. With hours to go before the deadline, Paul demanded a vote on an amendment to keep the budget caps in place, threatening to take advantage of rules that let individual senators slow down proceedings.

While most Republicans have decided they don’t care about their fiscal ideology now that Barack Obama isn’t president, Paul objected to the bill’s massive spending increases. He wanted a vote to show how his colleagues have gone back on their vow to rein in federal spending, but GOP leaders refused to allow it, saying if they gave in to his demands, other senators might follow suit.

Paul made good on his threat speaking on the floor for more than an hour as fellow senators of both parties grew increasingly annoyed. Republican Senator John Cornyn called the move “irresponsible,” while Democratic Senator Brian Schatz noted that Paul wasn’t that concerned about fiscal discipline a few weeks ago.

Rand Paul voted for a tax bill that blew a $1.5 trillion hole in the budget. Now he is shutting the government down for three hours because of the debt. The chance to demonstrate fiscal discipline was on the tax vote. Delaying a vote isn’t a profile in courage, it’s a cleanup. — Brian Schatz (@brianschatz) February 9, 2018

“The reason I’m here tonight is to put people on the spot,” Paul said on the Senate floor. “I want people to feel uncomfortable. I want them to have to answer people at home who said, ‘How come you were against President Obama’s deficits and then how come you’re for Republican deficits?’”

Paul succeeded. Shortly after 11 p.m., the Senate recessed until 12:01 a.m. without passing the spending bill, meaning the government would officially shut down. Senator Ted Cruz, who knows a thing or two about shutdowns, happened to be presiding over the chamber at the time.

JUST IN: The Senate has recessed until 12:01am ET Friday. The government will shutdown https://t.co/f5P9Eoy9zk pic.twitter.com/Uyxl3jqvBS — NBC Nightly News with Lester Holt (@NBCNightlyNews) February 9, 2018

The time allowed for debate expired at 1 a.m., robbing Paul of his powers of obstruction. A short time later senators passed the legislation, 71 to 28. The bill then headed to the House, where lawmakers rushed to prevent the shutdown from stretching into the work day.

Passage in the House remained uncertain until the last few minutes of voting. Like Paul, the conservative House Freedom Caucus was upset about the bill’s massive spending increases, and formally opposed the bill. Without their three dozen or so votes, House Speaker Paul Ryan needed Democratic votes to get the bill out of the chamber.

House Minority Leader Nancy Pelosi had sent conflicting messages, praising the deal but saying she would vote against it, as it does not address the fate of the young undocumented immigrants facing deportation when the Deferred Action for Childhood Arrivals program is canceled next month. Initially she suggested she would not pressure other Democrats to join her, but at a closed-door meeting on Thursday night she urged her caucus to hold together. “We have a moment. They don’t have the votes. All of us should use our leverage. This is what we believe in,” she said, according to one Democratic lawmaker.

Ryan wooed Democrats by promising to bring an immigration bill to the floor, though it wasn’t quite what the caucus wanted.

Paul Ryan says they'll bring an immigration bill to the floor — "one the president will sign."



That wasn't really the issue. — Matt Fuller (@MEPFuller) February 8, 2018

Still, that was enough to get 73 House Democrats to yes, offsetting the 67 Republicans who voted no. Around 5:30 a.m., the bill passed in the lower chamber by a vote of 240 to 186. President Trump is expected to sign it.

Now Congress has finally solved its long effort to reach a budget agreement — and can start fighting about what to put in the actual appropriations bills, which they need to finish by March 23.

This story has been updated throughout.