Edward Devenney, who had been stationed at Faslane Naval Base on the Clyde, was a communications engineer on nuclear sub HMS Vigilant when he rang the Russian Embassy.
Devenney had been in the Royal Navy for 10 years and had hopes of becoming a chief petty officer and then a commissioned officer. But at the time of the security breach, Devenney, 30, said he had become disillusioned with the Royal Navy because his promotion hopes had been dashed by defence cuts.
He was drinking heavily, had bouts of depression and had just been cleared of a rape charge, the Old Bailey heard, when he attempted to pass on classified material. Devenney contacted the Russian Embassy offering information and later met two men he thought were Russian agents in London.
He had offered to give the spies details of the movements of HMS Vigilant, which included its plans to sail to Faslane and then to the east coast of America for nuclear testing.
He also took pictures of a safe where encrypted material was stored, in a room which he had security clearance to enter.
In January, three days before the secret meeting, he was warned by bosses he would be discharged if his behaviour did not improve. In an exchange of text messages, Devenney had told one of the fake spies: "I am disillusioned by my employers and I feel let down by them. Think we can help each other."
He also texted: "I am in the Royal Navy and I am a bit p***ed off with them at the moment."
Devenney, however, did not meet any Russian spies as intended, but covert British intelligence officers who recorded their meeting, during which Devenney said he was fed up with the navy and did not want payment.
Mark Dennis, QC, prosecuting, said: "It was only through the vigilance of MI5 officers that national security was not harmed."
Mr Dennis added: "The potential damage could have been considerable and could have harmed the safety and security of the United Kingdom."
Devenney, originally from Northern Ireland, has pleaded guilty to breaching the Official Secrets Act and misconduct in public office.
Court One at the Old Bailey went into a secret session half-way through yesterday's hearing to discuss the impact of his actions on national security.
Lord Carlile, defending, read a letter to the court from Devenney which said: "I have brought great shame to my family, loved ones and the submarine service.
"I accept the consequences of my actions and I'm truly sorry.
"Mostly I would like to apologise for the shame I brought on the Royal Navy."
Devenney admitted collecting information for a purpose prejudicial to the safety or interests of the state between November 18 last year and March 7 this year.