22

I have a repository in visual studio team services that I would like to keep synchronized with a github repository.

This allows me to do my main development in VSTS and when merging into master it will be synced to github and also allow others to contribute on github and when there Pull Requests are merged into master its synced to VSTS.

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

2 Answers2

15

First create a new build on VSTS that is using the repository that should be synced from VSTS:

VSTS Build

add two CMD tasks that will run some git commands. git pull remote enter image description here

where the last of the two needs a personal access token from Github.

In the images both CMD tasks uses the GIT tool and the following two commands:

pull https://github.com/s-innovations/MessageProcessor.ServiceFabric.git master

and

push https://$(githubpersonaltoken)@github.com/s-innovations/MessageProcessor.ServiceFabric.git head:master

Enable the CI option to trigger the build to run whenever something is commited to master.

Now the same can be done the other way, where a new build is made the same way but with the urls changed to target visual studio online repository.

push to vsts

Do note that when using personal tokens on vsts the authentication part of the url needs to be https://:token@ and on github its just https://token@.

push https://$(vstspersonaltoken)@sinnovations.visualstudio.com/DefaultCollection/S-Innovations%20MessageProcessor/_git/messageprocessor-service-fabric head:master

Update AUG 2017

They changed it at VSTS, such if the colon is present it will fail auth. The above description have been updated.

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283
  • 1
    Sorry something's a little unclear to me here. why are you doing a pull from Github and then pushing to Github again? Shouldn't it be pulling from VSTS and pushing to Github? – Jay Jul 24 '17 at 13:48
  • Some time ago i used this, but if I recall correct it was done due to some changes might be made on github, and to pull those before pushing. – Poul K. Sørensen Jul 25 '17 at 11:58
  • Hi Poul, many thanks for your response. Yes i get that now i know what's the reason for that step. but when i try to run these steps in my VSTS it keeps throwing this error: `fatal: Not a git repository (or any of the parent directories): .` on the pull step. not sure what to make of this... – Jay Jul 25 '17 at 12:32
  • You can make a tutorial for Azure DevOps Repos? It would be a great help – Ruben Dario Guarnizo Martinez Oct 08 '20 at 02:43
1

For anyone wanna sync all branches from Github to VSTS by using powershell

You need to create a repo in VSTS with the same name in Github first.

Add a PowerShell process as the following script. It should work with any account and repo.

git branch -r | findstr /v "\->" | ForEach-Object {$br=$_.TrimStart(); git branch --track $br.TrimStart("origin/") $br} $repoName = "$env:BUILD_REPOSITORY_NAME".split('/')[1] $repoUri = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI".Substring(8)+ "_git/$repoName" git remote add vsts "https://$env:SYSTEM_ACCESSTOKEN@$repoUri" git branch -r | findstr /v "\->" | ForEach-Object { $br=$_.TrimStart(" origin/"); git push -u vsts $br }

maxisam
  • 21,975
  • 9
  • 75
  • 84