-
Notifications
You must be signed in to change notification settings - Fork 3
/
Build-Containers.ps1
70 lines (47 loc) · 2.66 KB
/
Build-Containers.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
#Requires -Version 3.0
Param(
[Parameter(Mandatory=$True)]
[string]
$EnvironmentTag,
[string]
$VersionTag = "latest"
)
# stop the script on first error
$ErrorActionPreference = 'Stop'
#******************************************************************************
# Functions
#******************************************************************************
Function Build-Container([string]$ContainerTag, [string]$BuildContextFolderName, [string]$RegistryUrl) {
Write-Host "`r`n[BUILD CONTAINER] Building $ContainerTag container..." -foreground "green"
$buildContext = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $BuildContextFolderName))
$dockerfile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, "$BuildContextFolderName/Dockerfile"))
docker build -t $ContainerTag -f $dockerfile $buildContext
if ($LastExitCode -ne 0) {
throw "Could not build the image $ContainerTag from file: $dockerfile for context: $buildContext"
}
docker tag $ContainerTag $registryUrl/$ContainerTag
if ($LastExitCode -ne 0) {
throw "Could not tag the image $ContainerTag for $registryUrl/$ContainerTag"
}
Write-Host "[PUSH TO REPOSITORY] Pushing $ContainerTag container to $RegistryUrl..." -foreground "green"
docker push $RegistryUrl/$ContainerTag
if ($LastExitCode -ne 0) {
throw "Could not push the image $RegistryUrl/$ContainerTag"
}
Write-Host "[FINISHED] Finished building $ContainerTag container" -foreground "green"
}
#******************************************************************************
# Script body
#******************************************************************************
Write-Host "Logging in into the Azure Container Registry..."
$keyVaultName = "ca-devcache-$EnvironmentTag"
$registryUsername = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -SecretName registryAdminUsername).SecretValueText
$registryPassword = (Get-AzureKeyVaultSecret -VaultName $keyVaultName -SecretName registryAdminPassword).SecretValueText
$registryUrl = ("cadevcache" + $EnvironmentTag + "registry.azurecr.io")
docker login $registryUrl -u $registryUsername -p $registryPassword
if ($LastExitCode -ne 0) {
throw "Could not login to the docker registry"
}
Build-Container -ContainerTag "devicecache-frontend:$VersionTag" -BuildContextFolderName "DeviceCache.Frontend" -RegistryUrl $registryUrl
Build-Container -ContainerTag "devicecache-processor:$VersionTag" -BuildContextFolderName "DeviceCache.Processor" -RegistryUrl $registryUrl
Build-Container -ContainerTag "devicecache-simulator:$VersionTag" -BuildContextFolderName "DeviceCache.Simulator" -RegistryUrl $registryUrl