Bety Martinez, from Peru, celebrates immigration reform outside the White House following President Obama's speech on Nov. 20. (Evelyn Hockstein/For The Washington Post)

On the evening of June 10, Rep. Mario Diaz-Balart (R-Fla.) and his staff had a party at his apartment, a block away from the Capitol. They opened a few beers and poured wine in celebration of their certainty that they had enough Republican votes to clear the way for debate on immigration reform in the House.

Given the overall resistance in the House GOP to doing something about undocumented immigrants, this was its own kind of triumph. “We were, frankly, happy and absolutely convinced that we were going to be able to finally fix this broken system,” Diaz-Balart said in an interview. “We had a couple of toasts, some high-fives and we were frankly convinced that we would move forward. And then all of a sudden, the unimaginable . . . we started getting alerts on our cellphones.”

June 10 was also primary election day in Virginia, and the unimaginable news was that then-House Majority Leader Eric Cantor (R-Va.) had lost his primary for reelection.

Cantor’s loss to Virginia Republican Dave Brat, an economist and vocal critic of immigration reform, turned the gathering into what Diaz-Balart called “a really sad affair,” and it helped doom any chance for an immigration overhaul in Congress this year.

That moment of frustration was one of many that came to define the debate about immigration over the past three years in Washington.

In a prime-time address on Thursday President Obama announced his plan to use his executive authority on immigration policy, protecting millions of illegal immigrants from deportation. (AP)

For pro-immigration-reform Democrats, the Cantor loss was a blow, too, but their unease had turned to a boil months earlier.

On March 13, the Congressional Hispanic Caucus was about to pass a resolution condemning President Obama for his administration’s deportation practices .

Their meeting was scheduled for 11:30 a.m, and about 30 minutes before it began, a few of their phones started buzzing with calls from the White House. The plea was simple: Don’t vote on the resolution until you’ve had a chance to talk to the president.

At 5 p.m., Obama told three senior members of the group that he instituted a three-month review of his deportation policies, which would be conducted by Homeland Security Secretary Jeh Johnson.

After that review, he promised, he would find ways to make changes.

“That was a signal to us that he was beginning to reshape and refocus what powers he had,” said Rep. Luis V. Gutiérrez (D-Ill.), who attended the meeting, which set in motion events leading to Obama’s announcement Thursday that he would use his executive powers to protect roughly 4 million people from deportation.

Although the eight-month process has been beset by setbacks and interruptions — as the president first sought a compromise with House Republicans and then acceded to a handful of Senate Democrats’ pleas to postpone action until after the fall elections — the final decision was little in doubt.

President Obama will take executive action Thursday to offer temporary legal status to millions of undocumented immigrants.

It also shows a president who is sometimes cautious, slow to confrontation, and can be susceptible to political pressure brought to bear by both allies and opponents. But it also shows Obama is willing to forge ahead despite the headwinds.

“I know the politics of this issue are tough. But let me tell you why I have come to feel so strongly about it,” Obama said Thursday night. “Over the past few years, I have seen the determination of immigrant fathers who worked two or three jobs, without taking a dime from the government, and at risk at any moment of losing it all, just to build a better life for their kids.”

Determined to protect his political legacy and provide a safe haven for many of the more than 11 million men, women and children living illegally in the United States, Obama came to accept the idea that some kind of change was worth pursuing. Even if it meant alienating some of the very political leaders whose cooperation he would need during his final two years in office.

By Wednesday night, when he hosted 18 Democrats at the White House for a meal of rib-eye steak and artichoke puree — followed by hazelnut cake for desert — Obama was prepared and resolute, according to people in the room.

“He spoke without notes [and] never had to make reference or ask for clarification from advisers, legal or otherwise. It was clear that he knew what he was talking about,” said Rep. Xavier Becerra (D-Calif.), one of the dinner attendees. “This was someone who had dived deeply into the subject and knew what was coming.”

But getting there involved both public and private feuds with lawmakers, as the president and his aides sought to forge a broader and more permanent solution for the nation’s immigration troubles.

Even after Obama directed Johnson to devise a plan using executive authority, he waited to see whether House Speaker John A. Boehner (R-Ohio) and a handful of his colleagues could bring legislation to the floor that could be reconciled with the bill the Senate passed by a wide margin in June 2013.

But first, Cantor lost and then came a flood of unaccompanied migrant children across the U.S.-Mexico border.

Cantor’s loss was significant because he was perceived as a supporter of immigration reform, and it was anticipated that he would bring a bill to the floor once he was past his primary challenge. His opponent, Brat, made immigration the key line of attack against Cantor, and his success in the primary left Republicans completely spooked.

Both Cantor’s loss and the wave of unaccompanied minors flooding across the U.S.-Mexico border made a House vote seem far-fetched by mid-June. Still, neither Boehner nor the White House was ready to declare the bill dead. On June 24, Congress held an event in the Capitol Rotunda to mark the 50th anniversary of the signing of the 1964 Civil Rights Act. House Minority Leader Nancy Pelosi (D-Calif.) buttonholed Boehner to ask about the immigration bill’s status.

