Created
May 19, 2017 11:33
-
-
Save Hallmanac/5db9e8fa4c258c664cf2c1d0d1d65ed4 to your computer and use it in GitHub Desktop.
A PowerShell script that can be used in a VSTS Build to get a filtered list of file changes and have those files copied to the Staging directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# FileChangesForRelease.ps1 | |
# | |
# Gets the list of .rdl files that were added or modified since the previous commit (based on the PR) and copies them to the staging directory | |
# | |
$urlBase = $env:SYSTEM_TEAMFOUNDATIONSERVERURI + $env:SYSTEM_TEAMPROJECT + "/_apis/"; | |
$accessToken = $env:SYSTEM_ACCESSTOKEN; | |
$auth = "Bearer $accessToken"; | |
$accept = "application/json"; | |
$headers = @{authorization=$auth;accept=$accept}; | |
# Call the API to get the commit changes object of the lastMergeCommit which is the source of this build | |
$commitChangesUrl = $urlBase + "git/repositories/" + $env:BUILD_REPOSITORY_NAME + "/commits/" + $env:BUILD_SOURCEVERSION + "/changes?api-version=1.0"; | |
$commitChanges = Invoke-RestMethod -Uri $commitChangesUrl -Method Get -Headers $headers -ContentType "application/json"; | |
# Enumerate the $commitChanges.changes array and filter down -> blobs -> only the added/modified .rdl files -> "select/foreach" into an array of paths | |
[string[]]$rdlFilePaths = $commitChanges.changes ` | |
| Where-Object {$_.item.gitObjectType -eq "blob" -and $_.item.path.EndsWith(".rdl") -and ($_.changeType -eq "edit" -or $_.changeType -eq "add" -or $_.changeType -eq "edit, rename")} ` | |
| Select-Object item ` | |
| ForEach-Object {$_.item.path}; | |
Write-Host "rdlFilePaths are: $rdlFilePaths"; | |
# Enumerate the array of paths and copy each file into the staging directory under a sub-folder called release package | |
for($i=0; $i -lt $rdlFilePaths.Length; $i++){ | |
[string]$pathItem = [string]$rdlFilePaths[$i]; | |
Write-Host "Iteration $i || The pathItem is $pathItem"; | |
# Get the file name and replace all spaces with dashes to make it more path friendly | |
[int]$fileIndex = $pathItem.LastIndexOf('/'); | |
[string]$fileName = $pathItem.Substring($fileIndex + 1); | |
$fileName = $fileName.Replace(' ', '-').Trim(); | |
# Replace all the forward slashes (/) with back slashes (\) because Windows | |
$pathInstance = $env:BUILD_SOURCESDIRECTORY + $pathItem.Replace("/", "\"); | |
Write-Host "Iteration $i || The Path Instance: $pathInstance"; | |
# Establish the destination file name and path | |
$destination = $env:BUILD_STAGINGDIRECTORY + "\release-package\" + $fileName; | |
Write-Host "Iteration $i || The Destination staging directory is: $destination"; | |
# If the desitnation doesn't exist create it | |
if(!(Test-Path -Path $destination)) {New-Item $destination}; | |
# Makin' Copies! | |
Copy-Item "$pathInstance" -Destination "$destination" -Force; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment