Learn-a-holic Geek Notes

Human compiled Brainwork by Kornelije Sajler.

Configuring TeamCity 7 With Git Revision Short Hash, Uploading to Amazon S3 and Sending Email With Release Link and Developer Commit Details

Here is a short tutorial (with extra long title!) how to improve the JetBrains TeamCity Build Server for CI (Continuos Integration).

This is what wiill do to improve TeamCity 7:

  • Change the Build version number to include Git short hash (7 chars) as Revison e.g. 1.0.1.53e7986.
  • Upload file to Amazon S3.
  • Send email via Gmail with:
    • Latest downloadable link of release from Amazon S3.
    • Developer commit message details.

Note:

This is not blog post how to set up TeamCity, this is usually really straight forward thing to do. So in this blog post I assume that there is TeamCity project and at least one Build Configuration already created!

Setting Git Short Hash as Revision of TeamCity Build Number

  • In TeamCity go to desired named ”Build Configuration” in our case ”aeon”.
  • Click somewhere on right side on Edit Configuration Settings to enter aeon Configuration Steps.
  • In (1) General Settings set Build format number to something like 1.0.{0} and Save.
  • In (3) Build Steps click Add build step choose Powershell in listbox.
  • Set Name if you want like ”Get Build Number with short Git Hash”.
  • For Script choose ”Source code” from drop down box.
  • In Script Source add following Powershell code and click Save:
1
2
3
4
$BuildNumber = "%build.number%"
$Hash = "%build.vcs.number%"
$ShortHash = $Hash.substring(0,7)
Write-Host "##teamcity[buildNumber '$BuildNumber.$ShortHash']"
  • Script execution mode should not matter, mine is “Execute .ps1 script…”.
  • After save make sure that this Build Step is first!!!!
  • Use Reorder build steps link, if you have existing build setps.

Note:

When build starts by Agent, it will be 1.0.1 and at the end it will change to 1.0.1.53e7986!

Shell version (Linux or Mac)

  • All same as Powershell until (3) Build Steps.
  • In (3) Build Steps click Add build step choose Command line in listbox.
  • In Custom Script add following shell script and click Save:
1
2
3
4
BUILD_NUMBER=%build.number%
GIT_HASH=%build.vcs.number%
GIT_HASH_SHORT=${GIT_HASH:0:7}
echo "##teamcity[buildNumber '$BUILD_NUMBER.${GIT_HASH_SHORT}']"

Note:

I didn’t test shell version, it should work, but maybe some tweaks are necessary!

Powershell script for sending file to Amazon S3 and sending email via Gmail

  • Download and install AWS Tools for Windows Powershell.
  • With this SDK is really easy to upload file to Amazon S3.
  • Amazon S3 account is needed and also Access Key and Secret Key.
  • Assumes that Amazon S3 bucket name is ”aeon” and region is ”eu-west-1”.
  • This will produce https://s3-eu-west-1.amazonaws.com/aeon/.
  • The script is lousy written, it should be better, especially when it is needed to be reused for multiple Builded apps.
  • The thing needed to be entered will be inside “<something-to-do>”.
  • In my case the uploaded file is “.exe” file so it is harcoded in Amazon S3 URL.
  • Make sure that file name is not really crazy and there are no spaces in it (it is always easier this way)!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
$bucketName = 'aeon'
$hasCredentials = $false;
$filePath = '<absolute-path-to-folder-where-file-for-upload-is>'


function GetLastGitLog {
    $sourcePath = '<absolute-path-to-where-teamcity-pulls-repo>'
    $isSourcePathExist = Test-Path $sourcePath

    if (!$isSourcePathExist) {
        "source folder ($sourcePath) for file is not existent! Exiting..."
        exit
    }

    Set-Location $sourcePath

    git log -n 1

}

function MessageBody($version) {
    $url = "https://s3-eu-west-1.amazonaws.com/{0}/{1}.exe" -f $bucketName, $version

    $commitMessage = GetLastGitLog | Out-String

    "<h2>My Fabulos Product - New Version</h2>

     <p><strong>Version:</strong> {0}</p>
     <p><strong>Download Link:</strong> <a href='{1}'>{2}.exe</a></p>

     <p></p>
     <p><strong>Developer Commit Details:</strong></p>
     <pre>{3}</pre>

     <p></p>
     <p>Regards,</p>
     <p>My Fabulous Team City Build Server (example.com)</p>
     " -f $version, $url, $version, $commitMessage
}

function SendEmail($binaryName) {
    $version = $binaryName.TrimEnd('.exe')
    $mailTo = "<first-email>,<second-email>"
    $subject = "[New Version]: $version"

    $smtp = new-object Net.Mail.SmtpClient('smtp.gmail.com', 587)
    $smtp.EnableSsl = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential("<my-gmail-username>", "<my-gmail-password>")

    $message = New-Object Net.Mail.MailMessage
    $message.From = "<my-gmail-username>@gmail.com"
    $message.IsBodyHtml = $true
    $message.Subject = $subject
    $message.Body = MessageBody($version)
    $message.To.Add($mailTo)

    $smtp.Send($message)
}

$credentials = Get-AWSCredentials -ListStoredCredentials

if ($currentCredentials -and $currentCredentials.Count -gt 0) {
    $hasCredentials = $true
}

if (!$hasCredentials) {
     Initialize-AWSDefaults -AccessKey <your-amazon-access-key> -SecretKey <your-amazon-secret-key> -Region eu-west-1
}

$isFilePathExist = Test-Path $filePath

if (!$isFilePathExist) {
    "Folder ($filePath) for file is not existent! Exiting..."
    exit
}

# Filename and extension of file.
$key = (Get-ChildItem $filePath)[0].Name;

$file = (Get-ChildItem $filePath)[0].FullName

if ($key -and $file) {
    Write-S3Object -BucketName $bucketName -File $file -Key $key -PublicReadOnly

    SendEmail($key)
}
  • Save this script to <whatever-you-want-to-call-it>.ps1.
  • This Powershell script can be added to TeamCity as a final Build Step.
  • In (3) Build Steps click Add build step choose Powershell in listbox.
  • For Script choose File from drop down box.
  • For Script File add absolute path to saved Powershell script saved as ps1.

Note:

Sorry, no Linux or Mac shell version of this script.

Conclusion

Having short Git hash for a Revision in your versioning is very helpful and seven chars are mostly always enough to uniquely identify this release build with commit in your Git repository.

The amazon S3 upload + send email with developer commit deatails is not state of art Powershell script, it is very buggy and not that much flexible, but it can help a lot in communication with QA (or outside beta testers) and having release build version binary, right after Developer commit!