{"id":817,"date":"2017-12-11T13:51:13","date_gmt":"2017-12-11T13:51:13","guid":{"rendered":"http:\/\/dirk.schuermans.me\/?p=817"},"modified":"2017-12-11T13:51:13","modified_gmt":"2017-12-11T13:51:13","slug":"vsts-build-script-tagging-build","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=817","title":{"rendered":"VSTS build script: Tagging build"},"content":{"rendered":"<p>Hey everyone,<\/p>\n<p>I was recently working on my build &#038; release flows for my project at work, trying to optimise it as much as possible.<br \/>\nI had the requirement that I only wanted 1 build definition, and 1 release definition per component.<br \/>\nThe project consisted out of several components (e.g. an API, angularjs front-end, &#8230;) and I only wanted to release components which had changed.<\/p>\n<p>I put together a powershell script which, when called as follows <\/p>\n<pre name=\"code\" class=\"powershell\"><code>.\\TagBuild.ps1 -Tag Api -PathFilters sources\/My.WebApi,sources\/My.DAL,sources\/My.Common<\/code><\/pre>\n<p> would tag my VSTS build with the tag <strong>Api<\/strong> when any files were changed in the paths <strong>sources\/My.WebApi\/*, sources\/My.DAL\/* or sources\/My.Common\/<\/strong>*<\/p>\n<p>Then I could update my release definition for my API to only trigger an automated release if the build was tagged with <strong>Api<\/strong><\/p>\n<p><!--more--><\/p>\n<p>The script:<\/p>\n<pre name=\"code\" class=\"powershell\">\r\n<code>\r\n[CmdletBinding()]\r\nparam(\r\n    [Parameter(mandatory=$true)]\r\n    [ValidateNotNullOrEmpty()]\r\n    [string]$Tag,\r\n\r\n     [Parameter(mandatory=$true)]\r\n    [string[]]$PathFilters = @(),\r\n\r\n    [string]$UseVerbose = \"false\"\r\n)\r\nbegin {\r\n    $CurrentVerbosePreference = $VerbosePreference;\r\n\r\n    if($VerbosePreference -eq \"SilentlyContinue\" -And $UseVerbose -eq \"true\") {\r\n        $VerbosePreference = \"continue\"\r\n    }\r\n\t\r\n    \r\n    if($PathFilters.Length -eq 0)\r\n    {\r\n        Write-Verbose 'No path filters provided, stopping prematurely'\r\n        exit 0\r\n    }\r\n\r\n    Write-Host \"Tag: $Tag\"\r\n    Write-Host \"Filters: $PathFilters\"\r\n}\r\nprocess {\r\n    # Get changes\r\n    $changes=$(git diff head head~ --name-only)\r\n\r\n    foreach($file in $changes){\r\n        \r\n        if($null -ne ($PathFilters | ? { $file.StartsWith($_) })){\r\n            Write-Verbose \"$File matches a filter, tagging build.\"\r\n            Write-Host \"##vso[build.addbuildtag]$Tag\"\r\n            break\r\n        }\r\n        else{\r\n            Write-Verbose \"$File - NO MATCH\"\r\n        }\r\n    }\r\n}\r\nend {\r\n    $VerbosePreference = $CurrentVerbosePreference\r\n}\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone, I was recently working on my build &#038; release flows for my project at work, trying to optimise it as much as possible. I had the requirement that I only wanted 1 build definition, and 1 release definition per component. The project consisted out of several components (e.g. an API, angularjs front-end, &#8230;) &hellip; <a href=\"https:\/\/dirk.schuermans.me\/?p=817\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">VSTS build script: Tagging build<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/817"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=817"}],"version-history":[{"count":8,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/817\/revisions"}],"predecessor-version":[{"id":844,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/817\/revisions\/844"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}