David Smith, 58, tried to damage Britain's interests by passing on details of the embassy and its staff for cash payments, a judge found after the spy pleaded guilty.
Sentencing him, Mr Justice Wall said Smith had "put people at maximum risk".
UK police have described Smith's actions as "reckless and dangerous".
They have revealed how the spy was caught in a "remarkable" investigation involving two fake Russian agents working for the British security services.
Smith, formerly with the RAF, had been employed as a security guard at the embassy in Berlin for four years when, in 2020, he wrote to a Russian general passing on the names, addresses and phone numbers of colleagues, along with documents and information about security passes.
He had been collecting classified documents since 2018, motivated by his support for Russian President Vladimir Putin and hatred of the UK, the judge found.
The letter ended up in the hands of the Metropolitan Police and MI5, triggering a major investigation which also involved German law enforcement.
In August 2021, to gather additional evidence, Smith was told a man with the Russian name Dmitry was to visit the embassy, and he was asked to arrange for him to pass through security.
"Oh it's one of those," Smith reportedly responded. He seemed to believe Dmitry was a Russian "walk-in", intent on passing secrets to Britain.
In his sentencing remarks, the judge said Smith had done as much as he could to "ensure that Dmitry's identity could be revealed".
Had Dmitry's story been genuine "it is impossible to know what would have been the consequences to him", the judge said.
During the visit, Smith was told to copy apparently secret documents Dmitry had brought with him. These had been marked with a pink highlighter to identify them.
Dmitry also asked Smith to throw away some packaging from a mobile phone Sim card he had been given.
It was all a ruse. Dimitry was an MI5-trained "role player" and "Smith fell for it", Cdr Richard Smith of the Metropolitan Police told the BBC.
The documents with the pink markings, which were not remotely secret, were later found at Smith's home, along with the Sim card packaging.
Investigators had also placed a covert camera in the room where Smith worked, monitoring CCTV images.
This captured him looking through video of Dmitry's visit, and taking pictures of the fake visitor.
He could be heard muttering to himself that if Dmitry worked at the Russian embassy, staff there would know who he was.
"It's probably nothing but at least I've done it," he was recorded as saying.
Next, MI5 arranged for "Irina", another Russian speaker, to approach Smith at a tram stop posing as an officer of the GRU, Russian military intelligence.
Her story, also false, was that she had been sent from Moscow to ask him for help because someone was passing information to the British.
Again, Smith fell for it, arranging a meeting with Irina the following day.
After his arrest, his flat in Potsdam was searched, and among the items found was a piece of paper with the email address of the Russian embassy.
Video was seized of Smith walking around the British embassy, meticulously filming offices, safes, the insides of drawers and a whiteboard recording details of staff deployments.
Smith had also photographed communications equipment.
A letter to a Russian colonel offering to send a "book from the defence section" classified as "official-sensitive" was found on a storage device.
Police also found images of a letter from two secretaries of state to then Prime Minister Boris Johnson.
Cdr Smith said: "The activities he was conducting and the information he was seeking to gain did raise a significant risk to UK interests and individuals."
A consular official said that as a result of Smith's actions, a security review had to be carried out for every member of embassy staff costing the taxpayer £825,000.
Smith admitted eight charges under the Official Secrets Act and was sentenced after a hearing to determine why he did it.
The spy was a keen military historian, whose Ukrainian wife had left Germany for her home country, because she did not like living there. Smith appeared to share this view, colleagues at the embassy told police. He had also told fake Russian intelligence officer Irina that Germany was dominated by Nazis.
In his evidence, Smith said he became depressed and was drinking heavily after his wife returned to Ukraine.
He was also angry at his employer, complaining he had to go to work every day during the pandemic when "everyone was sitting at home with full pay".
He admitted being interested in conspiracy theories, including those of David Icke and Alex Jones' Infowars, but denied being pro-Russian or having far right sympathies.
However, the prosecution highlighted his collection of Russian military memorabilia, as well as his past support for Russian-backed separatists in eastern Ukraine.
Inside his work locker was a cartoon of Mr Putin with his hands around the neck of former German chancellor Angela Merkel in Nazi uniform.
Cdr Smith said the spy was "motivated by anti-UK, pro-Russian views, he understood that the info he was collecting was damaging to British interests, and yet he shared it with the Russians anyway".
An MI5 officer gave evidence that Smith's spying coincided with the build-up of Russian forces before the war in Ukraine and the concerns about the treatment of the Russian opposition leader, Alexei Navalny.
The resulting risks could not "be sensibly described as theoretical", the MI5 staffer told the court, using the code name 2093.
Smith was arrested with 800 euros (£713) in cash, and police analysed his bank accounts which showed he was not withdrawing his salary, suggesting had another source of income. The amount of money he was paid may never be known.
Senior police officers would not say how much information Russia could have received from the spy.