Sync between Visual Studio Team Services and GitHub

The Scenario

Both Visual Studio Team Services (VSTS) and Github are incredible services, both have strenghts and weakness, to choose between each one is a thought task.

VSTS is full featured, the features present in it is suitable for big enterprises. Private repositories is the default and in the free plan i can share my repositoris with just 5 people.

Github is simple, popular, all repositories is public by default.

 

The Problem

I want to use VSTS as much as possible to be familiar with the Microsoft ALM tools and services but i want to be able to easily share my code (not all the code) with the public. How can i sync my VSTS repositories with GitHub.

 

The Solution

I will explain how to configure your VSTS to automatically share your code from VSTS to GitHub.

Create (or choose a existing one) project in the VSTS.

Choose Git as version control, obviously 🙂

Clone the project to a local repository (optional).

Here, i am using the Visual Studio 2017 as my git client, you can use your favorite client or the CLI tool.

Right now, all my commits are being pushed to my VSTS account.

Create Github repository.

This is important: When you create the repository the master branch only will be created in the first commit, so create a Readme.md in the Github and make your first commit. Be aware to not create the Readme.md in the VSTS too, if so, you will have a merge conflict (two add file actions).

 

Repo created! After this, we want all the operations made in the VSTS repo automatically synced to Github. Lets configure VSTS to make it.

Create a new Build Definition in VSTS

 

tezt

In the next window, select the “Empty Template”. And configure your build as follows:

 

In the Process step, select and Build Agent, i choosed “Hosted VS2017”. Yes! Microsoft gives you a cloud full of agents ready to build, run tests, package, zip, copy, move, send reports of your projects, etc… whatelse you need.

Next, create two steps of type “Command Line”, with the folowing values:

Command Line 1

Tool: Git

Arguments: pull https://github.com/Ewerton/SyncTestGIT.git master –allow-unrelated-histories

Command Line 2

Tool: Git

Arguments: push https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@github.com/Ewerton/SyncTestGIT.git head:master

 

What is the xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx in the command? It is a “Personal Access Token” from Github, without it, anyone with the correct URL could push (and destroy) your beloved repository.

To get an Personal Access Token, look here 

Save your Build Definition and queue a new build, look at the results:

Lets do a F5 in the Github to see if every thing is working fine:

Conclusion

VSTS is way more featured than Github and the only feature i missed in VSTS (Public Repos) can be “workarounded” by this solution.