Bitterly admitting defeat, the Republican-controlled Congress sent legislation to President Barack Obama on Tuesday that funds the Department of Homeland Security without any of the immigration-related concessions they demanded for months.

“Sanity is prevailing,” said Rep. Peter King, R-N.Y., a former chairman of the Homeland Security Committee, before the House voted 257-167 in favor of the $40 billion spending bill, which Obama was expected to sign promptly.

All 182 Democrats present voted for the bill, while it received only 75 Republican “yes” votes.

The outcome averted a partial agency shutdown which would have begun Friday at midnight. It was a major victory for Obama and the Democrats, and a wholesale retreat for Republicans, who have spent months railing against an “unconstitutional overreach” by Obama in extending deportation stays and work permits to millions of immigrants in this country illegally.

In the end Republicans who’d tried to use the DHS spending bill to undo Obama’s actions had little to show but weeks of gridlock and chaotic spectacle on Capitol Hill in the wake of assuming full control of Congress in the November midterm elections. The turmoil brought the Homeland Security Department to within hours of a partial shutdown last Friday before Congress passed a one-week extension, and raised questions about Republicans’ ability to govern responsibly.

On Tuesday morning, addressing an uncharacteristically subdued gathering of House Republicans, Speaker John Boehner indicated he was out of options.

“I am as outraged and frustrated as you at the lawless and unconstitutional actions of this president,” Boehner told his caucus. “I believe this decision – considering where we are – is the right one for this team, and the right one for this country.”

“Our Republican colleagues in the Senate never found a way to win this fight,” he said, noting that the matter is now in the courts. A federal judge last month put Obama’s directives on hold, a ruling the White House is appealing.

Conservative lawmakers who humiliated Boehner last week by voting down a three-week spending bill he proposed did not speak up in the private meeting to dissent or ask questions, people present said.

Afterward, they said they were disappointed but had no more moves to make.

“I don’t know that there is one,” said Rep. Steve King, R-Iowa. “This is the signal of capitulation.”

The measure passed Tuesday funds the Homeland Security Department through the Sept. 30 end of the budget year. It pays for numerous priorities including Transportation Security agents, the Coast Guard, the Secret Service, a host of immigration-related functions and grants to local governments.

There have been suggestions that Boehner would face an insurrection by tea party-backed conservatives if he brought a “clean” DHS bill to the floor. But Boehner’s opponents seemed resigned, and there was little sign of a brewing coup.

Indeed, several Republicans said Tuesday that the outcome was inevitable. Many had campaigned for re-election last fall on promises to stop Obama on immigration, and cheered when Boehner promised to fight the president’s moves “tooth and nail.” Yet several acknowledged they never had a viable plan to do so, given Obama’s veto pen and Senate Democrats’ opposition.

The GOP strategy was especially risky given the Homeland Security Department’s anti-terrorism responsibilities, which gave Democrats an opening to accuse Republicans of putting national security at risk.

“We all knew how this was going to end,” said Rep. Charlie Dent, R-Pa. “If somebody wants to make an argument against those of us who are doing our duty and governing responsibly, they can feel free to have the argument. We are prepared to defend ourselves and I believe the Speaker will come out of this just fine.”

The drama unfolded as a lesson in the limits of divided government.

The fight was set up last fall, when Boehner and GOP leaders convinced House conservatives to wait until this year to try to overturn Obama on immigration, when the GOP would command control of the Senate and bigger majorities in the House.

Congress passed a full-year spending bill for the rest of the government, but kept the Homeland Security Department on a short leash to use its spending bill as the vehicle to oppose Obama.

Republicans predicted that the handful of Senate Democrats who’d voiced concerns about Obama’s immigration actions would join them. But the DHS spending bill the House passed in January was yanked to the right by conservatives, undoing not only Obama’s most recent executive actions but an earlier directive, from 2012, that extended protections to immigrants brought illegally to the country as kids.

That helped unify Democrats against it, and Senate rules did the rest. Republicans command only 54 votes in the chamber, not the 60 needed to advance most legislation, and Senate Democrats blocked the House bill repeatedly.

In the end, the House contingent that opposed Boehner had little to do but bemoan what had become a foregone conclusion. As the drama neared its conclusion Tuesday they offered a few final procedural moves – forcing the reading clerk to read part of the bill out loud, and offering a motion to table – but they had no hope of prevailing.

“I believe this is a sad day for America,” said Rep. Matt Salmon, R-Ariz., one of the hardliners. “If we’re not going to fight now, when are we going to fight?”