Trump teased a veto a day after the White House had said he would sign the legislation despite his misgivings. He raised the risk of a government shutdown only hours before the midnight Friday deadline. Then the president, who enjoys creating tension and drama, announced he would not veto the bill in an event carried on live television.

"We're very proud of many of the items that we've been able to get. We're very disappointed that in order to fund the military, we had to give up things where we consider in many cases them to be bad or them to be a waste of money. But that's the way unfortunately right now the system works," Trump said at the White House. He added that he "looked very seriously" at a veto, but his support for the military spending levels "overrode" his concerns about the bill.

Trump slammed the rushed process to pass the more than 2,200-page bill released only Wednesday. Standing near the pile of documents, the president said he was "disappointed" in the legislation and would "never sign another bill like this again."

The president said he approved the legislation to fund the government through September for national security reasons, as it authorizes a major increase in military spending that he supports. But he stressed that he did so reluctantly.

An "unhappy" President Donald Trump signed the $1.3 trillion spending bill into law Friday, his second about-face in 24 hours on the measure to keep the government open.

President Donald Trump speaks at an event to sign Congress' $1.3 trillion spending bill in the Diplomatic Room of the White House in Washington, March 23, 2018.

Trump highlighted the portions of the bill he supports: the nearly $80 billion increase in defense spending, a $1.6 billion boost to border security funding and a cash injection to fight the opioid epidemic. The border money will go mostly toward surveillance technology and fencing similar to structures that already exist on the southern border. The amount of funding came in far below what the president wanted.

In a tweet Friday morning, Trump said he was "considering a VETO" because the proposal did not extend protections for hundreds of thousands of young undocumented immigrants or fully fund his proposed border wall.

Trump tweet

The president's tweet threw more chaos into the process to keep the government running Friday, even after it looked like Trump's signature would be a sure thing. Vice President Mike Pence offered support for the bill Thursday. Office of Management and Budget Director Mick Mulvaney answered "yes" on Thursday when asked if Trump would sign the bill.

"Why? Because it funds his priorities," he told reporters.

The legislation, which both chambers of Congress passed with bipartisan support, would fund the government through the end of September. It would significantly boost military spending and increase funding for border security, infrastructure and efforts to fight the opioid epidemic.

It also includes measures meant to strengthen gun sale background checks and improve school safety.

When Trump threatened a veto, many lawmakers had already left Washington. Some are in Rochester, New York, for the funeral Friday of longtime Rep. Louise Slaughter, who died last week. The Senate had adjourned and was not expected to take up legislative work again until April 9.

While Trump cheered the military funding levels, he was irked by the level of border security funding. While Republicans and the president himself have said the bill funds Trump's "wall," the money goes to miles of new fencing similar to existing structures.

Trump, who has tried to end the Deferred Action for Childhood Arrivals program, reportedly wanted $25 billion in long-term wall funding in exchange for protections for the young immigrants. Democrats and some Republicans have sought to extend the DACA program. Trump reportedly did not want to give up to 1.8 million immigrants a pathway to citizenship as part of that deal, as Democrats wanted.

On Friday, Trump claimed Democrats do not want to extend the legal protections for the immigrants.

"I do want the Hispanic community to know and DACA recipients to know that Republicans are much more on your side than the Democrats, who are using you for their own purposes," he said.

Some notable Republicans who opposed the spending bill encouraged Trump to torpedo it on Friday after his veto threat. Earlier, House Freedom Caucus Vice Chairman Jim Jordan of Ohio told CNBC the veto threat "is great" and said he hoped the president followed through on it.

"I think he got a full understanding of how bad this legislation is. … This is not even close to what the American people elected us to do," the hardline House conservative said.

Jordan pushed back on GOP claims that the bill funded the border wall, asking, "Why do you think [House and Senate Minority Leaders] Nancy Pelosi and Chuck Schumer are smiling ear to ear?"

Earlier Friday, Sen. Bob Corker, R-Tenn., told Trump to "please" veto the legislation, calling the spending levels "grotesque."

Corker tweet

Sen. Rand Paul, R-Ky., whom Senate Majority Leader Mitch McConnell persuaded not to delay the Senate vote on the bill, also urged Trump to "veto this sad excuse for legislation."

Paul tweet

At least one Democrat — Rep. Ted Lieu of California — said he supported a veto. He criticized the rushed process by which Congress passed the legislation.

Lieu tweet

House Speaker Paul Ryan, R-Wis., had worked this week to win Trump's support for the plan. He described it as a win for the president because it boosts Defense Department funding, authorizes more money to fight the opioid crisis and puts more money toward border enforcement.

"This funds the wall, fixes the military, fights opioids and does the things that we said," Ryan said Thursday on Fox News' "Fox & Friends," a show Trump frequently watches and tweets about.

Ryan also went to the White House on Wednesday as Trump was unsure about whether to support the spending bill. After the president spoke with Ryan and McConnell, the White House issued a statement saying he backed the legislation.

Trump's tweet Friday morning caught Ryan by surprise, according to a GOP source who declined to be named. The speaker is in his home state of Wisconsin for the Easter recess, and spoke to the president by phone at about 9:30 a.m., a little more than a half hour after Trump's tweet.

Ryan emphasized "all the wins" in the bill, particularly military spending, and felt encouraged after the call, the source added.

— CNBC's Ylan Mui and Eamon Javers contributed to this report