If you are using Team Foundation Visual Studio Online (TFVS) as your primary source control and your Github repositories as a public, sharing hub, that everyone can view and download the source code of your NuGet libraries and you want to keep your Git repository up to date with your TFS source code. Then this post may help.
In this post, I will show you how to create a build on TFVS that help to sync a particular source code folder on TFVS to a repository on Github.com.
I. Generate Github Personal Token Key.
The personal token key on Github as an OAuth access key allow TFVS download and submit your source code to Github without your username and password.
You can generate a new token key here by login to Github -> Account Setting -> Personal access tokens under Developer Setting. The permission of this token should be in repo scope. That allow to download the source code from Github and commit the changes back to the repository.
After generated a token your token will look like as below. Note down this token as you won’t be able to view it again after closed this page.
The URL format that will be used to pull and commit the source code to Github look like this https://[token]@github.com/[yourUserName]/[yourRepositoryName].git
II. Create a TFVS build definition.
Now, go to your TFVS and create a new build definition with following tasks.
1. Get sources task.
This is default task of the build definition will help you to download specific source code folder from TFS repository.
2. Config the variables.
As the URL format above will be used in many tasks below.In case if you want to changes your token key or repository name you need to go through all the tasks and changes one by one.
To make the life easier, I will create 2 variables in the Variable tab of the build definition as below.
- githubtoken with value is the token key had been generated in step 1.
- GitRepo with value is the name of the Github Repository that you want to sync the source code to.
So following is my new URL format with token and git repository now are the name of variables. Please remember to replace by username (baoduy) with your account.
3. The Git clone Task.
Now go back to the Tasks tab and Add new Command Line task and config the parameters as below. This task will help to clone a Github repository to the $(Build.SourcesDirectory)\GitRepo location.
- Display Name: Run git clone
- Tool: git
- Arguments: clone https://$(githubtoken)@github.com/baoduy/$(GitRepo).git
4. The Delete files from GitRepo Task.
As the purpose of this build definition is sync the source code from TFVS to Github. So that, the current source code in Github can be considered is the out of date code and will be clean up before copying the latest source code from TFS folder.
Add a new Delete File task and config the parameters as below.
- Display Name: Delete files from GitRepo
- Source Folder: $(Build.SourcesDirectory)\GitRepo\$(GitRepo)
5. The Copy Files Task.
This task will copy the file from TFS source code folder to the Github repository folder. The parameters will be configured as below
- Display Name: Copy Files to: $(Build.SourcesDirectory)\GitRepo\$(GitRepo)
- Source Folder: $(Build.SourcesDirectory)
- Target Folder: $(Build.SourcesDirectory)\GitRepo\$(GitRepo)
6. The Run git add Task.
This command will help to detect all changed files in the Github repository and prepare the changeset for the upcoming commit statement.
- Display Name: Run git add
- Tool: git
- Arguments: add -A .
7. The Run git commits Task.
This task will execute the commit command on the local Github repository.
- Display Name: Run git commit
- Tool: git
- Arguments: commit -m”Sync from TFVS”
8. The Run git push Task.
This is the final task to upload the changeset to the remote Github repository.
- Display Name: Run git push
- Tool: git
- Arguments: push https://$(githubtoken)@github.com/baoduy/$(GitRepo).git –force
After done the task’s configuration your build definition will look like this:
Now save and queue your build. The results of the run should be as following screen shots.
1. The build ran successfully.
2. The source code had been synced to the Github Repository.
III. Trigger a new build from the Release process.
So example, you have a Release process that uploading your binaries to Nuget and when the upload task is done you want to execute the above build definition to sync your code to Github automatically.
There is an extension named Queue Build(s) Task that provides a nice feature for your release process that able to launch a new build of the build definition. You can install this extention and add 1 more task for your Release process as the configuration below:
If you want to clone the build definition for the other Github repository. Only 2 setting had been highlight with red color need to be changes the all tasks configuration will be work without any amenment required.
Hope this help.