Congress managed at the last minute on Friday night to avert a partial shuttering of the Department of Homeland Security, passing a one-week funding measure for the agency. President Obama signed it shortly before the midnight deadline.

The deal came together after a whirlwind day of negotiations in which the House Republican leadership suffered a humiliating defeat when its 20-day funding bill was rejected. The arrangement is expected to prolong talks about longer-term DHS funding until at least early next week.

After the House bill went down, the Senate sought to pull DHS back from the brink by swiftly passing the one-week bill by a voice vote. The House followed suit shortly thereafter, voting 357 to 60 in favor of it.

Earlier in the day, the House collapsed in failure when a last-ditch attempt to fund the agency for an additional three weeks died at the hands of most Democrats and dozens of Republicans who voted against it. The defeat was a major blow to Speaker John A. Boehner (R-Ohio), whose struggles to get unruly members to fall in line have continued in the new Congress. More broadly, it was an early black eye for the unified Republican majority that had vowed to govern effectively.

All but a dozen Democrats rejected the measure, as did 52 Republicans, in a tense procedure that stretched for more than 40 minutes. Democrats have demanded a long-term funding bill that does not go after Obama’s executive actions on immigration. Republicans want to use the DHS debate to fight Obama on immigration.

Late Friday night, Republicans and Democrats sent conflicting signals about what will come next. House Minority Leader Nancy Pelosi (D-Calif.) sent a note to Democratic colleagues indicating that by voting on the seven-day bill, they would ­“assure that we will vote for full funding next week.”

But Michael Steel, Boehner’s spokesman, said, “We have made no such promise.”

Boehner hoped to give lawmakers more time to break the House-Senate impasse with his 20-day bill, which the Senate signaled it would have passed if the House did. But his plans were spoiled once again, mostly by a faction of rogue conservatives at odds with his strategy.

“Our leadership set the stage for this,” complained Rep. John Fleming (R-La.), who voted against the measure and argued that last year’s debate over funding the entire federal government was the time and place to do battle with the president. “That’s where we had the best chance and opportunity.”

In November, Obama announced the executive actions granting temporary relief from deportation for more than 4 million immigrants in the country illegally. Republicans accused him of overstepping his legal authority.

House Democrats rallied against the 20-day bill, arguing that a delay would only put off the inevitable: another 11th-hour standoff on March 19 and pressure from them to pass a “clean” bill with no immigration provisions.

“The bullet must get bit by Boehner,” said Rep. Raúl Grijalva (D-Ariz.), who represents a long stretch of the nation’s border with Mexico and who voted no. “It either gets bit tonight . . . or it gets bit in three weeks.”

House Speaker John A. Boehner of Ohio responds to reporters’ questions about the problems in passing the homeland security budget. (J. Scott Applewhite/AP)

In a statement, Senate Minority Leader Harry M. Reid (D-Nev.) called on House Republicans to take up a bill the Senate passed Friday morning that would fund DHS through September and would not touch Obama’s immigration directives.

“Now is the time to drop the partisan political games and come together to avoid a Homeland Security shutdown for the good of our country,” Reid said.

The House has so far resisted that bill. It passed its own measure weeks ago that would fund DHS for the same period and that would also undo Obama’s immigration actions.

The House passed a measure along party lines Friday afternoon to go to conference with the Senate to hash out the differ­ences between their long-term bills. Majority Leader Mitch McConnell (R-Ky.) later announced a motion to agree. But Senate Democrats, who insist on a clean bill, are expected to block plans for a conference on Monday.

It is at that point that Democrats are hopeful Boehner will reverse course and clear the way for the more lasting bill passed by the Senate.

As it braced for a potential shutdown, DHS issued a 46-page document titled “Procedures Relating to a Lapse in Appropriations.” In attempts to pressure Republicans to pass a clean long-term funding bill, Democrats have routinely invoked the threat of the Islamic State and other dangers the United States is confronting.

Signs emerged early in the day that Boehner was having trouble getting the votes he needed for his bill. He summoned his top lieutenants to a meeting in his office Friday afternoon amid nervousness about the bill.

After the vote on the 20-day bill, some moderate Republicans and some Democrats expressed frustration with the actions of their hard-line conservative colleagues.

Rep. Bill Pascrell Jr. (D-N.J.) said the fact that the Conservative Political Action Conference was simultaneously underway in suburban Maryland fueled the conservative mutiny.

“They’re going to go up and pound their chest,” he said. “That’s what this is about. Safety second, bravado first.”

The final margin was also decided by more than a dozen Republicans whose support the leadership can usually count on, including four Virginia Republicans who were close allies of former majority leader Eric Cantor. Cantor stunningly lost his 2014 primary to now-Rep. Dave Brat, who ran to Cantor’s right on immigration. Brat voted no on the 20-day bill.

Firm Democratic resistance complicated matters. Echoing Reid, House Minority Whip Steny H. Hoyer (D-Md.) called on Boehner to take up the Senate bill after his stopgap bill was rejected.

“Surely there are 30 Republicans who will vote to fund Department of Homeland Security. I’m confident of that,” he said.

While Democrats were united against the 20-day bill, they overwhelmingly supported the one-week stopgap. Only five of 179 Democrats who cast votes rejected it.

But Republicans couldn’t seem to please everyone in their ranks, no matter what. Fifty-five of them voted against the one-week bill.

Rep. Steve Womack (R-Ark.), who voted for both bills, called Friday’s session a “significant emotional event” for House Republicans.

Womack, as many others, didn’t see any easy way out.

“Everybody who hasn’t hit ‘Miller Time’ yet knows that we probably were going to be in this spot in three weeks anyway,” he said.

Paul Kane contributed to this report.