forked from dotnet/docker-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
60 lines (53 loc) · 1.67 KB
/
build.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
[cmdletbinding()]
param(
[string]$DockerRepo = "mcr.microsoft.com/dotnet-buildtools/image-builder",
[switch]$PushImages,
[switch]$CleanupDocker,
[string]$TagTimestamp = (Get-Date -Format yyyyMMddHHmmss)
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Invoke-CleanupDocker($ActiveOS)
{
if ($CleanupDocker) {
if ("$ActiveOS" -eq "windows") {
# Windows base images are large, preserve them to avoid the overhead of pulling each time.
docker images |
Where-Object {
-Not ($_.StartsWith("mcr.microsoft.com/windows")`
-Or $_.StartsWith("REPOSITORY ")) } |
ForEach-Object { $_.Split(' ', [System.StringSplitOptions]::RemoveEmptyEntries)[2] } |
Select-Object -Unique |
ForEach-Object { docker rmi -f $_ }
}
else {
docker system prune -a -f
}
}
}
$(docker version) | % { Write-Host "$_" }
$activeOS = docker version -f "{{ .Server.Os }}"
Invoke-CleanupDocker $activeOS
try {
if ($activeOS -eq "windows") {
$osFlavor = "nanoserver"
}
else {
$osFlavor = "debian"
}
$stableTag = "$($DockerRepo):$osFlavor-$TagTimestamp"
$floatingTag = "image-builder"
& docker build --pull -t $stableTag -t $floatingTag -f "$($PSScriptRoot)/Dockerfile.$osFlavor" $PSScriptRoot
if ($LastExitCode -ne 0) {
throw "Failed building ImageBuilder"
}
if ($PushImages) {
& docker push $stableTag
if ($LastExitCode -ne 0) {
throw "Failed pushing images"
}
}
}
finally {
Invoke-CleanupDocker $activeOS
}