WASHINGTON -- President Donald Trump signed a two-year budget deal Friday morning, along with a stopgap spending bill to end a brief government shutdown.

The measure narrowly passed in the House hours earlier, in a 240-186 vote, overcoming opposition from both conservative Republicans opposed to boosting federal spending and Democrats worried its passage would diminish their leverage in the coming debate over immigration.

After months of funding the government through short-term patches, a coalition of centrist House Republicans and Democrats combined to end the year's second government shutdown and resolve a fiscal fight that had spilled over from 2017. The Senate had passed the spending package hours earlier in a 71-28 vote.

The president signed the bill about 8:30 a.m.

"Our Military will now be stronger than ever before," he wrote on Twitter. "We love and need our Military and gave them everything -- and more. First time this has happened in a long time. Also means JOBS, JOBS, JOBS!"

The budget deal's passage effectively ends one of the most high-stakes fights in Washington, which devolved last month into a three-day partial government shutdown and a second, shorter one Friday. By ensuring stable government funding, the budget agreement removes the threat of a shutdown from Democrats' arsenal, disappointing those who had wanted the minority party to wield it in the coming fight over immigration.

Senate Majority Leader Mitch McConnell (R., Ky.) took the first steps Friday morning to begin debating immigration legislation next week. Lawmakers have been wrestling with the fate of young immigrants known as Dreamers, who were brought to the U.S. illegally by their parents, since Mr. Trump in September ended a program shielding them from deportation, but gave Congress until March 5 to hammer out a replacement.

The bipartisan support for the budget deal is unlikely to carry into the immigration debate, where each party's base has staked out uncompromising ground. But on Thursday, three of the four top congressional leaders took note of a rare moment of bipartisanship since Mr. Trump took office.

This week's long-term budget deal "is a strong signal that we can break the gridlock that has overwhelmed this body and work together for the good of the country," Senate Minority Leader Chuck Schumer (D., N.Y.) said on the Senate floor Thursday.

The two-year budget deal will boost federal spending for both the military and domestic programs by almost $300 billion over two years, in addition to nearly $90 billion in disaster aid for areas recovering from last year's destructive storms. It will also suspend the government's borrowing limit through March 1, 2019.

After signing the bill on Friday morning, President Trump lamented the increase in domestic spending, saying it illustrated the need for Republican gains in the November midterm elections.

"Costs on non-military lines will never come down if we do not elect more Republicans in the 2018 Election, and beyond," he wrote. "This Bill is a BIG VICTORY for our Military, but much waste in order to get Dem votes."

The package includes a short-term spending measure that will keep the government, once reopened, running through March 23. That will give lawmakers enough time to translate the deal's overall funding levels into the detailed spending bill that will fund the government through September.

The action by Congress in the wee hours of Friday morning meant that, technically, the government had run out of funds. However, since the House acted before business hours, few offices or services were expected to be shutdown.

The Senate had passed the spending package hours earlier, after missing a midnight deadline to prevent a shutdown. Lawmakers from both parties pinned the sudden shutdown squarely on Sen. Rand Paul (R., Ky.) whose objections stalled all action in the Senate for hours on Thursday night.

"The senator from Kentucky by objecting to the unanimous consent requests will effectively shut down the federal government, for no real reason," said Senate Majority Whip John Cornyn (R., Texas).

Mr. Paul said he wanted more time to debate the bill's impact on the federal budget deficit.

"I can't just look the other way because my party is now complicit in the deficits," he said on the Senate floor Thursday night. "It's a bipartisan compromise in the wrong direction."

In the House, the budget deal had opened a different rift among Democrats, many of whom had hoped to use their leverage on spending bills to secure legal protections for Dreamers. Spending bills need 60 votes to clear the Senate, where Republicans hold 51 seats. They often require bipartisan support in the House as well.

"It does diminish our leverage, absolutely," Rep. Juan Vargas (D., Calif.) said of passing the budget deal before the vote, which he planned to oppose. "I don't see how it doesn't."

In an acknowledgment of many Democrats' anger, House Minority Leader Nancy Pelosi (D., Calif.) spoke for a record-breaking eight hours on the House floor on Wednesday, advocating for the young illegal immigrants known as Dreamers. She received a standing ovation when she walked into a caucus dinner Wednesday night, but some Democratic lawmakers said Mrs. Pelosi's speech may have misleadingly broadcast that their sole ambition is to resolve the immigration debate.

"It may have once again reinforced with the American public that that is our singular priority, but I think that is not really the case," Rep. Marcia Fudge (D., Ohio) said of Mrs. Pelosi's speech. Ms. Fudge said she planned to vote for the budget deal, which she said contained "almost everything that I wanted."

Some Democrats this week began more openly questioning the wisdom of their leaders' attempts to link support for the spending bills to securing legal protections for the Dreamers.

"We weren't going to get DACA through the budget process no matter what, so yeah we can scream and yell and Nancy can get on the floor for eight hours, and I congratulate her for doing that," said Rep. John Yarmuth (D., Ky.), referring to the Deferred Action for Childhood Arrivals program, "but it still wasn't going to get us a resolution."

In the Senate, Mr. McConnell has promised an open debate over the fate of the young illegal immigrants. Mrs. Pelosi said she wanted the same commitment from House Speaker Paul Ryan (R., Wis.), who has said he would only bring a bill to the House floor that had Mr. Trump's support. But on Thursday, Mr. Ryan said he was confident they could find a bipartisan bill that the president would back.

The spending bill also faced opposition from many conservatives in the House, who objected to its higher spending levels and suspension of the debt ceiling. On Wednesday night, the House Freedom Caucus, a group of roughly three dozen conservative House Republicans, said they would oppose it.

"We're spending more money than we ever spent in the Obama era," said Rep. Jeff Duncan (R., S.C.), a member of the Freedom Caucus, who said he planned to vote against the deal.

Write to Kristina Peterson at kristina.peterson@wsj.com and Natalie Andrews at Natalie.Andrews@wsj.com