So you are utilizing Visual Studio Team Services as your build server for direct integration into GitHub, perhaps entirely or perhaps just for your public repo’s in GitHub (while your private repo’s are in VSTS). In either case, in a standard continuous delivery fashion your going to want to publish a tag to your GitHub repo whenever you have a successful packaging of your software.
Lucky for us, the GitHub API makes this a pretty trivial web request, so all we need to do is wire it up inside a custom VSTS Build Task. If our not sure of the basics of custom build tasks, check out “Building Custom VSTS Tasks“.
We are going to simply create a standard GitHub reference for our tag (lightweight), utilizing this API here:
https://developer.github.com/v3/git/refs/#create-a-reference
If you just want the task code, find it and others here:
https://github.com/travisgosselin/vsts-tasks
And then you’ll have a task that looks like this at the end:
– Tag: This will the actual name of the reference you want to create. We will default it to the existing build number, assuming that build number is our continuous delivery number we want to tag $(Build.BuildNumber). But you could manually enter a tag or some other dynamic combination here.
– Repository Name: The name of the repository that the tag will be added too (you’ll specify account below under “owner”).
– Commit: This is the commit hash that the reference should be applied too. Of course you’ll likely want that applied to the latest commit in which this build definition is consuming, in which case stick with the default build variable: $(Build.SourceVersion)
– Owner: This is the repository owner. It can also be an organization (since that is the owner in some cases). For example, my own repo, my account (username) is “travisgosselin” from http://github.com/travisgosselin
– OAuth Token: You must supply credentials which have access to add tag references to this repository. Utilizing an account with the necessary permissions, create a personal access token and added it in here. If your not sure how to create a Personal Access Token then check out this article. Also remember if you don’t want to leave your access token out in the open for others on your account to steal, you can add this token under the “Variables” section of the build utilizing the encryption option and then specify the variable here instead.
Below is the JSON task definition as an example. You may want to add your own custom defaults for owner or token if you plan to utilize it personally. I have a copy of this task utilized in my organization that defaults all of the “Advanced” settings so you can drop the task in without any modifications (other than repo) and your good to go.
The script to generate the GitHub Tag is relatively simple, take a peak:
Any improvements on error handling and scripting is much appreciated!
Also, I’m always on the lookout for some other repo’s of VSTS Build Tasks that might add some great utility. Please leave em’ in the comments if you know of any!