America’s longest war finally has an end in sight.

The US and the Taliban signed a peace deal Saturday that could see all American troops withdrawn from Afghanistan by May 2021, ending the conflict that began less than a month after the Sept. 11 terror attacks.

President George W. Bush launched the invasion in pursuit of 9/11 mastermind Osama bin Laden and al-Qaeda, who was sheltered in Afghanistan’s mountains by the Taliban, a militant Islamist group.

Over the years, as the US helped to train Afghanistan’s military and build up the civilian government, the fighting left 2,309 US troops dead and 20,660 injured ⁠— and cost more than $2 trillion.

“We’ve had tremendous success in Afghanistan in the killing of terrorists, but it’s time after all these years to bring our people back home,” President Trump said at a news conference Saturday in Washington.

At a ceremony in Doha, Qatar, US Special Representative Zalmay Khalilzad and Taliban leader Mullah Abdul Ghani Baradar signed the agreement, hashed out over months of negotiations.

“I call on all Afghans to honestly work for peace and gather around the table for peace negotiations,” Baradar said.

Key points of the deal:

• The US will draw down its forces to 8,600 troops from about 12,000, and close five bases, all by mid-July. Remaining troops will be withdrawn by May 2021.

• The Taliban will prevent militant groups from using Afghan soil to threaten the US and its allies.

• The Afghan government will release up to 5,000 Taliban prisoners in exchange for 1,000 Afghan security forces held by the Taliban.

• The US will work to remove Taliban members from sanctions.

• The Taliban and the Afghan government will start negotiating a comprehensive cease-fire in March.

“I could not be more supportive of this agreement,” said Rep. Max Rose (D-Staten Island), who served in Afghanistan from 2012-13. “I don’t want to see us send another generation of soldiers into a conflict that we should be winding down.”