WASHINGTON — With government funding set to run out this weekend, congressional leaders agreed on a voluminous $1.3 trillion spending bill on Wednesday that would beef up domestic and military programs and keep the government open through September.

“Every bill takes compromise, and there was plenty here,” said Senator Chuck Schumer of New York, the Democratic leader, “but at the end of the day, we Democrats feel very good because so many of our priorities for the middle class were included.”

The House and Senate have until midnight Friday to pass the spending bill to avoid what would be the third government shutdown of the year. As part of the spending talks, congressional leaders worked in recent days to resolve disputes over issues like immigration, a wall on the southern border with Mexico, health care and a planned rail tunnel between New York and New Jersey that has drawn the ire of President Trump.

As the negotiations wrapped up on Capitol Hill, the president himself emerged as a potential obstacle, waffling on Wednesday about whether he could support the final package in part because it lacked sufficient funding for his wall. To salvage the agreement, Speaker Paul D. Ryan went to the White House residence to meet with Mr. Trump, Vice President Mike Pence and Marc Short, the White House legislative affairs chief, with Senator Mitch McConnell, the majority leader, joining by telephone.

A White House official said the group smoothed over differences over a number of items, including the wall and other border issues, as well as transportation and military funding.

Afterward, Sarah Huckabee Sanders, the White House press secretary, issued an upbeat statement making it clear that Mr. Trump had overcome his reservations, saying that he and the congressional leaders had “discussed their support for the bill.”

The president appeared to endorse the deal late Wednesday night, even as he vented frustration about the compromises that made it possible.

“Got $1.6 Billion to start Wall on Southern Border, rest will be forthcoming,” he wrote on Twitter. “Most importantly, got $700 Billion to rebuild our Military, $716 Billion next year...most ever. Had to waste money on Dem giveaways in order to take care of military pay increase and new equipment.”

Democrats were able to exert influence in the negotiations over the spending bill because votes from their party are needed to approve the legislation in the Senate and will most likely be needed in the House, as well.

The text of the spending bill, spanning 2,232 pages, was unveiled Wednesday night. To improve border security, the measure includes $1.6 billion for more than 90 miles of physical barriers along the border with Mexico, as well as related technology, according to congressional aides.

But there are strings attached to what can be built, and the funding is far short of the total Mr. Trump would ultimately need to build his promised “big, beautiful wall.” Some of the funding is for replacing existing barriers.

The spending bill does not resolve the uncertain fate of hundreds of thousands of young undocumented immigrants, known as Dreamers, who have been protected under an Obama-era program, Deferred Action for Childhood Arrivals, or DACA, that Mr. Trump has moved to end.

But the president squarely put the blame on Democrats on Wednesday, writing that they “refused to take care of DACA. Would have been so easy, but they just didn’t care. I had to fight for Military and start of Wall.”

Over the weekend, the White House offered to extend protections for DACA recipients for two and a half years in exchange for $25 billion for the border wall, according to congressional aides. Democrats countered by offering $25 billion in wall funding in exchange for a pathway to citizenship for a broader population of Dreamers, which the White House rejected.

An effort by some lawmakers to include in the spending bill a proposal to shore up insurance markets under the Affordable Care Act was unsuccessful, at least in part because of a dispute over abortion. But the bill includes a fix to the so-called grain glitch, a flaw in the sweeping tax overhaul passed last year that stood to hurt certain agricultural businesses.

Another sticking point in recent days was funding for a series of rail infrastructure projects in the New York City area known as the Gateway program, including a new tunnel under the Hudson River. Despite his New York roots, Mr. Trump zeroed in on Gateway and urged Republican leaders not to provide federal funds for it — an apparent rebuke to Mr. Schumer, whose caucus the president has repeatedly accused of obstructionism.

The spending bill does not include $900 million in funding for Gateway that had been included last year in House legislation. But according to a senior Senate Democratic aide, it includes hundreds of millions of dollars that could go toward the Gateway program, including funds that do not require the approval of Mr. Trump’s Transportation Department.

As November’s midterm elections loom, the legislation also includes $380 million for grants to states to improve their election infrastructure and bolster election security.

And although Congress has shown little appetite for passing significant gun control legislation in response to the mass shooting last month in Parkland, Fla., the spending bill includes a modest measure to improve reporting to the national background check system for gun purchases. It also includes a measure to provide grants to improve school safety.

Congress approved a broad two-year budget deal last month that paved the way for this week’s legislation. That deal set overall spending levels, raising strict limits on military and domestic spending by a total of about $140 billion this year. The spending bill this week allocates the allowed spending among a vast array of federal programs.

The bill is long overdue, coming almost halfway through the 2018 fiscal year, which began Oct. 1. Since then, Congress has needed five stopgap spending measures to keep the government open. By snapping that streak of short-term patches, lawmakers would provide a dose of stability to federal agencies that have been left in limbo as Congress lurched from one stopgap measure to the next.

Even with the spending bill unveiled, there is still some risk of a brief shutdown this weekend, as any one senator can stop the Senate from speeding up consideration of the bill to meet Friday’s deadline. Last month, Senator Rand Paul, Republican of Kentucky, did just that, causing an hourslong shutdown as he bemoaned the government’s mounting debt.

This time around, with lawmakers expected to vote on a gigantic spending bill with little time to digest its contents, Mr. Paul is unhappy yet again.

“It’s a rotten, terrible, no-good way to run your government,” he said Tuesday, adding, “Really, should we be looking in thousand-page bills with 24 hours to decide what’s in them?”

He was not the only lawmaker with such frustration.

“Whoever designed this process is not qualified to run a food truck,” said Senator John Kennedy, Republican of Louisiana. “It’s embarrassing. And as bad as it looks from the outside, you ought to see it from the inside.”

The approval of the spending bill would be another blow to those worried about the government’s ballooning debt, which has topped $21 trillion. That issue seemed of little concern on Capitol Hill as Mr. Trump and Republican lawmakers pushed for much more military funding, and Democrats demanded similar increases for domestic priorities. The spending bill will leave lawmakers from both parties with much to boast about, including billions of dollars for infrastructure and for fighting the opioid epidemic, as well as the biggest increase in military funding in years.

“This legislation fulfills our pledge to rebuild the United States military,” Mr. Ryan said after the bill was unveiled, while conceding, “No bill of this size is perfect.”

The spending spree comes on the heels of the Republicans’ tax overhaul, which was projected to add $1.5 trillion to federal budget deficits over a decade. The deficit is now expected to exceed $1 trillion in the 2019 fiscal year, according to the Committee for a Responsible Federal Budget, a fiscal watchdog group.

The spending bill gave the most conservative members of Congress little to celebrate. Representative Mark Meadows, Republican of North Carolina and the chairman of the House Freedom Caucus, expressed disappointment with the modest funding for fortifying the border.

“It is troubling when we get a tunnel and we don’t get a wall,” he said. “The last time I checked, the president didn’t make any promises about getting a tunnel in any of his campaign stops, at least not in North Carolina.”

A version of this article appears in print on , on Page A12 of the New York edition with the headline: Leaders in Congress Reach an Agreement To Spend $1.3 Trillion. Order Reprints | Today’s Paper | Subscribe