Image for post
Image for post
Original photo by Pero Kalimero on Unsplash

How to use Visual Studio Codespaces with Azure DevOps and SSH authentication

While offering a great out-of-the-box experience with repos in GitHub, setting up a cloud based dev environment with Visual Studio Codespaces and repos located in Azure DevOps requires some extra steps.

Philipp Bauknecht
Jul 9, 2020 · 4 min read

Intro

I 💗 this service as I can work from any machine just with Visual Studio Code installed or even through the browser and connect to different development environments in the cloud!

All guides I’ve found so far assume the one uses GitHub as a repo host. While there is nothing wrong with GitHub I mostly use Azure DevOps for my work projects to host my repositories. It turns out that entering a DevOps git url when prompted for a repository during the creation process of the Codespace it fails as the extension is unable to authenticate.

It took my some trials to find the shortest way of connecting to Azure DevOps inside a Codespace. So here is a guide to save you some time.

Prerequisites

Create a Codespace

Image for post
Image for post

Now jump over to Codespaces and sign in:

Image for post
Image for post

This will open a browser window with a log in page. Once you have successfully logged in, go back to Code a select create new Codespace. Pick the default settings, select the subscription you want to use (if this fails you need to install the Azure Tools extension and sign in first).

Next is the step to provide a repository. This will not work with a Azure DevOps URL so leave this input blank and continue. Finally pick a name for your Codespace and wait for the Codespace to be prepared.

Once the Codespace is ready click on “Connect”.

Image for post
Image for post
Process of setting up a Codespace

Connect Codespace to a repo using SSH

git init

Now let’s add the SSH url of the Azure DevOps repository we want to connect as a git remote to the workspace using the terminal:

git remote add origin git@ssh.dev.azure.com

You can copy the SSH url from the repository’s website like this:

Image for post
Image for post

Since we don’t want to cache secrets on our dev environment and also don’t want to enter a password for each git operation let’s use SSH. Therefore we need to create SSH keys (Learn more here: https://docs.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate) also using the Terminal:

ssh-keygen -t rsa -b 4096

Stick with the default file and also skip the passphrase. So use this key we need to add the public key’s data to our user profile in Azure DevOps. So open the key file in Code:

code /home/codespace/.ssh/id_rsa.pub

Copy everything without the trailing whitespace and linebreaks and add the key in DevOps (there is a “Manage SSH Keys” link on the same page than the SSH url). Pick any description that helps you identify the key later.

Image for post
Image for post

To test if this works we need to do a git operation like fetch in the Terminal. Since it’s the first time we call this endpoint, we to add the endpoint to the list of known hosts by typing yes:

Image for post
Image for post

Now that the connection is established we can perform any operation without entering any credentials. Now we are ready to code!

Summary

medialesson

We help our customers design, architect, develop and…

Philipp Bauknecht

Written by

CEO @ medialesson. Microsoft Regional Director & MVP Windows Development. Father of identical twins. Passionate about great User Interfaces, NYC & Steaks

medialesson

We help our customers design, architect, develop and operate modern, intelligent, beautiful and usable apps on any platform powered by the Cloud, IoT and AI.

Philipp Bauknecht

Written by

CEO @ medialesson. Microsoft Regional Director & MVP Windows Development. Father of identical twins. Passionate about great User Interfaces, NYC & Steaks

medialesson

We help our customers design, architect, develop and operate modern, intelligent, beautiful and usable apps on any platform powered by the Cloud, IoT and AI.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store