-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathuploadContentToStorageAccount.ps1
174 lines (146 loc) · 5.11 KB
/
uploadContentToStorageAccount.ps1
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$StorageAccountName,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$StorageAccountKey,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$SourcePath
)
function WriteLog
{
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,
[Switch]
$Throw
)
$Message = (Get-Date -Format G) + " -- $Message"
if ($Throw)
{
throw $Message
}
Write-Host $Message
}
function GetContentType
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$FilePath
)
$fileExtension = [System.IO.Path]::GetExtension($FilePath)
switch ($fileExtension)
{
".txt" { "text/plain" }
".json" { "application/json" }
default { "application/octet-stream"}
}
}
WriteLog -Message "Script started."
$CONTAINER_NAME = "builds"
$FUNC_RUNTIME_VERSION = '4'
if (-not (Test-Path $SourcePath))
{
throw "SourcePath '$SourcePath' does not exist."
}
WriteLog "Validating source path '$SourcePath'."
$filesToUpload = @(Get-ChildItem -Path "$SourcePath/*.zip" | ForEach-Object {$_.FullName})
if ($filesToUpload.Count -eq 0)
{
WriteLog -Message "'$SourcePath' does not contain any zip files to upload." -Throw
}
if (-not (Get-command New-AzStorageContext -ea SilentlyContinue))
{
WriteLog "Installing Az.Storage."
Install-Module Az.Storage -Force -Verbose -AllowClobber -Scope CurrentUser
}
$context = $null
try
{
WriteLog "Connecting to storage account..."
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -ErrorAction Stop
}
catch
{
$message = "Failed to authenticate with Azure. Please verify the StorageAccountName and StorageAccountKey. Exception information: $_"
WriteLog -Message $message -Throw
}
# Validate and read manifest file
$manifestFileName = "integrationTestsBuildManifest.json"
$manifestFilePath = Join-Path $SourcePath $manifestFileName
if (-not (Test-Path $manifestFilePath))
{
WriteLog -Message "File '$manifestFilePath' does not exist." -Throw
}
$manifest = $null
try
{
WriteLog -Message "Reading $manifestFileName."
$manifest = Get-Content $manifestFilePath -Raw | ConvertFrom-Json -ErrorAction Stop
$filesToUpload += $manifestFilePath
}
catch
{
WriteLog -Message "Failed to parse '$manifestFilePath'. Please make sure the file content is a valid JSON." -Throw
}
# Create a version.txt file from the integrationTestBuildManifest.json and add it to the list of files to upload
WriteLog -Message "Creating version.txt file..."
$version = $manifest.CoreToolsVersion
$versionFilePath = Join-Path $SourcePath "version.txt"
$version | Set-Content -Path $versionFilePath
$filesToUpload += $versionFilePath
# These are the destination paths in the storage account
# "https://<storageAccountName>.blob.core.windows.net/builds/$FUNC_RUNTIME_VERSION/latest/Azure.Functions.Cli.$os-$arch.zip"
# "https://<storageAccountName>.blob.core.windows.net/builds/$FUNC_RUNTIME_VERSION/$version/Azure.Functions.Cli.$os-$arch.zip"
$latestDestinationPath = "$FUNC_RUNTIME_VERSION/latest"
$versionDestinationPath = "$FUNC_RUNTIME_VERSION/$($version)"
# Delete the files in the latest folder if it is not empty
$filesToDelete = @(Get-AzStorageBlob -Container $CONTAINER_NAME -Context $context -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*$latestDestinationPath*" })
if ($filesToDelete.Count -gt 0)
{
WriteLog -Message "Deleting files in the latest folder...."
$filesToDelete | ForEach-Object {
Remove-AzStorageBlob -Container $CONTAINER_NAME -Context $context -Blob $_.Name -Force -ErrorAction SilentlyContinue | Out-Null
}
}
foreach ($path in @($latestDestinationPath, $versionDestinationPath))
{
foreach ($file in $filesToUpload)
{
$fileName = Split-Path $file -Leaf
$destinationPath = Join-Path $path $fileName
$contentType = GetContentType -FilePath $file
if ($destinationPath -like "*latest*")
{
# Remove the Core Tools version from the path for latest
$destinationPath = $destinationPath.Replace("." + $version, "")
}
try
{
WriteLog -Message "Uploading '$fileName' to '$destinationPath'."
Set-AzStorageBlobContent -File $file `
-Container $CONTAINER_NAME `
-Blob $destinationPath `
-Context $context `
-StandardBlobTier Hot `
-ErrorAction Stop `
-Properties @{"ContentType" = $contentType} `
-Force | Out-Null
}
catch
{
WriteLog -Message "Failed to upload file '$file' to storage account. Exception information: $_" -Throw
}
}
}
WriteLog -Message "Script completed."