Pelosi subsequently described his reaction as, “Why are you bothering me? I told you we were going to do it,” according to her aides. That afternoon, Boehner privately told Obama he was still short of the votes to move a bill. Six days later, the president called the speaker from the Oval Office and informed him that he was headed to the Rose Garden to declare he would move ahead on his own.

Before making his remarks, Obama met privately with about 20 leading immigration advocates. Three months earlier, the president had chastised many of the same advocates at the White House after Janet Murguía, president of the National Council of La Raza, had referred to him as the “deporter-in-chief.”

Murguía, who drew the ire of Obama aides, was not invited to the Rose Garden ceremony. But in the private meeting with advocates, Obama insisted that he would no longer wait on Republicans. Though the president cautioned that he probably would not be able to address everything advocates were asking for, “he made it clear he was not playing small ball,” said one advocate who was in the room.

In his speech moments later, the president vowed to take action by summer’s end. And he used harsh language to describe Boehner and his colleagues, saying House Republicans had “proven again and again that they’re unwilling to stand up to the tea party in order to do what’s best for the country. And the worst part about it is, a bunch of them know better.”

Boehner was unprepared for the personal attack, according to individuals familiar with his reaction; it was another blow to already-tense relations between the two men. But for Obama, the time for negotiations had ended.

Administration lawyers and policymakers, including White House Counsel Neil Eggleston and domestic policy adviser Cecilia Muñoz, began meeting with advocates, law enforcement officials and legal scholars to determine the limits of presidential powers on immigration laws — and outside groups offered their briefs.

Still, it was the president’s Senate allies who began urging him to slow down as the end of the summer neared. Democrats embroiled in tight races such as Sens. Kay Hagan (N.C.) and Mark Pryor (Ark.) publicly called on him to not act unilaterally. And Sen. Angus King (I-Maine), who caucuses with Democrats and was not up for reelection, made a private case for delay that helped sway senior White House officials.

Obama’s aides asked Democratic Senatorial Campaign Committee officials whether they had asked voters if they were more likely to vote against the party that fall if the president took executive action on immigration. While the DSCC had not posed that question before, it handed over individual candidates’ polling that found it could hurt imperiled Senate incumbents.

In an interview Thursday, King said he raised a few objections with the White House, including that the move “may make it more difficult to achieve immigration reform” and that he had “grave constitutional concerns” about it. “Every time you do something like this, you are transferring power from Congress to the president,” he said.

Right after Labor Day, top presidential advisers, including Valerie Jarrett and Muñoz, started telling labor leaders and immigration activists that the administration would delay acting until after the fall election, asking them not to abandon their get-out-the vote efforts in key states. On Sept. 6, the White House made it official.

Frustrated advocates, blindsided by the delay, reacted angrily and aimed their ire at the president. Obama was interrupted five times by immigration rights protesters during an election rally in Connecticut this month.

Even as activists railed against the White House in public, White House Chief of Staff Denis McDonough instructed his staff to move on immigration by the end of the year. While the Republicans’ decisive win on Nov. 4 represented a major political setback, the next morning, officials did not alter their plans.

Rep. Paul Ryan (R-Wis.), one of the Republicans who worked hardest on bringing up an immigration bill in the House, said Thursday that he and other leaders have asked the White House “to give us the time and space to do this right. Yet they proceeded anyway. . . . Their objective is political, not policy.”

But White House aides scoff at the idea that they are undercutting a future legislative solution, pointing out that House Republicans failed to support the bipartisan Senate bill and never put forward their own proposal for a vote. “I don’t think there will be a moment where Republicans will not say, ‘Just wait. Wait one more day,’ ” said a senior administration official, who spoke on the condition of anonymity to brief reporters on the president’s thinking.

Asked whether the administration considered waiting, the same official said: “If we were ever going to consider it, that went out the window the day after the election when Boehner had a press conference and one of your colleagues asked, ‘If the president agrees to wait, would you promise to bring [a bill] up?’ And he would not make that promise.”

Obama spent last week on a trip to the Asia-Pacific region, where he was preoccupied and did not have time to make a final decision on his immigration plans, aides said. When he returned Monday, they added, he decided to make the announcement this week instead of waiting until after negotiations over an extension of the government spending bill, which expires Dec. 11. Some Democrats had urged the president to wait to not inject immigration politics into the budget discussion.

On Monday, White House officials starting calling key Senate Democrats to inform them that the president would announce his immigration plan Thursday. Late Tuesday night, Gutiérrez got his own call; he later agreed to travel with Obama on Air Force One to Las Vegas on Friday.

“I want to be a validator of this decision. I want people to come forward,” he said. “My first responsibility is to embrace the 5 million that he’s offering [assistance to]. I’ve got to send a clear, unequivocal signal that this is the right thing to do. To tell them to sign up.”