Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tagging a TFS Git repository during a release

I am setting our build/release environment using TFS 2017.

I set up the Build to run automatically after each commit, and when we are ready to release a version of our application, a Release is manually created, and then deployed to various environments.

We would like to tag released versions in our git repository in order to easily know which git revision correspond to a binary. The "Label Source" setting in the Build definition allows to tag a git revision at build time, but since we build on every commit that would generate a lot of tags which we don't care about (as they are not deployed anywhere until a release is made).

How can TFS be setup so that when a Release is created (or possibly, deployed to an environment), a tag is created on the corresponding commit in our Git repository?

like image 606
ndeslandes Avatar asked Mar 17 '17 14:03

ndeslandes


3 Answers

You can add tags by using REST API:

POST https://{instance}/DefaultCollection/_apis/git/repositories/{repository}/refs?api-version={version}

[
  {
    "name": {string},
    "oldObjectId": {string}, (The current commit id the ref is at. 0000000000000000000000000000000000000000 when creating a new ref.)
    "newObjectId": {string}
  }
]

Calling Rest API through PowerShell:

$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

More information, you can refer to Calling VSTS APIs with PowerShell (apply to tfs 2017)

like image 108
starian chen-MSFT Avatar answered Oct 28 '22 19:10

starian chen-MSFT


I have built a vsts extension that does exactly this: https://marketplace.visualstudio.com/items?itemName=jabbera.git-tag-on-release-task

like image 43
Mike Barry Avatar answered Oct 28 '22 19:10

Mike Barry


You can add a command line task in your release definition and use git command git tag -a vx.x $(Build.SourceVersion) -m "xxx" to add a tag, check the screenshot below:

enter image description here

=========================================================================

Update:

In order to avoid specifying the working folder where the git reop checked out, you can publish the $(build.sourcesdirectory) to drop folder in build definition, then in release definition, you can simply select the drop folder directory. Check the screenshots below:

enter image description here

enter image description here

like image 44
Cece Dong - MSFT Avatar answered Oct 28 '22 18:10

Cece Dong - MSFT