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.