Olympic ski champion Bode Miller has reached a settlement in his bitter, bicoastal custody battle with his ex-girlfriend over their 18-month-old son.

The deal was finalized late last week just before the two were scheduled to testify in a public Manhattan trial about who would be a better parent to Samuel Bode Miller McKenna.

Details of the agreement between Miller and former US Marine Sara McKenna are confidential, her attorney, Michael Stutman, said.

“Neither of us got exactly what we wanted, but Sam got what he needed and that’s what’s most important to me,” McKenna told The Post Monday.

A lawyer for Miller did not respond to a request for comment.

McKenna, who is now attending Columbia University on the GI Bill, first lost custody of her son in September 2013 after a Family Court referee ruled that she shouldn’t have left her native California, where Miller lives, while pregnant even though the couple had only a short-lived romance.

McKenna, 28, told The Post last year that dating the skier was “the worst mistake of my life.”

Miller’s lawyer, Barbara Schaffer had told the referee that “New York is an inconvenient forum,” for the millionaire ski racer, who at the time lived on a 100-foot yacht in San Diego.

Schaffer had accused McKenna, also a former firefighter, of moving to New York because the Empire State “does not have a presumption as California does of joint custody.”

The referee punted the case to California where a judge gave custody to Miller, who is now married to professional volleyball player Morgan Beck.

A Manhattan appeals court reversed that decision a few months later.

The six-time Olympic medalist was forced to bring his son to Family Court in Manhattan to hand him over to McKenna.

During the custody fight, the pair couldn’t even agree on what to call their son.

Miller, 37, wanted the boy to be named Nathaniel after his late brother, who was found dead in a van at age 29.

Nathaniel “is not an honorable name for my son,” McKenna had told The Post.